Interface Element

元素

通用元素。Document 内容表示为元素。例如,ListItemParagraphTable 是元素,会继承 Element 定义的所有方法(例如 getType())。

实现类

名称简介
Body表示文档正文的元素。
ContainerElement可能包含其他元素的通用元素。
Date表示设置了格式的日期的元素
Equation表示数学表达式的元素。
EquationFunction一个元素,在数学 Equation 中表示函数。
EquationFunctionArgumentSeparator一个元素,表示数学 Equation 中的函数分隔符。
EquationSymbol一个元素,表示数学 Equation 中的符号。
FooterSection表示页脚部分的元素。
Footnote表示脚注的元素。
FootnoteSection表示脚注部分的元素。
HeaderSection表示标题部分的元素。
HorizontalRule一个表示水平规则的元素。
InlineDrawing表示嵌入的绘图的元素。
InlineImage表示嵌入图片的元素。
ListItem表示列表项的元素。
PageBreak表示分页符的元素。
Paragraph表示段落的元素。
Person表示人员链接的元素。
RichLink表示指向 Google 资源(例如云端硬盘文件或 YouTube 视频)的链接的元素。
Table表示表的元素。
TableCell表示表格单元格的元素。
TableOfContents包含目录的元素。
TableRow表示表格行的元素。
Text表示富文本区域的元素。
UnsupportedElement表示未知或无法受脚本影响的区域的元素,例如页码。

方法

方法返回类型简介
asBody()BodyBody 的形式返回当前元素。
asDate()DateDate 的形式返回当前元素。
asEquation()EquationEquation 的形式返回当前元素。
asEquationFunction()EquationFunctionEquationFunction 的形式返回当前元素。
asEquationFunctionArgumentSeparator()EquationFunctionArgumentSeparatorEquationFunctionArgumentSeparator 的形式返回当前元素。
asEquationSymbol()EquationSymbolEquationSymbol 的形式返回当前元素。
asFooterSection()FooterSectionFooterSection 的形式返回当前元素。
asFootnote()FootnoteFootnote 的形式返回当前元素。
asFootnoteSection()FootnoteSectionFootnoteSection 的形式返回当前元素。
asHeaderSection()HeaderSectionHeaderSection 的形式返回当前元素。
asHorizontalRule()HorizontalRuleHorizontalRule 的形式返回当前元素。
asInlineDrawing()InlineDrawingInlineDrawing 的形式返回当前元素。
asInlineImage()InlineImageInlineImage 的形式返回当前元素。
asListItem()ListItemListItem 的形式返回当前元素。
asPageBreak()PageBreakPageBreak 的形式返回当前元素。
asParagraph()ParagraphParagraph 的形式返回当前元素。
asPerson()PersonPerson 的形式返回当前元素。
asRichLink()RichLinkRichLink 形式返回当前元素,例如指向 Google 表格文件的链接。
asTable()TableTable 的形式返回当前元素。
asTableCell()TableCellTableCell 的形式返回当前元素。
asTableOfContents()TableOfContentsTableOfContents 的形式返回当前元素。
asTableRow()TableRowTableRow 的形式返回当前元素。
asText()TextText 的形式返回当前元素。
copy()Element返回当前元素的已分离的深层副本。
getAttributes()Object检索元素的属性。
getNextSibling()Element检索元素的下一个同级元素。
getParent()ContainerElement获取元素的父元素。
getPreviousSibling()Element检索元素的上一个同级元素。
getType()ElementType检索元素的 ElementType
isAtDocumentEnd()Boolean确定元素是否位于 Document 的末尾。
merge()Element将元素与同一类型的上一个同级元素合并。
removeFromParent()Element将元素从其父项中移除。
setAttributes(attributes)Element设置元素的属性。

详细文档

asBody()

Body 形式返回当前元素。

如果已知给定元素为特定类型的元素,可使用此方法来协助自动补全。

弃踢回攻

Body - 当前元素。

授权

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

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

asDate()

Date 形式返回当前元素。

如果您知道某个元素是 Date,可以使用此方法将其类型设置为 Date。这样,Apps 脚本编辑器中的自动补全功能就会向您显示可与 Date 搭配使用的方法。

弃踢回攻

Date - 类型设置为 Date 的当前元素。

授权

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

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

asEquation()

Equation 的形式返回当前元素。

如果已知给定元素为特定类型的元素,可使用此方法来协助自动补全。

弃踢回攻

Equation - 当前元素。

授权

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

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

asEquationFunction()

EquationFunction 形式返回当前元素。

如果已知给定元素为特定类型的元素,可使用此方法来协助自动补全。

弃踢回攻

EquationFunction - 当前元素。

授权

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

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

asEquationFunctionArgumentSeparator()

EquationFunctionArgumentSeparator 形式返回当前元素。

如果已知给定元素为特定类型的元素,可使用此方法来协助自动补全。

弃踢回攻

EquationFunctionArgumentSeparator - 当前元素。

授权

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

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

asEquationSymbol()

EquationSymbol 形式返回当前元素。

如果已知给定元素为特定类型的元素,可使用此方法来协助自动补全。

弃踢回攻

EquationSymbol - 当前元素。

授权

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

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

asFooterSection()

FooterSection 形式返回当前元素。

如果已知给定元素为特定类型的元素,可使用此方法来协助自动补全。

弃踢回攻

FooterSection - 当前元素。

授权

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

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

asFootnote()

Footnote 形式返回当前元素。

如果已知给定元素为特定类型的元素,可使用此方法来协助自动补全。

弃踢回攻

Footnote - 当前元素。

授权

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

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

asFootnoteSection()

FootnoteSection 形式返回当前元素。

如果已知给定元素为特定类型的元素,可使用此方法来协助自动补全。

弃踢回攻

FootnoteSection - 当前元素。

授权

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

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

asHeaderSection()

HeaderSection 形式返回当前元素。

如果已知给定元素为特定类型的元素,可使用此方法来协助自动补全。

弃踢回攻

HeaderSection - 当前元素。

授权

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

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

asHorizontalRule()

HorizontalRule 形式返回当前元素。

如果已知给定元素为特定类型的元素,可使用此方法来协助自动补全。

弃踢回攻

HorizontalRule - 当前元素。

授权

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

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

asInlineDrawing()

InlineDrawing 形式返回当前元素。

如果已知给定元素为特定类型的元素,可使用此方法来协助自动补全。

弃踢回攻

InlineDrawing - 当前元素。

授权

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

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

asInlineImage()

InlineImage 形式返回当前元素。

如果已知给定元素为特定类型的元素,可使用此方法来协助自动补全。

弃踢回攻

InlineImage - 当前元素。

授权

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

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

asListItem()

ListItem 形式返回当前元素。

如果已知给定元素为特定类型的元素,可使用此方法来协助自动补全。

弃踢回攻

ListItem - 当前元素。

授权

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

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

asPageBreak()

PageBreak 形式返回当前元素。

如果已知给定元素为特定类型的元素,可使用此方法来协助自动补全。

弃踢回攻

PageBreak - 当前元素。

授权

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

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

asParagraph()

Paragraph 形式返回当前元素。

如果已知给定元素为特定类型的元素,可使用此方法来协助自动补全。

弃踢回攻

Paragraph - 当前元素。

授权

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

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

asPerson()

Person 形式返回当前元素。

如果您知道某个元素是 Person,可以使用此方法将其类型设置为人物。这样,Apps 脚本编辑器中的自动补全功能就会向您显示可对人物元素使用的方法。

弃踢回攻

Person - 类型设置为 Person 的当前元素。

授权

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

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

RichLink 形式返回当前元素,例如指向 Google 表格文件的链接。

如果您知道某个元素是 RichLink,可以使用此方法将其类型设置为 RichLink。这样,Apps 脚本编辑器中的自动补全功能就会向您显示可与 RichLink 搭配使用的方法。

弃踢回攻

RichLink - 类型设置为 RichLink 的当前元素。

授权

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

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

asTable()

Table 形式返回当前元素。

如果已知给定元素为特定类型的元素,可使用此方法来协助自动补全。

弃踢回攻

Table - 当前元素。

授权

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

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

asTableCell()

TableCell 形式返回当前元素。

如果已知给定元素为特定类型的元素,可使用此方法来协助自动补全。

弃踢回攻

TableCell - 当前元素。

授权

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

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

asTableOfContents()

TableOfContents 形式返回当前元素。

如果已知给定元素为特定类型的元素,可使用此方法来协助自动补全。

弃踢回攻

TableOfContents - 当前元素。

授权

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

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

asTableRow()

TableRow 形式返回当前元素。

如果已知给定元素为特定类型的元素,可使用此方法来协助自动补全。

弃踢回攻

TableRow - 当前元素。

授权

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

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

asText()

Text 形式返回当前元素。

如果已知给定元素为特定类型的元素,可使用此方法来协助自动补全。

弃踢回攻

Text - 当前元素。

授权

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

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

copy()

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

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

弃踢回攻

Element - 新副本。

授权

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

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

getAttributes()

检索元素的属性。

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

var body = DocumentApp.getActiveDocument().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

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 body = DocumentApp.getActiveDocument().getBody();

// Obtain the first element in the document 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

isAtDocumentEnd()

确定元素是否位于 Document 的末尾。

弃踢回攻

Boolean - 元素是否位于文档末尾。

授权

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

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

merge()

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

只能合并同一 ElementType 的元素。当前元素中包含的任何子元素都将移动到前面的同级元素。

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

var body = DocumentApp.getActiveDocument().getBody();
// Example 1: Merge paragraphs
// Append two paragraphs to the document.
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();

弃踢回攻

Element - 已合并的元素。

授权

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

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

removeFromParent()

将元素从其父项中移除。

var body = DocumentApp.getActiveDocument().getBody();

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

弃踢回攻

Element - 已移除的元素。

授权

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

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

setAttributes(attributes)

设置元素的属性。

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

var body = DocumentApp.getActiveDocument().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元素的属性。

弃踢回攻

Element - 当前元素。

授权

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

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