表示段落的元素。Paragraph
可以包含 Equation
、Footnote
、HorizontalRule
、InlineDrawing
、InlineImage
、PageBreak
和 Text
元素。如需详细了解文档结构,请参阅扩展 Google 文档的指南。
“Paragraphs
”不能包含换行符。换行符 ("\n") 会转换为换行符 ("了解")。
var body = DocumentApp.getActiveDocument().getBody(); // Append a document header paragraph. var header = body.appendParagraph("A Document"); header.setHeading(DocumentApp.ParagraphHeading.HEADING1); // Append a section header paragraph. var section = body.appendParagraph("Section 1"); section.setHeading(DocumentApp.ParagraphHeading.HEADING2); // Append a regular paragraph. body.appendParagraph("This is a typical paragraph.");
方法
详细文档
addPositionedImage(image)
根据指定的图片 blob 创建并插入新的 PositionedImage
。
// Opens the Docs file by its ID. If you created your script from within // a Google Docs file, you can use DocumentApp.getActiveDocument() instead. // TODO(developer): Replace the ID with your own. const doc = DocumentApp.openById('abc123456'); // Gets the document body. const body = doc.getBody(); // Gets the first paragraph from the body. const paragraph = body.getParagraphs()[0]; // Fetches the specified image URL. const image = UrlFetchApp.fetch('https://fonts.gstatic.com/s/i/productlogos/apps_script/v10/web-24dp/logo_apps_script_color_1x_web_24dp.png'); // Adds the image to the document, anchored to the first paragraph. paragraph.addPositionedImage(image);
参数
名称 | 类型 | 说明 |
---|---|---|
image | BlobSource | 图片数据。 |
返程
PositionedImage
- 新定位的图片。
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
https://www.googleapis.com/auth/documents.currentonly
-
https://www.googleapis.com/auth/documents
appendHorizontalRule()
创建并附加新的 HorizontalRule
。
// Opens the Docs file by its ID. If you created your script from within // a Google Docs file, you can use DocumentApp.getActiveDocument() instead. // TODO(developer): Replace the ID with your own. const doc = DocumentApp.openById('abc123456'); // Gets the document body. const body = doc.getBody(); // Gets the first paragraph from the body. const paragraph = body.getParagraphs()[0]; // Adds a horizontal line under the first paragraph. paragraph.appendHorizontalRule();
返程
HorizontalRule
- 新的水平规则。
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
https://www.googleapis.com/auth/documents.currentonly
-
https://www.googleapis.com/auth/documents
appendInlineImage(image)
根据指定的图片 blob 创建并附加新的 InlineImage
。
// Opens the Docs file by its ID. If you created your script from within // a Google Docs file, you can use DocumentApp.getActiveDocument() instead. // TODO(developer): Replace the ID with your own. const doc = DocumentApp.openById('abc123456'); // Gets the document body. const body = doc.getBody(); // Gets the first paragraph from the body. const paragraph = body.getParagraphs()[0]; // Fetches the image from the specified image URL. const image = UrlFetchApp .fetch('https://fonts.gstatic.com/s/i/productlogos/apps_script/v10/web-96dp/logo_apps_script_color_1x_web_96dp.png'); // Adds the image to the first paragraph. paragraph.appendInlineImage(image);
参数
名称 | 类型 | 说明 |
---|---|---|
image | BlobSource | 图片数据。 |
返程
InlineImage
- 附加的图片。
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
https://www.googleapis.com/auth/documents.currentonly
-
https://www.googleapis.com/auth/documents
appendInlineImage(image)
附加给定的 InlineImage
。
// Opens the Docs file by its ID. If you created your script from within // a Google Docs file, you can use DocumentApp.getActiveDocument() instead. // TODO(developer): Replace the ID with your own. const doc = DocumentApp.openById('abc123456'); // Gets the document body. const body = doc.getBody(); // Gets the first paragraph from the body. const paragraph = body.getParagraphs()[0]; // Makes a copy of the first image in the body. const image = body.getImages()[0].copy();; // Adds the image to the first paragraph. paragraph.appendInlineImage(image);
参数
名称 | 类型 | 说明 |
---|---|---|
image | InlineImage | 图片数据。 |
返程
InlineImage
- 附加的图片。
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
https://www.googleapis.com/auth/documents.currentonly
-
https://www.googleapis.com/auth/documents
appendPageBreak()
创建并附加新的 PageBreak
。
注意:TableCells
中不能包含 PageBreaks
。如果当前元素包含在表格单元格中,则会抛出异常。
// Opens the Docs file by its ID. If you created your script from within // a Google Docs file, you can use DocumentApp.getActiveDocument() instead. // TODO(developer): Replace the ID with your own. const doc = DocumentApp.openById('abc123456'); // Gets the document body. const body = doc.getBody(); // Gets the first paragraph from the body. const paragraph = body.getParagraphs()[0]; // Adds a page break after the first paragraph. paragraph.appendPageBreak();
返程
PageBreak
- 新的分页符。
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
https://www.googleapis.com/auth/documents.currentonly
-
https://www.googleapis.com/auth/documents
appendPageBreak(pageBreak)
附加给定的 PageBreak
。
注意:TableCells
不能包含 PageBreaks
。如果表单元格中存在当前元素,该脚本会抛出异常。
// Opens the Docs file by its ID. If you created your script from within // a Google Docs file, you can use DocumentApp.getActiveDocument() instead. // TODO(developer): Replace the ID with your own. const doc = DocumentApp.openById('abc123456'); // Gets the document body. const body = doc.getBody(); // Gets the first paragraph from the body. const paragraph = body.getParagraphs()[0]; // Adds a page break after the first paragraph. const pageBreak = paragraph.appendPageBreak(); // Makes a copy of the page break. const newPageBreak = pageBreak.copy(); // Adds the copied page break to the paragraph. paragraph.appendPageBreak(newPageBreak);
参数
名称 | 类型 | 说明 |
---|---|---|
pageBreak | PageBreak | 要附加的分页符。 |
返程
PageBreak
- 附加的分页符。
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
https://www.googleapis.com/auth/documents.currentonly
-
https://www.googleapis.com/auth/documents
appendText(text)
创建并附加具有指定内容的新 Text
元素。
// Opens the Docs file by its ID. If you created your script from within // a Google Docs file, you can use DocumentApp.getActiveDocument() instead. // TODO(developer): Replace the ID with your own. const doc = DocumentApp.openById('abc123456'); // Gets the document body. const body = doc.getBody(); // Gets the first paragraph from the body. const paragraph = body.getParagraphs()[0]; // Adds a string to the paragraph. paragraph.appendText('This is a new sentence.');
参数
名称 | 类型 | 说明 |
---|---|---|
text | String | 文本内容。 |
返程
Text
- 新的文本元素。
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
https://www.googleapis.com/auth/documents.currentonly
-
https://www.googleapis.com/auth/documents
appendText(text)
附加给定的 Text
元素。
// Opens the Docs file by its ID. If you created your script from within // a Google Docs file, you can use DocumentApp.getActiveDocument() instead. // TODO(developer): Replace the ID with your own. const doc = DocumentApp.openById('abc123456'); // Gets the document body. const body = doc.getBody(); // Gets the text from the first paragraph in the body. const paragraph1 = body.getParagraphs()[0]; const text = paragraph1.getText(); // Gets the third paragraph in the body. const paragraph3 = body.getParagraphs()[2]; // Adds the text from the first paragraph to the third paragraph. paragraph3.appendText(text);
参数
名称 | 类型 | 说明 |
---|---|---|
text | Text | 要附加的文本元素。 |
返程
Text
- 附加的文本元素。
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
https://www.googleapis.com/auth/documents.currentonly
-
https://www.googleapis.com/auth/documents
clear()
copy()
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
getAlignment()
// Opens the Docs file by its ID. If you created your script from within // a Google Docs file, you can use DocumentApp.getActiveDocument() instead. // TODO(developer): Replace the ID with your own. const doc = DocumentApp.openById('abc123456'); // Gets the document body. const body = doc.getBody(); // Gets the first paragraph from the body. const paragraph = body.getParagraphs()[0]; // Sets the horizontal alignment to left for the first paragraph. paragraph.setAlignment(DocumentApp.HorizontalAlignment.LEFT); // Gets the horizontal alignment of the first paragraph and logs it to the console. console.log(paragraph.getAlignment().toString());
返程
HorizontalAlignment
- 对齐方式。
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
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)
getHeading()
检索 ParagraphHeading
。
// Opens the Docs file by its ID. If you created your script from within // a Google Docs file, you can use DocumentApp.getActiveDocument() instead. // TODO(developer): Replace the ID with your own. const doc = DocumentApp.openById('abc123456'); // Gets the document body. const body = doc.getBody(); // Adds a paragraph to the body. const paragraph = body.appendParagraph('Title heading'); // Sets the paragraph heading style to 'Title.' paragraph.setHeading(DocumentApp.ParagraphHeading.TITLE); // Gets the heading style and logs it to the console. console.log(paragraph.getHeading().toString());
返程
ParagraphHeading
- 标题。
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
https://www.googleapis.com/auth/documents.currentonly
-
https://www.googleapis.com/auth/documents
getIndentEnd()
检索结束缩进(以点为单位)。
返程
Number
- 末尾缩进(以点为单位)
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
https://www.googleapis.com/auth/documents.currentonly
-
https://www.googleapis.com/auth/documents
getIndentFirstLine()
检索首行缩进,以点为单位。
返程
Number
- 首行缩进,以点为单位
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
https://www.googleapis.com/auth/documents.currentonly
-
https://www.googleapis.com/auth/documents
getIndentStart()
检索起始缩进。
返程
Number
- 开头缩进
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
https://www.googleapis.com/auth/documents.currentonly
-
https://www.googleapis.com/auth/documents
getLineSpacing()
检索行间距(以点为单位)。
返程
Number
- 行间距(以点为单位)
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
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()
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
getParent()
检索元素的父元素。
父元素包含当前元素。
返程
ContainerElement
- 父元素。
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
https://www.googleapis.com/auth/documents.currentonly
-
https://www.googleapis.com/auth/documents
getPositionedImage(id)
根据图片的 ID 获取 PositionedImage
。
参数
名称 | 类型 | 说明 |
---|---|---|
id | String | 图片 ID |
返程
PositionedImage
- 定位的图片
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
https://www.googleapis.com/auth/documents.currentonly
-
https://www.googleapis.com/auth/documents
getPositionedImages()
获取锚定在段落中的所有 PositionedImage
对象。
返程
PositionedImage[]
- 已定位图像的列表
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
https://www.googleapis.com/auth/documents.currentonly
-
https://www.googleapis.com/auth/documents
getPreviousSibling()
getSpacingAfter()
检索元素后的间距,以点为单位。
返程
Number
- 元素后面的间距(以点为单位)
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
https://www.googleapis.com/auth/documents.currentonly
-
https://www.googleapis.com/auth/documents
getSpacingBefore()
检索元素之前的间距(以点为单位)。
返程
Number
- 元素之前的间距(以点为单位)
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
https://www.googleapis.com/auth/documents.currentonly
-
https://www.googleapis.com/auth/documents
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
insertHorizontalRule(childIndex)
在指定索引处创建并插入 HorizontalRule
。
参数
名称 | 类型 | 说明 |
---|---|---|
childIndex | Integer | 要插入元素的索引 |
返程
HorizontalRule
- 新的水平规则元素
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
https://www.googleapis.com/auth/documents.currentonly
-
https://www.googleapis.com/auth/documents
insertInlineImage(childIndex, image)
根据指定的图片 blob 的指定索引创建并插入新的 InlineImage
。
参数
名称 | 类型 | 说明 |
---|---|---|
childIndex | Integer | 要插入元素的索引 |
image | BlobSource | 图片数据 |
返程
InlineImage
- 插入的内嵌图片元素
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
https://www.googleapis.com/auth/documents.currentonly
-
https://www.googleapis.com/auth/documents
insertInlineImage(childIndex, image)
在指定索引处插入指定的 InlineImage
。
参数
名称 | 类型 | 说明 |
---|---|---|
childIndex | Integer | 要插入元素的索引 |
image | InlineImage | 图片数据 |
返程
InlineImage
- 插入的内嵌图片元素
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
https://www.googleapis.com/auth/documents.currentonly
-
https://www.googleapis.com/auth/documents
insertPageBreak(childIndex)
在指定索引处创建并插入新的 PageBreak
。
注意:TableCells
中不能包含 PageBreaks
。如果当前元素包含在表格单元格中,则会抛出异常。
参数
名称 | 类型 | 说明 |
---|---|---|
childIndex | Integer | 要插入元素的索引 |
返程
PageBreak
- 新的分页符
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
https://www.googleapis.com/auth/documents.currentonly
-
https://www.googleapis.com/auth/documents
insertPageBreak(childIndex, pageBreak)
在指定索引处插入指定的 PageBreak
。
注意:TableCells
中不能包含 PageBreaks
。如果当前元素包含在表格单元格中,则会抛出异常。
参数
名称 | 类型 | 说明 |
---|---|---|
childIndex | Integer | 要插入元素的索引 |
pageBreak | PageBreak | 插入 [分页符] |
返程
PageBreak
- 插入的分页符
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
https://www.googleapis.com/auth/documents.currentonly
-
https://www.googleapis.com/auth/documents
insertText(childIndex, text)
insertText(childIndex, text)
isAtDocumentEnd()
isLeftToRight()
检索从左到右的设置。
返程
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();
返程
Paragraph
- 合并的元素。
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
https://www.googleapis.com/auth/documents.currentonly
-
https://www.googleapis.com/auth/documents
removeChild(child)
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(); }
返程
Paragraph
- 已移除的元素。
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
https://www.googleapis.com/auth/documents.currentonly
-
https://www.googleapis.com/auth/documents
removePositionedImage(id)
根据图片的 ID 移除 PositionedImage
。
参数
名称 | 类型 | 说明 |
---|---|---|
id | String | 图片 ID |
返程
Boolean
- 指定的图片是否被移除
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
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
setAlignment(alignment)
参数
名称 | 类型 | 说明 |
---|---|---|
alignment | HorizontalAlignment | 水平对齐 |
返程
Paragraph
- 当前元素
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
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 | 元素的属性。 |
返程
Paragraph
- 当前元素。
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
https://www.googleapis.com/auth/documents.currentonly
-
https://www.googleapis.com/auth/documents
setHeading(heading)
设置 ParagraphHeading
。
参数
名称 | 类型 | 说明 |
---|---|---|
heading | ParagraphHeading | 标题 |
返程
Paragraph
- 当前元素
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
https://www.googleapis.com/auth/documents.currentonly
-
https://www.googleapis.com/auth/documents
setIndentEnd(indentEnd)
setIndentFirstLine(indentFirstLine)
setIndentStart(indentStart)
setLeftToRight(leftToRight)
setLineSpacing(multiplier)
setLinkUrl(url)
setSpacingAfter(spacingAfter)
setSpacingBefore(spacingBefore)
setText(text)
将段落内容设置为文本。
注意:现有内容已清除。
参数
名称 | 类型 | 说明 |
---|---|---|
text | String | 新的文本内容 |
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
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 | 要应用的文本对齐类型 |
返程
Paragraph
- 当前元素
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
https://www.googleapis.com/auth/documents.currentonly
-
https://www.googleapis.com/auth/documents