表示脚注部分的元素。FootnoteSection
包含与 Footnote
对应的文本。FootnoteSection
可以包含 ListItem
或 Paragraph
元素。如需详细了解文档结构,请参阅关于扩展 Google 文档的指南。
方法
详细文档
appendParagraph(paragraph)
appendParagraph(text)
clear()
copy()
返回当前元素的已分离深层副本。
元素中的所有子元素也会被复制。新元素没有父元素。
返回
FootnoteSection
- 新副本。
授权
使用此方法的脚本需要通过以下一个或多个范围进行授权:
-
https://www.googleapis.com/auth/documents.currentonly
-
https://www.googleapis.com/auth/documents
editAsText()
获取当前元素的 Text
版本,以进行修改。
使用 editAsText
将元素内容处理为富文本。editAsText
模式会忽略非文本元素(例如 InlineImage
和 HorizontalRule
)。
完全包含在已删除文本范围内的子元素会从该元素中移除。
var body = DocumentApp.getActiveDocument().getBody(); // Insert two paragraphs separated by a paragraph containing an // horizontal rule. body.insertParagraph(0, "An editAsText sample."); body.insertHorizontalRule(0); body.insertParagraph(0, "An example."); // Delete " sample.\n\n An" removing the horizontal rule in the process. body.editAsText().deleteText(14, 25);
返回
Text
- 当前元素的文本版本
findElement(elementType)
在该元素的内容中搜索指定类型的后代。
参数
名称 | 类型 | 说明 |
---|---|---|
elementType | ElementType | 要搜索的元素的类型 |
返回
RangeElement
- 指示搜索元素位置的搜索结果
授权
使用此方法的脚本需要通过以下一个或多个范围进行授权:
-
https://www.googleapis.com/auth/documents.currentonly
-
https://www.googleapis.com/auth/documents
findElement(elementType, from)
从指定的 RangeElement
开始,在元素的内容中搜索指定类型的后代。
// Get the body section of the active document. var body = DocumentApp.getActiveDocument().getBody(); // Define the search parameters. var searchType = DocumentApp.ElementType.PARAGRAPH; var searchHeading = DocumentApp.ParagraphHeading.HEADING1; var searchResult = null; // Search until the paragraph is found. while (searchResult = body.findElement(searchType, searchResult)) { var par = searchResult.getElement().asParagraph(); if (par.getHeading() == searchHeading) { // Found one, update and stop. par.setText('This is the first header.'); return; } }
参数
名称 | 类型 | 说明 |
---|---|---|
elementType | ElementType | 要搜索的元素的类型 |
from | RangeElement | 要搜索的搜索结果 |
返回
RangeElement
- 指示搜索元素下一个位置的搜索结果
授权
使用此方法的脚本需要通过以下一个或多个范围进行授权:
-
https://www.googleapis.com/auth/documents.currentonly
-
https://www.googleapis.com/auth/documents
findText(searchPattern)
使用正则表达式在元素的内容中搜索指定的文本模式。
不完全支持部分 JavaScript 正则表达式功能,例如捕获组和模式修饰符。
提供的正则表达式模式与当前元素中包含的每个文本块都独立匹配。
参数
名称 | 类型 | 说明 |
---|---|---|
searchPattern | String | 要搜索的模式 |
返回
RangeElement
- 表示搜索文本位置的搜索结果;如果没有匹配项,则返回 null
授权
使用此方法的脚本需要通过以下一个或多个范围进行授权:
-
https://www.googleapis.com/auth/documents.currentonly
-
https://www.googleapis.com/auth/documents
findText(searchPattern, from)
从指定的搜索结果中开始搜索指定文本格式的元素内容。
不完全支持部分 JavaScript 正则表达式功能,例如捕获组和模式修饰符。
提供的正则表达式模式与当前元素中包含的每个文本块都独立匹配。
参数
名称 | 类型 | 说明 |
---|---|---|
searchPattern | String | 要搜索的模式 |
from | RangeElement | 要搜索的搜索结果 |
返回
RangeElement
- 表示搜索文本下一个位置的搜索结果,如果没有匹配项,则返回 null
授权
使用此方法的脚本需要通过以下一个或多个范围进行授权:
-
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
getChild(childIndex)
检索指定的子索引处的子元素。
// Get the body section of the active document. var body = DocumentApp.getActiveDocument().getBody(); // Obtain the first element in the document. var firstChild = body.getChild(0); // If it's a paragraph, set its contents. if (firstChild.getType() == DocumentApp.ElementType.PARAGRAPH) { firstChild.asParagraph().setText("This is the first paragraph."); }
参数
名称 | 类型 | 说明 |
---|---|---|
childIndex | Integer | 要检索的子元素的索引 |
返回
Element
- 指定索引处的子元素
授权
使用此方法的脚本需要通过以下一个或多个范围进行授权:
-
https://www.googleapis.com/auth/documents.currentonly
-
https://www.googleapis.com/auth/documents
getChildIndex(child)
getNextSibling()
getNumChildren()
检索子项的数量。
// Get the body section of the active document. var body = DocumentApp.getActiveDocument().getBody(); // Log the number of elements in the document. Logger.log("There are " + body.getNumChildren() + " elements in the document body.");
返回
Integer
- 子项的数量
授权
使用此方法的脚本需要通过以下一个或多个范围进行授权:
-
https://www.googleapis.com/auth/documents.currentonly
-
https://www.googleapis.com/auth/documents
getParagraphs()
检索该部分包含的所有 Paragraphs
(包括 ListItems
)。
返回
Paragraph[]
- 段落
授权
使用此方法的脚本需要通过以下一个或多个范围进行授权:
-
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()
getText()
以文本字符串的形式检索元素的内容。
返回
String
- 以文本字符串形式显示的元素内容
授权
使用此方法的脚本需要通过以下一个或多个范围进行授权:
-
https://www.googleapis.com/auth/documents.currentonly
-
https://www.googleapis.com/auth/documents
getTextAlignment()
获取文本对齐方式。可用的对齐类型为 DocumentApp.TextAlignment.NORMAL
、DocumentApp.TextAlignment.SUBSCRIPT
和 DocumentApp.TextAlignment.SUPERSCRIPT
。
返回
TextAlignment
- 文本对齐类型,如果文本包含多种类型的文本对齐,或者从未设置过文本对齐,则为 null
授权
使用此方法的脚本需要通过以下一个或多个范围进行授权:
-
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
insertParagraph(childIndex, paragraph)
insertParagraph(childIndex, text)
removeChild(child)
移除指定的子元素。
参数
名称 | 类型 | 说明 |
---|---|---|
child | Element | 要移除的子元素 |
返回
FootnoteSection
- 当前元素
授权
使用此方法的脚本需要通过以下一个或多个范围进行授权:
-
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(); }
返回
FootnoteSection
- 已移除的元素。
授权
使用此方法的脚本需要通过以下一个或多个范围进行授权:
-
https://www.googleapis.com/auth/documents.currentonly
-
https://www.googleapis.com/auth/documents
replaceText(searchPattern, replacement)
使用正则表达式替换给定文本模式中出现的所有字符。
搜索模式会作为字符串(而非 JavaScript 正则表达式对象)传递。因此,您需要对格式中的所有反斜杠进行转义。
此方法使用 Google 的 RE2 正则表达式库,该库限制了受支持的语法。
提供的正则表达式模式与当前元素中包含的每个文本块都独立匹配。
var body = DocumentApp.getActiveDocument().getBody(); // Clear the text surrounding "Apps Script", with or without text. body.replaceText("^.*Apps ?Script.*$", "Apps Script");
参数
名称 | 类型 | 说明 |
---|---|---|
searchPattern | String | 要搜索的正则表达式模式 |
replacement | String | 要用作替换文字的文本 |
返回
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 | 元素的属性。 |
返回
FootnoteSection
- 当前元素。
授权
使用此方法的脚本需要通过以下一个或多个范围进行授权:
-
https://www.googleapis.com/auth/documents.currentonly
-
https://www.googleapis.com/auth/documents
setText(text)
将内容设置为纯文本。
注意:系统会清除现有内容。
参数
名称 | 类型 | 说明 |
---|---|---|
text | String | 新的文字内容 |
返回
FootnoteSection
- 当前元素
授权
使用此方法的脚本需要通过以下一个或多个范围进行授权:
-
https://www.googleapis.com/auth/documents.currentonly
-
https://www.googleapis.com/auth/documents
setTextAlignment(textAlignment)
设置文本对齐方式。可用的对齐类型为 DocumentApp.TextAlignment.NORMAL
、DocumentApp.TextAlignment.SUBSCRIPT
和 DocumentApp.TextAlignment.SUPERSCRIPT
。
// Make the entire first paragraph be superscript. var text = DocumentApp.getActiveDocument().getBody().getParagraphs()[0].editAsText(); text.setTextAlignment(DocumentApp.TextAlignment.SUPERSCRIPT);
参数
名称 | 类型 | 说明 |
---|---|---|
textAlignment | TextAlignment | 要应用的文本对齐类型 |
返回
FootnoteSection
- 当前元素
授权
使用此方法的脚本需要通过以下一个或多个范围进行授权:
-
https://www.googleapis.com/auth/documents.currentonly
-
https://www.googleapis.com/auth/documents