Class InlineImage

InlineImage

表示嵌入图片的元素。InlineImage 可包含在 ListItemParagraph,除非 ListItemParagraph 介于 FootnoteSectionInlineImage 本身不能包含任何其他元素。对于 有关文档结构的更多信息,请参阅扩展 Google 文档指南

方法

方法返回类型简介
copy()InlineImage返回当前元素的分离的深层副本。
getAltDescription()String返回图片的替代说明。
getAltTitle()String返回图片的替代标题。
getAs(contentType)Blob将此对象中的数据作为转换为指定内容类型的 blob 返回。
getAttributes()Object检索元素的属性。
getBlob()Blob将此对象中的数据作为 blob 返回。
getHeight()Integer检索图片的高度(以像素为单位)。
getLinkUrl()String检索链接网址。
getNextSibling()Element检索该元素的下一个同级元素。
getParent()ContainerElement获取该元素的父元素。
getPreviousSibling()Element检索该元素的前一个同级元素。
getType()ElementType检索元素的 ElementType
getWidth()Integer检索图片的宽度(以像素为单位)。
isAtDocumentEnd()Boolean确定元素是否在 Document 的末尾。
merge()InlineImage将元素与前一个相同类型的同级元素合并。
removeFromParent()InlineImage将元素从其父元素中移除。
setAltDescription(description)InlineImage设置图片的替代说明。
setAltTitle(title)InlineImage设置图片的替代标题。
setAttributes(attributes)InlineImage设置元素的属性。
setHeight(height)InlineImage设置图片的高度(以像素为单位)。
setLinkUrl(url)InlineImage设置链接网址。
setWidth(width)InlineImage设置图片的宽度(以像素为单位)。

详细文档

copy()

返回当前元素的分离的深层副本。

元素中存在的所有子元素也会被复制。新元素不含 。

返回

InlineImage - 新副本。

授权

使用此方法的脚本需要获得以下一个或多个范围的授权:

  • https://www.googleapis.com/auth/documents.currentonly
  • https://www.googleapis.com/auth/documents

getAltDescription()

返回图片的替代说明。

返回

String - 备用标题,或者 null(如果元素没有备用标题)

授权

使用此方法的脚本需要获得以下一个或多个范围的授权:

  • https://www.googleapis.com/auth/documents.currentonly
  • https://www.googleapis.com/auth/documents

getAltTitle()

返回图片的替代标题。

返回

String - 备用标题,或者 null(如果元素没有备用标题)

授权

使用此方法的脚本需要获得以下一个或多个范围的授权:

  • https://www.googleapis.com/auth/documents.currentonly
  • https://www.googleapis.com/auth/documents

getAs(contentType)

将此对象中的数据作为转换为指定内容类型的 blob 返回。本次 方法会为文件名添加相应的扩展名,例如“myfile.pdf”。不过, 最后一个句点之后的文件名部分(如果有)假定存在 扩展名。因此,“ShoppingList.12.25.2014”会变为 “ShoppingList.12.25.pdf”。

要查看转化次数的每日配额,请参阅 Google 配额 服务。新创建的 Google Workspace 网域可能会暂时适用更严格的条件 配额。

参数

名称类型说明
contentTypeString要转换为的 MIME 类型。对于大多数 blob,'application/pdf' 为 唯一有效的选项对于 BMP、GIF、JPEG 或 PNG 格式的图片,'image/bmp''image/gif''image/jpeg''image/png' 中的任何一个也 有效。对于 Google 文档,'text/markdown' 也有效。

返回

Blob - 以 blob 形式表示的数据。


getAttributes()

检索元素的属性。

结果会得到一个对象,其中包含每个有效元素属性对应的属性,其中每个 属性名称对应于 DocumentApp.Attribute 枚举中的一个项。

var doc = DocumentApp.getActiveDocument();
var documentTab = doc.getActiveTab().asDocumentTab();
var body = documentTab.getBody();

// Append a styled paragraph.
var par = body.appendParagraph('A bold, italicized paragraph.');
par.setBold(true);
par.setItalic(true);

// Retrieve the paragraph's attributes.
var atts = par.getAttributes();

// Log the paragraph attributes.
for (var att in atts) {
  Logger.log(att + ":" + atts[att]);
}

返回

Object - 元素的属性。

授权

使用此方法的脚本需要获得以下一个或多个范围的授权:

  • https://www.googleapis.com/auth/documents.currentonly
  • https://www.googleapis.com/auth/documents

getBlob()

将此对象中的数据作为 blob 返回。

返回

Blob - 以 blob 形式表示的数据。


getHeight()

检索图片的高度(以像素为单位)。

返回

Integer - 图片的高度(以像素为单位)

授权

使用此方法的脚本需要获得以下一个或多个范围的授权:

  • https://www.googleapis.com/auth/documents.currentonly
  • https://www.googleapis.com/auth/documents

getLinkUrl()

检索链接网址。

返回

String - 链接网址;如果元素包含多个此键的值,则为 null 属性。

授权

使用此方法的脚本需要获得以下一个或多个范围的授权:

  • https://www.googleapis.com/auth/documents.currentonly
  • https://www.googleapis.com/auth/documents

getNextSibling()

检索该元素的下一个同级元素。

下一个同级元素具有相同的父级元素并位于当前元素之后。

返回

Element - 下一个同级元素。

授权

使用此方法的脚本需要获得以下一个或多个范围的授权:

  • https://www.googleapis.com/auth/documents.currentonly
  • https://www.googleapis.com/auth/documents

getParent()

获取该元素的父元素。

父元素包含当前元素。

返回

ContainerElement - 父元素。

授权

使用此方法的脚本需要获得以下一个或多个范围的授权:

  • https://www.googleapis.com/auth/documents.currentonly
  • https://www.googleapis.com/auth/documents

getPreviousSibling()

检索该元素的前一个同级元素。

上一个同级项具有相同的父元素,并且位于当前元素之前。

返回

Element - 上一个同级元素。

授权

使用此方法的脚本需要获得以下一个或多个范围的授权:

  • https://www.googleapis.com/auth/documents.currentonly
  • https://www.googleapis.com/auth/documents

getType()

检索元素的 ElementType

使用 getType() 确定指定元素的确切类型。

var doc = DocumentApp.getActiveDocument();
var documentTab = doc.getActiveTab().asDocumentTab();
var body = documentTab.getBody();

// Obtain the first element in the active tab's body.

var firstChild = body.getChild(0);

// Use getType() to determine the element's type.
if (firstChild.getType() == DocumentApp.ElementType.PARAGRAPH) {
  Logger.log('The first element is a paragraph.');
} else {
  Logger.log('The first element is not a paragraph.');
}

返回

ElementType - 元素类型。

授权

使用此方法的脚本需要获得以下一个或多个范围的授权:

  • https://www.googleapis.com/auth/documents.currentonly
  • https://www.googleapis.com/auth/documents

getWidth()

检索图片的宽度(以像素为单位)。

返回

Integer - 图片的宽度(以像素为单位)

授权

使用此方法的脚本需要获得以下一个或多个范围的授权:

  • https://www.googleapis.com/auth/documents.currentonly
  • https://www.googleapis.com/auth/documents

isAtDocumentEnd()

确定元素是否在 Document 的末尾。

返回

Boolean - 表示元素是否在标签页的末尾。

授权

使用此方法的脚本需要获得以下一个或多个范围的授权:

  • https://www.googleapis.com/auth/documents.currentonly
  • https://www.googleapis.com/auth/documents

merge()

将元素与前一个相同类型的同级元素合并。

只能合并同一 ElementType 的元素。文件中包含的 当前元素会移动到前一个同级元素。

当前元素会从文档中移除。

var doc = DocumentApp.getActiveDocument();
var documentTab = doc.getActiveTab().asDocumentTab();
var body = documentTab.getBody();

// Example 1: Merge paragraphs
// Append two paragraphs to the document's active tab.
var par1 = body.appendParagraph('Paragraph 1.');
var par2 = body.appendParagraph('Paragraph 2.');
// Merge the newly added paragraphs into a single paragraph.
par2.merge();

// Example 2: Merge table cells
// Create a two-dimensional array containing the table's cell contents.
var cells = [
['Row 1, Cell 1', 'Row 1, Cell 2'],
['Row 2, Cell 1', 'Row 2, Cell 2']
];
// Build a table from the array.
var table = body.appendTable(cells);
// Get the first row in the table.
 var row = table.getRow(0);
// Get the two cells in this row.
var cell1 = row.getCell(0);
var cell2 = row.getCell(1);
// Merge the current cell into its preceding sibling element.
var merged = cell2.merge();

返回

InlineImage - 合并的元素。

授权

使用此方法的脚本需要获得以下一个或多个范围的授权:

  • https://www.googleapis.com/auth/documents.currentonly
  • https://www.googleapis.com/auth/documents

removeFromParent()

将元素从其父元素中移除。

var doc = DocumentApp.getActiveDocument();
var documentTab = doc.getActiveTab().asDocumentTab()
var body = documentTab.getBody();

// Remove all images in the active tab's body.
var imgs = body.getImages();
for (var i = 0; i < imgs.length; i++) {
  imgs[i].removeFromParent();
}

返回

InlineImage - 已移除的元素。

授权

使用此方法的脚本需要获得以下一个或多个范围的授权:

  • https://www.googleapis.com/auth/documents.currentonly
  • https://www.googleapis.com/auth/documents

setAltDescription(description)

设置图片的替代说明。如果给定标题为 null,则将 空字符串。

参数

名称类型说明
descriptionString备选标题

返回

InlineImage - 当前对象

授权

使用此方法的脚本需要获得以下一个或多个范围的授权:

  • https://www.googleapis.com/auth/documents.currentonly
  • https://www.googleapis.com/auth/documents

setAltTitle(title)

设置图片的替代标题。如果给定标题为 null,则将标题设置为 空字符串。

参数

名称类型说明
titleString备选标题

返回

InlineImage - 当前对象

授权

使用此方法的脚本需要获得以下一个或多个范围的授权:

  • https://www.googleapis.com/auth/documents.currentonly
  • https://www.googleapis.com/auth/documents

setAttributes(attributes)

设置元素的属性。

指定的属性参数必须是一个对象,其中每个属性名称都是 DocumentApp.Attribute 枚举,并且每个属性值都是要 。

var doc = DocumentApp.getActiveDocument();
var documentTab = doc.getActiveTab().asDocumentTab();
var body = documentTab.getBody();

// Define a custom paragraph style.
var style = {};
style[DocumentApp.Attribute.HORIZONTAL_ALIGNMENT] =
    DocumentApp.HorizontalAlignment.RIGHT;
style[DocumentApp.Attribute.FONT_FAMILY] = 'Calibri';
style[DocumentApp.Attribute.FONT_SIZE] = 18;
style[DocumentApp.Attribute.BOLD] = true;

// Append a plain paragraph.
var par = body.appendParagraph('A paragraph with custom style.');

// Apply the custom style.
par.setAttributes(style);

参数

名称类型说明
attributesObject元素的属性。

返回

InlineImage - 当前元素。

授权

使用此方法的脚本需要获得以下一个或多个范围的授权:

  • https://www.googleapis.com/auth/documents.currentonly
  • https://www.googleapis.com/auth/documents

setHeight(height)

设置图片的高度(以像素为单位)。

参数

名称类型说明
heightInteger图片的高度(以像素为单位)

返回

InlineImage - 当前对象

授权

使用此方法的脚本需要获得以下一个或多个范围的授权:

  • https://www.googleapis.com/auth/documents.currentonly
  • https://www.googleapis.com/auth/documents

setLinkUrl(url)

设置链接网址。如果给定网址为 null 或空字符串,此方法将创建 包含空白网址的链接(可能会显示为“无效链接”)。

参数

名称类型说明
urlString链接网址

返回

InlineImage - 当前对象

授权

使用此方法的脚本需要获得以下一个或多个范围的授权:

  • https://www.googleapis.com/auth/documents.currentonly
  • https://www.googleapis.com/auth/documents

setWidth(width)

设置图片的宽度(以像素为单位)。

参数

名称类型说明
widthInteger图片的宽度(以像素为单位)

返回

InlineImage - 当前对象

授权

使用此方法的脚本需要获得以下一个或多个范围的授权:

  • https://www.googleapis.com/auth/documents.currentonly
  • https://www.googleapis.com/auth/documents