一个通用元素。Document
内容表示为元素。例如,ListItem
、Paragraph
和 Table
是元素,并继承 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()
asDate()
asEquation()
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
asFootnote()
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()
asPageBreak()
asParagraph()
asPerson()
asRichLink()
asTable()
asTableCell()
asTableOfContents()
以 TableOfContents
的形式返回当前元素。
每当给定元素属于特定类型时,使用此方法可帮助自动填充。
返回
TableOfContents
- 当前元素。
授权
使用此方法的脚本需要通过以下一个或多个范围进行授权:
-
https://www.googleapis.com/auth/documents.currentonly
-
https://www.googleapis.com/auth/documents
asTableRow()
asText()
copy()
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()
getParent()
检索元素的父元素。
父元素包含当前元素。
返回
ContainerElement
- 父元素。
授权
使用此方法的脚本需要通过以下一个或多个范围进行授权:
-
https://www.googleapis.com/auth/documents.currentonly
-
https://www.googleapis.com/auth/documents
getPreviousSibling()
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()
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);
参数
名称 | 类型 | 说明 |
---|---|---|
attributes | Object | 元素的属性。 |
返回
Element
- 当前元素。
授权
使用此方法的脚本需要通过以下一个或多个范围进行授权:
-
https://www.googleapis.com/auth/documents.currentonly
-
https://www.googleapis.com/auth/documents