通用元素。Document 内容以元素的形式表示。例如,List、Paragraph 和 Table 是元素,并继承了 Element 定义的所有方法,例如 get。
实现类
| 名称 | 简介 | 
|---|---|
| Body | Google 文档中标签页中的内容。 | 
| Container | 一个可能包含其他元素的通用元素。 | 
| Date | 表示格式化日期的元素 | 
| Equation | 表示数学表达式的元素。 | 
| Equation | 表示数学 Equation中的函数的元素。 | 
| Equation | 表示数学 Equation中的函数分隔符的元素。 | 
| Equation | 表示数学 Equation中的符号的元素。 | 
| Footer | 表示页脚部分的元素。 | 
| Footnote | 表示脚注的元素。 | 
| Footnote | 表示脚注部分的元素。 | 
| Header | 表示标题部分的元素。 | 
| Horizontal | 表示水平线的元素。 | 
| Inline | 表示嵌入式绘图的元素。 | 
| Inline | 表示嵌入图片的元素。 | 
| List | 表示列表项的元素。 | 
| Page | 表示分页的元素。 | 
| Paragraph | 表示段落的元素。 | 
| Person | 表示指向人物的链接的元素。 | 
| Rich | 表示指向 Google 资源(例如云端硬盘文件或 YouTube 视频)的链接的元素。 | 
| Table | 表示表格的元素。 | 
| Table | 表示表格单元格的元素。 | 
| Table | 包含目录的元素。 | 
| Table | 表示表格的行。 | 
| Text | 表示富文本区域的元素。 | 
| Unsupported | 表示未知或无法受脚本影响的区域的元素,例如页码。 | 
方法
详细文档
as
as
as
as
以 Equation 的形式返回当前元素。
每当知道给定元素属于特定类型时,请使用此方法来辅助自动补全。
返回
Equation - 当前元素。
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
- 
https://www.googleapis.com/auth/documents.currentonly
- 
https://www.googleapis.com/auth/documents
as
以 Equation 的形式返回当前元素。
每当知道给定元素属于特定类型时,请使用此方法来辅助自动补全。
返回
Equation - 当前元素。
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
- 
https://www.googleapis.com/auth/documents.currentonly
- 
https://www.googleapis.com/auth/documents
as
以 Equation 的形式返回当前元素。
每当知道给定元素属于特定类型时,请使用此方法来辅助自动补全。
返回
Equation - 当前元素。
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
- 
https://www.googleapis.com/auth/documents.currentonly
- 
https://www.googleapis.com/auth/documents
as
as
以 Footnote 的形式返回当前元素。
每当知道给定元素属于特定类型时,请使用此方法来辅助自动补全。
返回
Footnote - 当前元素。
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
- 
https://www.googleapis.com/auth/documents.currentonly
- 
https://www.googleapis.com/auth/documents
as
以 Header 的形式返回当前元素。
每当知道给定元素属于特定类型时,请使用此方法来辅助自动补全。
返回
Header - 当前元素。
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
- 
https://www.googleapis.com/auth/documents.currentonly
- 
https://www.googleapis.com/auth/documents
as
以 Horizontal 的形式返回当前元素。
每当知道给定元素属于特定类型时,请使用此方法来辅助自动补全。
返回
Horizontal - 当前元素。
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
- 
https://www.googleapis.com/auth/documents.currentonly
- 
https://www.googleapis.com/auth/documents
as
以 Inline 的形式返回当前元素。
每当知道给定元素属于特定类型时,请使用此方法来辅助自动补全。
返回
Inline - 当前元素。
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
- 
https://www.googleapis.com/auth/documents.currentonly
- 
https://www.googleapis.com/auth/documents
as
以 Inline 的形式返回当前元素。
每当知道给定元素属于特定类型时,请使用此方法来辅助自动补全。
返回
Inline - 当前元素。
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
- 
https://www.googleapis.com/auth/documents.currentonly
- 
https://www.googleapis.com/auth/documents
as
as
as
as
as
as
as
as
以 Table 的形式返回当前元素。
每当知道给定元素属于特定类型时,请使用此方法来辅助自动补全。
返回
Table - 当前元素。
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
- 
https://www.googleapis.com/auth/documents.currentonly
- 
https://www.googleapis.com/auth/documents
as
as
copy()
get
检索元素的属性。
结果是一个对象,其中包含每个有效元素属性的属性,每个属性名称对应于 Document 枚举中的项。
const doc = DocumentApp.getActiveDocument(); const documentTab = doc.getActiveTab().asDocumentTab(); const body = documentTab.getBody(); // Append a styled paragraph. const par = body.appendParagraph('A bold, italicized paragraph.'); par.setBold(true); par.setItalic(true); // Retrieve the paragraph's attributes. const atts = par.getAttributes(); // Log the paragraph attributes. for (const att in atts) { Logger.log(`${att}:${atts[att]}`); }
返回
Object - 元素的属性。
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
- 
https://www.googleapis.com/auth/documents.currentonly
- 
https://www.googleapis.com/auth/documents
get
get
检索元素的父元素。
父元素包含当前元素。
返回
Container - 父元素。
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
- 
https://www.googleapis.com/auth/documents.currentonly
- 
https://www.googleapis.com/auth/documents
get
get
检索元素的 Element。
使用 get 确定给定元素的确切类型。
const doc = DocumentApp.getActiveDocument(); const documentTab = doc.getActiveTab().asDocumentTab(); const body = documentTab.getBody(); // Obtain the first element in the active tab's body. const 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.'); }
返回
Element - 元素类型。
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
- 
https://www.googleapis.com/auth/documents.currentonly
- 
https://www.googleapis.com/auth/documents
is
merge()
将元素与同一类型的上一个同级元素合并。
只有同一 Element 的元素才能合并。当前元素包含的所有子元素都会移至前面的同级元素。
当前元素会从文档中移除。
const doc = DocumentApp.getActiveDocument(); const documentTab = doc.getActiveTab().asDocumentTab(); const body = documentTab.getBody(); // Example 1: Merge paragraphs // Append two paragraphs to the document's active tab. const par1 = body.appendParagraph('Paragraph 1.'); const 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. const cells = [ ['Row 1, Cell 1', 'Row 1, Cell 2'], ['Row 2, Cell 1', 'Row 2, Cell 2'], ]; // Build a table from the array. const table = body.appendTable(cells); // Get the first row in the table. const row = table.getRow(0); // Get the two cells in this row. const cell1 = row.getCell(0); const cell2 = row.getCell(1); // Merge the current cell into its preceding sibling element. const merged = cell2.merge();
返回
Element - 合并后的元素。
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
- 
https://www.googleapis.com/auth/documents.currentonly
- 
https://www.googleapis.com/auth/documents
remove
从其父元素中移除元素。
const doc = DocumentApp.getActiveDocument(); const documentTab = doc.getActiveTab().asDocumentTab(); const body = documentTab.getBody(); // Remove all images in the active tab's body. const imgs = body.getImages(); for (let i = 0; i < imgs.length; i++) { imgs[i].removeFromParent(); }
返回
Element - 移除的元素。
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
- 
https://www.googleapis.com/auth/documents.currentonly
- 
https://www.googleapis.com/auth/documents
set
设置元素的属性。
指定的 attributes 参数必须是对象,其中每个属性名称都是 Document 枚举中的项,每个属性值都是要应用的新值。
const doc = DocumentApp.getActiveDocument(); const documentTab = doc.getActiveTab().asDocumentTab(); const body = documentTab.getBody(); // Define a custom paragraph style. const 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. const 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