表示段落的元素。Paragraph 可以包含 Equation、Footnote、Horizontal、Inline、Inline、Page 和 Text 元素。如需详细了解文档结构,请参阅扩展 Google 文档指南。
Paragraphs 不得包含换行符。换行符(“\n”)会转换为换行符(“\r”)。
const body = DocumentApp.getActiveDocument().getActiveTab().asDocumentTab().getBody(); // Append a tab header paragraph. const header = body.appendParagraph('A Document'); header.setHeading(DocumentApp.ParagraphHeading.HEADING1); // Append a section header paragraph. const section = body.appendParagraph('Section 1'); section.setHeading(DocumentApp.ParagraphHeading.HEADING2); // Append a regular paragraph. body.appendParagraph('This is a typical paragraph.');
方法
详细文档
add Positioned Image(image)
根据指定的图片 blob 创建并插入新的 Positioned。
// 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('123abc'); // Gets the body contents of the tab by its ID. // TODO(developer): Replace the ID with your own. const body = doc.getTab('123abc').asDocumentTab().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 tab, anchored to the first paragraph. paragraph.addPositionedImage(image);
参数
| 名称 | 类型 | 说明 |
|---|---|---|
image | Blob | 图片数据。 |
返回
Positioned - 新的定位图片。
授权
使用此方法的脚本需要获得以下一项或多项范围的授权:
-
https://www.googleapis.com/auth/documents.currentonly -
https://www.googleapis.com/auth/documents
append Horizontal Rule()
创建并附加新的 Horizontal。
// 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('123abc'); // Gets the body contents of the tab by its ID. // TODO(developer): Replace the ID with your own. const body = doc.getTab('123abc').asDocumentTab().getBody(); // Gets the first paragraph from the body. const paragraph = body.getParagraphs()[0]; // Adds a horizontal line under the first paragraph. paragraph.appendHorizontalRule();
返回
Horizontal - 新的水平线。
授权
使用此方法的脚本需要获得以下一项或多项范围的授权:
-
https://www.googleapis.com/auth/documents.currentonly -
https://www.googleapis.com/auth/documents
append Inline Image(image)
根据指定的图片 blob 创建并附加新的 Inline。
// 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('123abc'); // Gets the body contents of the tab by its ID. // TODO(developer): Replace the ID with your own. const body = doc.getTab('123abc').asDocumentTab().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 | Blob | 图片数据。 |
返回
Inline - 附加的图片。
授权
使用此方法的脚本需要获得以下一项或多项范围的授权:
-
https://www.googleapis.com/auth/documents.currentonly -
https://www.googleapis.com/auth/documents
append Inline Image(image)
附加给定的 Inline。
// 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('123abc'); // Gets the body contents of the tab by its ID. // TODO(developer): Replace the ID with your own. const body = doc.getTab('123abc').asDocumentTab().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 | Inline | 图片数据。 |
返回
Inline - 附加的图片。
授权
使用此方法的脚本需要获得以下一项或多项范围的授权:
-
https://www.googleapis.com/auth/documents.currentonly -
https://www.googleapis.com/auth/documents
append Page Break()
创建并附加新的 Page。
注意:Page 可能不包含在 Table 中。
如果当前元素包含在表格单元格中,则会抛出异常。
// 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('123abc'); // Gets the body contents of the tab by its ID. // TODO(developer): Replace the ID with your own. const body = doc.getTab('123abc').asDocumentTab().getBody(); // Gets the first paragraph from the body. const paragraph = body.getParagraphs()[0]; // Adds a page break after the first paragraph. paragraph.appendPageBreak();
返回
Page - 新的分页符元素。
授权
使用此方法的脚本需要获得以下一项或多项范围的授权:
-
https://www.googleapis.com/auth/documents.currentonly -
https://www.googleapis.com/auth/documents
append Page Break(pageBreak)
附加给定的 Page。
注意:Page 不能包含在 Table 中。
如果当前元素位于表格单元格中,脚本会抛出异常。
// 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('123abc'); // Gets the body contents of the tab by its ID. // TODO(developer): Replace the ID with your own. const body = doc.getTab('123abc').asDocumentTab().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);
参数
| 名称 | 类型 | 说明 |
|---|---|---|
page | Page | 要附加的分页符。 |
返回
Page - 附加的分页符元素。
授权
使用此方法的脚本需要获得以下一项或多项范围的授权:
-
https://www.googleapis.com/auth/documents.currentonly -
https://www.googleapis.com/auth/documents
append Text(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('123abc'); // Gets the body contents of the tab by its ID. // TODO(developer): Replace the ID with your own. const body = doc.getTab('123abc').asDocumentTab().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
append Text(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('123abc'); // Gets the body contents of the tab by its ID. // TODO(developer): Replace the ID with your own. const body = doc.getTab('123abc').asDocumentTab().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()
edit As Text()
获取当前元素的 Text 版本以进行修改。
使用 edit 将元素内容作为富文本进行操作。edit 模式会忽略非文本元素(例如 Inline 和 Horizontal)。
完全包含在已删除文本范围内的子元素会从相应元素中移除。
const body = DocumentApp.getActiveDocument().getActiveTab().asDocumentTab().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 - 当前元素的文本版本
find Element(elementType)
在元素的内容中搜索指定类型的后代。
参数
| 名称 | 类型 | 说明 |
|---|---|---|
element | Element | 要搜索的元素类型。 |
返回
Range - 一种搜索结果,用于指示搜索元素的位置。
授权
使用此方法的脚本需要获得以下一项或多项范围的授权:
-
https://www.googleapis.com/auth/documents.currentonly -
https://www.googleapis.com/auth/documents
find Element(elementType, from)
从指定的 Range 开始,在元素的内容中搜索指定类型的后代。
const body = DocumentApp.getActiveDocument().getActiveTab().asDocumentTab().getBody(); // Define the search parameters. let searchResult = null; // Search until the paragraph is found. while ( (searchResult = body.findElement( DocumentApp.ElementType.PARAGRAPH, searchResult, ))) { const par = searchResult.getElement().asParagraph(); if (par.getHeading() === DocumentApp.ParagraphHeading.HEADING1) { // Found one, update and stop. par.setText('This is the first header.'); break; } }
参数
| 名称 | 类型 | 说明 |
|---|---|---|
element | Element | 要搜索的元素类型。 |
from | Range | 要从中搜索的搜索结果。 |
返回
Range - 一种搜索结果,用于指示搜索元素的下一个位置。
授权
使用此方法的脚本需要获得以下一项或多项范围的授权:
-
https://www.googleapis.com/auth/documents.currentonly -
https://www.googleapis.com/auth/documents
find Text(searchPattern)
使用正则表达式在元素的内容中搜索指定的文本模式。
部分 JavaScript 正则表达式功能不受完全支持,例如捕获组和模式修饰符。
系统会针对当前元素中包含的每个文本块独立匹配所提供的正则表达式模式。
参数
| 名称 | 类型 | 说明 |
|---|---|---|
search | String | 要搜索的模式 |
返回
Range - 指示搜索文本位置的搜索结果,如果没有匹配项,则为 null
授权
使用此方法的脚本需要获得以下一项或多项范围的授权:
-
https://www.googleapis.com/auth/documents.currentonly -
https://www.googleapis.com/auth/documents
find Text(searchPattern, from)
从给定的搜索结果开始,在元素的内容中搜索指定的文本模式。
部分 JavaScript 正则表达式功能不受完全支持,例如捕获组和模式修饰符。
系统会针对当前元素中包含的每个文本块独立匹配所提供的正则表达式模式。
参数
| 名称 | 类型 | 说明 |
|---|---|---|
search | String | 要搜索的模式 |
from | Range | 要开始搜索的搜索结果 |
返回
Range - 指示搜索文本的下一个位置的搜索结果,如果没有匹配项,则为 null
授权
使用此方法的脚本需要获得以下一项或多项范围的授权:
-
https://www.googleapis.com/auth/documents.currentonly -
https://www.googleapis.com/auth/documents
get Alignment()
// 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('123abc'); // Gets the body contents of the tab by its ID. // TODO(developer): Replace the ID with your own. const body = doc.getTab('123abc').asDocumentTab().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());
返回
Horizontal - 对齐。
授权
使用此方法的脚本需要获得以下一项或多项范围的授权:
-
https://www.googleapis.com/auth/documents.currentonly -
https://www.googleapis.com/auth/documents
get Attributes()
检索元素的属性。
结果是一个对象,其中包含每个有效元素属性的属性,每个属性名称都对应于 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 Child(childIndex)
检索指定子索引处的子元素。
const body = DocumentApp.getActiveDocument().getActiveTab().asDocumentTab().getBody(); // Obtain the first element in the tab. const 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.'); }
参数
| 名称 | 类型 | 说明 |
|---|---|---|
child | Integer | 要检索的子元素的索引。 |
返回
Element - 指定索引处的子元素。
授权
使用此方法的脚本需要获得以下一项或多项范围的授权:
-
https://www.googleapis.com/auth/documents.currentonly -
https://www.googleapis.com/auth/documents
get Child Index(child)
get Heading()
检索 Paragraph。
// 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('123abc'); // Gets the body contents of the tab by its ID. // TODO(developer): Replace the ID with your own. const body = doc.getTab('123abc').asDocumentTab().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());
返回
Paragraph - 标题。
授权
使用此方法的脚本需要获得以下一项或多项范围的授权:
-
https://www.googleapis.com/auth/documents.currentonly -
https://www.googleapis.com/auth/documents
get Indent End()
检索结束缩进(以磅为单位)。
返回
Number|null - 结束缩进,以磅为单位
授权
使用此方法的脚本需要获得以下一项或多项范围的授权:
-
https://www.googleapis.com/auth/documents.currentonly -
https://www.googleapis.com/auth/documents
get Indent First Line()
检索第一行缩进(以磅为单位)。
返回
Number|null - 首行缩进,以点为单位
授权
使用此方法的脚本需要获得以下一项或多项范围的授权:
-
https://www.googleapis.com/auth/documents.currentonly -
https://www.googleapis.com/auth/documents
get Indent Start()
检索起始缩进。
返回
Number|null - 起始缩进
授权
使用此方法的脚本需要获得以下一项或多项范围的授权:
-
https://www.googleapis.com/auth/documents.currentonly -
https://www.googleapis.com/auth/documents
get Line Spacing()
检索行间距(以磅为单位)。
返回
Number|null - 行间距(以磅为单位)
授权
使用此方法的脚本需要获得以下一项或多项范围的授权:
-
https://www.googleapis.com/auth/documents.currentonly -
https://www.googleapis.com/auth/documents
get Link Url()
检索链接网址。
返回
String|null - 链接网址,如果元素包含此属性的多个值,则为 null
授权
使用此方法的脚本需要获得以下一项或多项范围的授权:
-
https://www.googleapis.com/auth/documents.currentonly -
https://www.googleapis.com/auth/documents
get Next Sibling()
get Num Children()
检索子项的数量。
const body = DocumentApp.getActiveDocument().getActiveTab().asDocumentTab().getBody(); // Log the number of elements in the tab. Logger.log(`There are ${body.getNumChildren()} elements in the tab's body.`);
返回
Integer - 子项数量。
授权
使用此方法的脚本需要获得以下一项或多项范围的授权:
-
https://www.googleapis.com/auth/documents.currentonly -
https://www.googleapis.com/auth/documents
get Parent()
检索元素的父元素。
父元素包含当前元素。
返回
Container - 父元素。
授权
使用此方法的脚本需要获得以下一项或多项范围的授权:
-
https://www.googleapis.com/auth/documents.currentonly -
https://www.googleapis.com/auth/documents
get Positioned Image(id)
根据图片的 ID 获取 Positioned。
参数
| 名称 | 类型 | 说明 |
|---|---|---|
id | String | 图片 ID |
返回
Positioned - 定位后的图片
授权
使用此方法的脚本需要获得以下一项或多项范围的授权:
-
https://www.googleapis.com/auth/documents.currentonly -
https://www.googleapis.com/auth/documents
get Positioned Images()
获取锚定到段落的所有 Positioned 对象。
返回
Positioned - 一个包含已定位图片的列表
授权
使用此方法的脚本需要获得以下一项或多项范围的授权:
-
https://www.googleapis.com/auth/documents.currentonly -
https://www.googleapis.com/auth/documents
get Previous Sibling()
get Spacing After()
检索元素后的间距(以磅为单位)。
返回
Number|null - 元素后的间距(以磅为单位)
授权
使用此方法的脚本需要获得以下一项或多项范围的授权:
-
https://www.googleapis.com/auth/documents.currentonly -
https://www.googleapis.com/auth/documents
get Spacing Before()
检索元素之前的间距(以磅为单位)。
返回
Number|null - 元素之前的间距(以点为单位)
授权
使用此方法的脚本需要获得以下一项或多项范围的授权:
-
https://www.googleapis.com/auth/documents.currentonly -
https://www.googleapis.com/auth/documents
get Text()
以文本字符串形式检索元素的内容。
返回
String - 元素的内容(以文本字符串形式表示)
授权
使用此方法的脚本需要获得以下一项或多项范围的授权:
-
https://www.googleapis.com/auth/documents.currentonly -
https://www.googleapis.com/auth/documents
get Text Alignment()
获取文本对齐方式。可用的对齐类型包括 Document、Document 和 Document。
返回
Text - 文本对齐类型,如果文本包含多种文本对齐类型或从未设置过文本对齐方式,则为 null
授权
使用此方法的脚本需要获得以下一项或多项范围的授权:
-
https://www.googleapis.com/auth/documents.currentonly -
https://www.googleapis.com/auth/documents
get Type()
检索元素的 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
insert Horizontal Rule(childIndex)
在指定索引处创建并插入 Horizontal。
参数
| 名称 | 类型 | 说明 |
|---|---|---|
child | Integer | 要插入元素的索引 |
返回
Horizontal - 新的水平分隔线元素
授权
使用此方法的脚本需要获得以下一项或多项范围的授权:
-
https://www.googleapis.com/auth/documents.currentonly -
https://www.googleapis.com/auth/documents
insert Inline Image(childIndex, image)
在指定索引处,根据指定的图片 blob 创建并插入新的 Inline。
参数
| 名称 | 类型 | 说明 |
|---|---|---|
child | Integer | 要插入元素的索引 |
image | Blob | 图片数据 |
返回
Inline - 插入的内嵌图片元素
授权
使用此方法的脚本需要获得以下一项或多项范围的授权:
-
https://www.googleapis.com/auth/documents.currentonly -
https://www.googleapis.com/auth/documents
insert Inline Image(childIndex, image)
在指定索引处插入给定的 Inline。
参数
| 名称 | 类型 | 说明 |
|---|---|---|
child | Integer | 要插入元素的索引 |
image | Inline | 图片数据 |
返回
Inline - 插入的内嵌图片元素
授权
使用此方法的脚本需要获得以下一项或多项范围的授权:
-
https://www.googleapis.com/auth/documents.currentonly -
https://www.googleapis.com/auth/documents
insert Page Break(childIndex)
在指定索引处创建并插入新的 Page。
注意:Page 可能不包含在 Table 中。
如果当前元素包含在表格单元格中,则会抛出异常。
参数
| 名称 | 类型 | 说明 |
|---|---|---|
child | Integer | 要插入元素的索引 |
返回
Page - 新的分页符元素
授权
使用此方法的脚本需要获得以下一项或多项范围的授权:
-
https://www.googleapis.com/auth/documents.currentonly -
https://www.googleapis.com/auth/documents
insert Page Break(childIndex, pageBreak)
在指定索引处插入给定的 Page。
注意:Page 可能不包含在 Table 中。
如果当前元素包含在表格单元格中,则会抛出异常。
参数
| 名称 | 类型 | 说明 |
|---|---|---|
child | Integer | 要插入元素的索引 |
page | Page | 要插入的分页符 |
返回
Page - 插入的分页符元素
授权
使用此方法的脚本需要获得以下一项或多项范围的授权:
-
https://www.googleapis.com/auth/documents.currentonly -
https://www.googleapis.com/auth/documents
insert Text(childIndex, text)
insert Text(childIndex, text)
is At Document End()
is Left To Right()
检索从左到右的设置。
返回
Boolean|null - 从左到右设置
授权
使用此方法的脚本需要获得以下一项或多项范围的授权:
-
https://www.googleapis.com/auth/documents.currentonly -
https://www.googleapis.com/auth/documents
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();
返回
Paragraph|null - 合并后的元素。
授权
使用此方法的脚本需要获得以下一项或多项范围的授权:
-
https://www.googleapis.com/auth/documents.currentonly -
https://www.googleapis.com/auth/documents
remove Child(child)
remove From Parent()
从父级中移除元素。
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(); }
返回
Paragraph|null - 移除的元素。
授权
使用此方法的脚本需要获得以下一项或多项范围的授权:
-
https://www.googleapis.com/auth/documents.currentonly -
https://www.googleapis.com/auth/documents
remove Positioned Image(id)
按图片的 ID 移除 Positioned。
参数
| 名称 | 类型 | 说明 |
|---|---|---|
id | String | 图片 ID |
返回
Boolean - 指定的图片是否已移除
授权
使用此方法的脚本需要获得以下一项或多项范围的授权:
-
https://www.googleapis.com/auth/documents.currentonly -
https://www.googleapis.com/auth/documents
replace Text(searchPattern, replacement)
使用正则表达式将指定文本模式的所有匹配项替换为指定的替换字符串。
搜索模式以字符串形式传递,而不是以 JavaScript 正则表达式对象形式传递。 因此,您需要对模式中的所有反斜杠进行转义。
此方法使用 Google 的 RE2 正则表达式库,该库限制了支持的语法。
系统会根据所提供的正则表达式模式,单独匹配当前元素中包含的每个文本块。
const body = DocumentApp.getActiveDocument().getActiveTab().asDocumentTab().getBody(); // Clear the text surrounding "Apps Script", with or without text. body.replaceText('^.*Apps ?Script.*$', 'Apps Script');
参数
| 名称 | 类型 | 说明 |
|---|---|---|
search | String | 要搜索的正则表达式模式 |
replacement | String | 用作替换项的文本 |
返回
Element - 当前元素
授权
使用此方法的脚本需要获得以下一项或多项范围的授权:
-
https://www.googleapis.com/auth/documents.currentonly -
https://www.googleapis.com/auth/documents
set Alignment(alignment)
参数
| 名称 | 类型 | 说明 |
|---|---|---|
alignment | Horizontal | 水平对齐方式 |
返回
Paragraph - 当前元素
授权
使用此方法的脚本需要获得以下一项或多项范围的授权:
-
https://www.googleapis.com/auth/documents.currentonly -
https://www.googleapis.com/auth/documents
set Attributes(attributes)
设置元素的属性。
指定的 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 | 元素的属性。 |
返回
Paragraph - 当前元素。
授权
使用此方法的脚本需要获得以下一项或多项范围的授权:
-
https://www.googleapis.com/auth/documents.currentonly -
https://www.googleapis.com/auth/documents
set Heading(heading)
设置 Paragraph。
参数
| 名称 | 类型 | 说明 |
|---|---|---|
heading | Paragraph | 标题 |
返回
Paragraph - 当前元素
授权
使用此方法的脚本需要获得以下一项或多项范围的授权:
-
https://www.googleapis.com/auth/documents.currentonly -
https://www.googleapis.com/auth/documents
set Indent End(indentEnd)
set Indent First Line(indentFirstLine)
set Indent Start(indentStart)
set Left To Right(leftToRight)
set Line Spacing(multiplier)
set Link Url(url)
set Spacing After(spacingAfter)
set Spacing Before(spacingBefore)
set Text(text)
将段落的内容设置为文本。
注意:系统会清除现有内容。
参数
| 名称 | 类型 | 说明 |
|---|---|---|
text | String | 新文本内容 |
授权
使用此方法的脚本需要获得以下一项或多项范围的授权:
-
https://www.googleapis.com/auth/documents.currentonly -
https://www.googleapis.com/auth/documents
set Text Alignment(textAlignment)
设置文本对齐方式。可用的对齐类型包括 Document、Document 和 Document。
// Make the entire first paragraph in the active tab be superscript. const documentTab = DocumentApp.getActiveDocument().getActiveTab().asDocumentTab(); const text = documentTab.getBody().getParagraphs()[0].editAsText(); text.setTextAlignment(DocumentApp.TextAlignment.SUPERSCRIPT);
参数
| 名称 | 类型 | 说明 |
|---|---|---|
text | Text | 要应用的文字对齐类型 |
返回
Paragraph - 当前元素
授权
使用此方法的脚本需要获得以下一项或多项范围的授权:
-
https://www.googleapis.com/auth/documents.currentonly -
https://www.googleapis.com/auth/documents