Class Paragraph

段落

表示段落的元素。Paragraph 可能包含 EquationFootnoteHorizontalRuleInlineDrawingInlineImagePageBreakText 元素。有关文档结构的详细信息,请参阅扩展 Google 文档指南

Paragraphs不得包含换行符。换行符(“\n”) 已转换为换行符(“\r”)。

var body = DocumentApp.getActiveDocument().getActiveTab().asDocumentTab().getBody();

// Append a tab 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)PositionedImage根据指定的图片 blob 创建并插入新的 PositionedImage
appendHorizontalRule()HorizontalRule创建并附加新的 HorizontalRule
appendInlineImage(image)InlineImage根据指定的图片 blob 创建并附加新的 InlineImage
appendInlineImage(image)InlineImage附加指定的 InlineImage
appendPageBreak()PageBreak创建并附加新的 PageBreak
appendPageBreak(pageBreak)PageBreak附加指定的 PageBreak
appendText(text)Text创建带有指定内容的新 Text 元素并向其附加该元素。
appendText(text)Text附加指定的 Text 元素。
clear()Paragraph清除元素的内容。
copy()Paragraph返回当前元素的分离的深层副本。
editAsText()Text获取当前元素的 Text 版本,以用于修改。
findElement(elementType)RangeElement搜索指定类型的后代元素的内容。
findElement(elementType, from)RangeElement搜索指定类型的后代元素的内容,从 指定的 RangeElement
findText(searchPattern)RangeElement使用正则表达式搜索指定文本格式的元素内容。
findText(searchPattern, from)RangeElement搜索指定文本格式的元素内容,从指定的 搜索结果。
getAlignment()HorizontalAlignment检索 HorizontalAlignment
getAttributes()Object检索元素的属性。
getChild(childIndex)Element检索指定子索引处的子元素。
getChildIndex(child)Integer检索指定子元素的子索引。
getHeading()ParagraphHeading检索 ParagraphHeading
getIndentEnd()Number检索末尾缩进(以点为单位)。
getIndentFirstLine()Number检索首行缩进(以点为单位)。
getIndentStart()Number检索起始缩进。
getLineSpacing()Number检索行间距(以点为单位)。
getLinkUrl()String检索链接网址。
getNextSibling()Element检索该元素的下一个同级元素。
getNumChildren()Integer检索子元素的数量。
getParent()ContainerElement获取该元素的父元素。
getPositionedImage(id)PositionedImage根据图片的 ID 获取 PositionedImage
getPositionedImages()PositionedImage[]获取锚定到段落的所有 PositionedImage 对象。
getPreviousSibling()Element检索该元素的前一个同级元素。
getSpacingAfter()Number检索元素后的间距(以点为单位)。
getSpacingBefore()Number检索元素前的间距,以点为单位。
getText()String检索文本字符串形式的元素内容。
getTextAlignment()TextAlignment获取文本对齐方式。
getType()ElementType检索元素的 ElementType
insertHorizontalRule(childIndex)HorizontalRule在指定索引处创建并插入 HorizontalRule
insertInlineImage(childIndex, image)InlineImage在指定的图片 blob 处创建并插入新的 InlineImage。 索引中。
insertInlineImage(childIndex, image)InlineImage在指定索引处插入给定 InlineImage
insertPageBreak(childIndex)PageBreak在指定索引处创建并插入新的 PageBreak
insertPageBreak(childIndex, pageBreak)PageBreak在指定索引处插入给定 PageBreak
insertText(childIndex, text)Text在指定索引处创建并插入新的文本元素。
insertText(childIndex, text)Text使用指定文本在指定索引处插入指定的 Text 元素 内容。
isAtDocumentEnd()Boolean确定元素是否在 Document 的末尾。
isLeftToRight()Boolean检索从左到右的设置。
merge()Paragraph将元素与前一个相同类型的同级元素合并。
removeChild(child)Paragraph删除指定的子元素。
removeFromParent()Paragraph将元素从其父元素中移除。
removePositionedImage(id)Boolean根据映像的 ID 移除 PositionedImage
replaceText(searchPattern, replacement)Element使用常规 表达式。
setAlignment(alignment)Paragraph设置 HorizontalAlignment
setAttributes(attributes)Paragraph设置元素的属性。
setHeading(heading)Paragraph设置 ParagraphHeading
setIndentEnd(indentEnd)Paragraph设置末尾缩进(以点为单位)。
setIndentFirstLine(indentFirstLine)Paragraph设置首行缩进(以点为单位)。
setIndentStart(indentStart)Paragraph设置起始缩进(以点为单位)。
setLeftToRight(leftToRight)Paragraph设置从左到右的设置。
setLineSpacing(multiplier)Paragraph设置行间距,即一个数量,用于表示要用于间距的行数。
setLinkUrl(url)Paragraph设置链接网址。
setSpacingAfter(spacingAfter)Paragraph设置元素后的间距(以点为单位)。
setSpacingBefore(spacingBefore)Paragraph设置元素前的间距(以点为单位)。
setText(text)void将段落内容设置为文本。
setTextAlignment(textAlignment)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 body contents of the tab by its ID.
// TODO(developer): Replace the ID with your own.
const body = doc.getTab('t.0').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);

参数

名称类型说明
imageBlobSource图片数据。

返回

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 body contents of the tab by its ID.
// TODO(developer): Replace the ID with your own.
const body = doc.getTab('t.0').asDocumentTab().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 body contents of the tab by its ID.
// TODO(developer): Replace the ID with your own.
const body = doc.getTab('t.0').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);

参数

名称类型说明
imageBlobSource图片数据。

返回

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 body contents of the tab by its ID.
// TODO(developer): Replace the ID with your own.
const body = doc.getTab('t.0').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);

参数

名称类型说明
imageInlineImage图片数据。

返回

InlineImage - 附加的图片。

授权

使用此方法的脚本需要获得以下一个或多个范围的授权:

  • https://www.googleapis.com/auth/documents.currentonly
  • https://www.googleapis.com/auth/documents

appendPageBreak()

创建并附加新的 PageBreak

注意:PageBreaks 不能包含在 TableCells 中。 如果当前元素包含在表格单元格中,则会抛出异常。

// 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 body contents of the tab by its ID.
// TODO(developer): Replace the ID with your own.
const body = doc.getTab('t.0').asDocumentTab().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

注意:PageBreaks 不能包含在 TableCells 中。 如果表单元格中存在当前元素,脚本会抛出异常。

// 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 body contents of the tab by its ID.
// TODO(developer): Replace the ID with your own.
const body = doc.getTab('t.0').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);

参数

名称类型说明
pageBreakPageBreak要附加的分页符。

返回

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 body contents of the tab by its ID.
// TODO(developer): Replace the ID with your own.
const body = doc.getTab('t.0').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.');

参数

名称类型说明
textString文本内容。

返回

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 body contents of the tab by its ID.
// TODO(developer): Replace the ID with your own.
const body = doc.getTab('t.0').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);

参数

名称类型说明
textText要附加的文本元素。

返回

Text - 附加的文本元素。

授权

使用此方法的脚本需要获得以下一个或多个范围的授权:

  • https://www.googleapis.com/auth/documents.currentonly
  • https://www.googleapis.com/auth/documents

clear()

清除元素的内容。

返回

Paragraph - 当前元素。


copy()

返回当前元素的分离的深层副本。

元素中存在的所有子元素也会被复制。新元素不含 。

返回

Paragraph - 新副本。

授权

使用此方法的脚本需要获得以下一个或多个范围的授权:

  • https://www.googleapis.com/auth/documents.currentonly
  • https://www.googleapis.com/auth/documents

editAsText()

获取当前元素的 Text 版本,以便进行修改。

使用 editAsText 可将元素内容处理为富文本格式。editAsText 模式会忽略非文本元素(例如 InlineImageHorizontalRule)。

完全包含在已删除文本范围内的子元素会从相应元素中移除。

var 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 - 当前元素的文本版本


findElement(elementType)

搜索指定类型的后代元素的内容。

参数

名称类型说明
elementTypeElementType要搜索的元素的类型。

返回

RangeElement - 表示搜索元素位置的搜索结果。

授权

使用此方法的脚本需要获得以下一个或多个范围的授权:

  • https://www.googleapis.com/auth/documents.currentonly
  • https://www.googleapis.com/auth/documents

findElement(elementType, from)

搜索指定类型的后代元素的内容,从 指定的 RangeElement

var body = DocumentApp.getActiveDocument().getActiveTab().asDocumentTab().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;
  }
}

参数

名称类型说明
elementTypeElementType要搜索的元素的类型。
fromRangeElement要搜索的搜索结果。

返回

RangeElement - 表示搜索元素的下一个位置的搜索结果。

授权

使用此方法的脚本需要获得以下一个或多个范围的授权:

  • https://www.googleapis.com/auth/documents.currentonly
  • https://www.googleapis.com/auth/documents

findText(searchPattern)

使用正则表达式搜索指定文本格式的元素内容。

部分 JavaScript 正则表达式功能尚不完全受支持,例如 捕获组和模式修饰符。

所提供的正则表达式格式会与每个文本块独立进行匹配 当前元素中包含的某些内容。

参数

名称类型说明
searchPatternString要搜索的模式

返回

RangeElement - 表示搜索文本位置的搜索结果;如果没有搜索文本的位置,则为 null 匹配

授权

使用此方法的脚本需要获得以下一个或多个范围的授权:

  • https://www.googleapis.com/auth/documents.currentonly
  • https://www.googleapis.com/auth/documents

findText(searchPattern, from)

搜索指定文本格式的元素内容,从指定的 搜索结果。

部分 JavaScript 正则表达式功能尚不完全受支持,例如 捕获组和模式修饰符。

所提供的正则表达式格式会与每个文本块独立进行匹配 当前元素中包含的某些内容。

参数

名称类型说明
searchPatternString要搜索的模式
fromRangeElement要搜索的搜索结果

返回

RangeElement - 表示搜索文本下一个位置的搜索结果;如果没有搜索文本的下一个位置,则返回 null。 匹配

授权

使用此方法的脚本需要获得以下一个或多个范围的授权:

  • https://www.googleapis.com/auth/documents.currentonly
  • https://www.googleapis.com/auth/documents

getAlignment()

检索 HorizontalAlignment

// 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 body contents of the tab by its ID.
// TODO(developer): Replace the ID with your own.
const body = doc.getTab('t.0').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());

返回

HorizontalAlignment - 对齐方式。

授权

使用此方法的脚本需要获得以下一个或多个范围的授权:

  • https://www.googleapis.com/auth/documents.currentonly
  • https://www.googleapis.com/auth/documents

getAttributes()

检索元素的属性。

结果会得到一个对象,其中包含每个有效元素属性对应的属性,其中每个 属性名称对应于 DocumentApp.Attribute 枚举中的一个项。

var doc = DocumentApp.getActiveDocument();
var documentTab = doc.getActiveTab().asDocumentTab();
var body = documentTab.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)

检索指定子索引处的子元素。

var body = DocumentApp.getActiveDocument().getActiveTab().asDocumentTab().getBody();

// Obtain the first element in the tab.
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.");
}

参数

名称类型说明
childIndexInteger要检索的子元素的索引。

返回

Element - 位于指定索引处的子元素。

授权

使用此方法的脚本需要获得以下一个或多个范围的授权:

  • https://www.googleapis.com/auth/documents.currentonly
  • https://www.googleapis.com/auth/documents

getChildIndex(child)

检索指定子元素的子索引。

参数

名称类型说明
childElement要为其检索索引的子元素。

返回

Integer - 子索引。

授权

使用此方法的脚本需要获得以下一个或多个范围的授权:

  • https://www.googleapis.com/auth/documents.currentonly
  • https://www.googleapis.com/auth/documents

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 body contents of the tab by its ID.
// TODO(developer): Replace the ID with your own.
const body = doc.getTab('t.0').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());

返回

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()

检索该元素的下一个同级元素。

下一个同级元素具有相同的父级元素并位于当前元素之后。

返回

Element - 下一个同级元素。

授权

使用此方法的脚本需要获得以下一个或多个范围的授权:

  • https://www.googleapis.com/auth/documents.currentonly
  • https://www.googleapis.com/auth/documents

getNumChildren()

检索子元素的数量。

var 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

getParent()

获取该元素的父元素。

父元素包含当前元素。

返回

ContainerElement - 父元素。

授权

使用此方法的脚本需要获得以下一个或多个范围的授权:

  • https://www.googleapis.com/auth/documents.currentonly
  • https://www.googleapis.com/auth/documents

getPositionedImage(id)

根据图片的 ID 获取 PositionedImage

参数

名称类型说明
idString映像 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()

检索该元素的前一个同级元素。

上一个同级项具有相同的父元素,并且位于当前元素之前。

返回

Element - 上一个同级元素。

授权

使用此方法的脚本需要获得以下一个或多个范围的授权:

  • https://www.googleapis.com/auth/documents.currentonly
  • https://www.googleapis.com/auth/documents

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.NORMALDocumentApp.TextAlignment.SUBSCRIPTDocumentApp.TextAlignment.SUPERSCRIPT

返回

TextAlignment - 文本的对齐方式类型;如果文本包含多种类型的文本,则为 null 对齐方式,或者从未设置文本对齐方式

授权

使用此方法的脚本需要获得以下一个或多个范围的授权:

  • https://www.googleapis.com/auth/documents.currentonly
  • https://www.googleapis.com/auth/documents

getType()

检索元素的 ElementType

使用 getType() 确定指定元素的确切类型。

var doc = DocumentApp.getActiveDocument();
var documentTab = doc.getActiveTab().asDocumentTab();
var body = documentTab.getBody();

// Obtain the first element in the active tab's 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

参数

名称类型说明
childIndexInteger要插入元素的索引

返回

HorizontalRule - 新的水平规则元素

授权

使用此方法的脚本需要获得以下一个或多个范围的授权:

  • https://www.googleapis.com/auth/documents.currentonly
  • https://www.googleapis.com/auth/documents

insertInlineImage(childIndex, image)

在指定的图片 blob 处创建并插入新的 InlineImage。 索引中。

参数

名称类型说明
childIndexInteger要插入元素的索引
imageBlobSource图片数据

返回

InlineImage - 插入的内嵌图片元素

授权

使用此方法的脚本需要获得以下一个或多个范围的授权:

  • https://www.googleapis.com/auth/documents.currentonly
  • https://www.googleapis.com/auth/documents

insertInlineImage(childIndex, image)

在指定索引处插入给定 InlineImage

参数

名称类型说明
childIndexInteger要插入元素的索引
imageInlineImage图片数据

返回

InlineImage - 插入的内嵌图片元素

授权

使用此方法的脚本需要获得以下一个或多个范围的授权:

  • https://www.googleapis.com/auth/documents.currentonly
  • https://www.googleapis.com/auth/documents

insertPageBreak(childIndex)

在指定索引处创建并插入新的 PageBreak

注意:PageBreaks 可能不包含在 TableCells 中。 如果当前元素包含在表格单元格中,则会抛出异常。

参数

名称类型说明
childIndexInteger要插入元素的索引

返回

PageBreak - 新的分页符元素

授权

使用此方法的脚本需要获得以下一个或多个范围的授权:

  • https://www.googleapis.com/auth/documents.currentonly
  • https://www.googleapis.com/auth/documents

insertPageBreak(childIndex, pageBreak)

在指定索引处插入给定 PageBreak

注意:PageBreaks 可能不包含在 TableCells 中。 如果当前元素包含在表格单元格中,则会抛出异常。

参数

名称类型说明
childIndexInteger要插入元素的索引
pageBreakPageBreak要插入的分页符

返回

PageBreak - 插入的分页符元素

授权

使用此方法的脚本需要获得以下一个或多个范围的授权:

  • https://www.googleapis.com/auth/documents.currentonly
  • https://www.googleapis.com/auth/documents

insertText(childIndex, text)

在指定索引处创建并插入新的文本元素。

参数

名称类型说明
childIndexInteger要插入元素的索引
textString文本内容

返回

Text - 新的文本元素

授权

使用此方法的脚本需要获得以下一个或多个范围的授权:

  • https://www.googleapis.com/auth/documents.currentonly
  • https://www.googleapis.com/auth/documents

insertText(childIndex, text)

使用指定文本在指定索引处插入指定的 Text 元素 内容。

参数

名称类型说明
childIndexInteger要插入元素的索引
textText要插入的文本元素

返回

Text - 插入的文本元素

授权

使用此方法的脚本需要获得以下一个或多个范围的授权:

  • https://www.googleapis.com/auth/documents.currentonly
  • https://www.googleapis.com/auth/documents

isAtDocumentEnd()

确定元素是否在 Document 的末尾。

返回

Boolean - 表示元素是否在标签页的末尾。

授权

使用此方法的脚本需要获得以下一个或多个范围的授权:

  • https://www.googleapis.com/auth/documents.currentonly
  • https://www.googleapis.com/auth/documents

isLeftToRight()

检索从左到右的设置。

返回

Boolean - 从左到右设置

授权

使用此方法的脚本需要获得以下一个或多个范围的授权:

  • https://www.googleapis.com/auth/documents.currentonly
  • https://www.googleapis.com/auth/documents

merge()

将元素与前一个相同类型的同级元素合并。

只能合并同一 ElementType 的元素。文件中包含的 当前元素会移动到前一个同级元素。

当前元素会从文档中移除。

var doc = DocumentApp.getActiveDocument();
var documentTab = doc.getActiveTab().asDocumentTab();
var body = documentTab.getBody();

// Example 1: Merge paragraphs
// Append two paragraphs to the document's active tab.
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)

删除指定的子元素。

参数

名称类型说明
childElement要移除的子元素

返回

Paragraph - 当前元素

授权

使用此方法的脚本需要获得以下一个或多个范围的授权:

  • https://www.googleapis.com/auth/documents.currentonly
  • https://www.googleapis.com/auth/documents

removeFromParent()

将元素从其父元素中移除。

var doc = DocumentApp.getActiveDocument();
var documentTab = doc.getActiveTab().asDocumentTab()
var body = documentTab.getBody();

// Remove all images in the active tab's 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

参数

名称类型说明
idString映像 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().getActiveTab().asDocumentTab().getBody();

// Clear the text surrounding "Apps Script", with or without text.
body.replaceText("^.*Apps ?Script.*$", "Apps Script");

参数

名称类型说明
searchPatternString要搜索的正则表达式模式
replacementString要用作替换的文本

返回

Element - 当前元素

授权

使用此方法的脚本需要获得以下一个或多个范围的授权:

  • https://www.googleapis.com/auth/documents.currentonly
  • https://www.googleapis.com/auth/documents

setAlignment(alignment)

设置 HorizontalAlignment

参数

名称类型说明
alignmentHorizontalAlignment水平对齐方式

返回

Paragraph - 当前元素

授权

使用此方法的脚本需要获得以下一个或多个范围的授权:

  • https://www.googleapis.com/auth/documents.currentonly
  • https://www.googleapis.com/auth/documents

setAttributes(attributes)

设置元素的属性。

指定的属性参数必须是一个对象,其中每个属性名称都是 DocumentApp.Attribute 枚举,并且每个属性值都是要 。

var doc = DocumentApp.getActiveDocument();
var documentTab = doc.getActiveTab().asDocumentTab();
var body = documentTab.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);

参数

名称类型说明
attributesObject元素的属性。

返回

Paragraph - 当前元素。

授权

使用此方法的脚本需要获得以下一个或多个范围的授权:

  • https://www.googleapis.com/auth/documents.currentonly
  • https://www.googleapis.com/auth/documents

setHeading(heading)

设置 ParagraphHeading

参数

名称类型说明
headingParagraphHeading标题

返回

Paragraph - 当前元素

授权

使用此方法的脚本需要获得以下一个或多个范围的授权:

  • https://www.googleapis.com/auth/documents.currentonly
  • https://www.googleapis.com/auth/documents

setIndentEnd(indentEnd)

设置末尾缩进(以点为单位)。

参数

名称类型说明
indentEndNumber末尾缩进,以点为单位

返回

Paragraph - 当前元素

授权

使用此方法的脚本需要获得以下一个或多个范围的授权:

  • https://www.googleapis.com/auth/documents.currentonly
  • https://www.googleapis.com/auth/documents

setIndentFirstLine(indentFirstLine)

设置首行缩进(以点为单位)。

参数

名称类型说明
indentFirstLineNumber首行缩进(以点为单位)

返回

Paragraph - 当前元素

授权

使用此方法的脚本需要获得以下一个或多个范围的授权:

  • https://www.googleapis.com/auth/documents.currentonly
  • https://www.googleapis.com/auth/documents

setIndentStart(indentStart)

设置起始缩进(以点为单位)。

参数

名称类型说明
indentStartNumber起始缩进,以点为单位

返回

Paragraph - 当前元素

授权

使用此方法的脚本需要获得以下一个或多个范围的授权:

  • https://www.googleapis.com/auth/documents.currentonly
  • https://www.googleapis.com/auth/documents

setLeftToRight(leftToRight)

设置从左到右的设置。

参数

名称类型说明
leftToRightBoolean“从左到右”设置

返回

Paragraph - 当前元素

授权

使用此方法的脚本需要获得以下一个或多个范围的授权:

  • https://www.googleapis.com/auth/documents.currentonly
  • https://www.googleapis.com/auth/documents

setLineSpacing(multiplier)

设置行间距,是一个数量,用于表示要用于间距的行数。

参数

名称类型说明
multiplierNumber行数

返回

Paragraph - 当前元素

授权

使用此方法的脚本需要获得以下一个或多个范围的授权:

  • https://www.googleapis.com/auth/documents.currentonly
  • https://www.googleapis.com/auth/documents

setLinkUrl(url)

设置链接网址。

参数

名称类型说明
urlString链接网址

返回

Paragraph - 当前元素

授权

使用此方法的脚本需要获得以下一个或多个范围的授权:

  • https://www.googleapis.com/auth/documents.currentonly
  • https://www.googleapis.com/auth/documents

setSpacingAfter(spacingAfter)

设置元素后的间距(以点为单位)。

参数

名称类型说明
spacingAfterNumber元素后面的间距(以点为单位)

返回

Paragraph - 当前元素

授权

使用此方法的脚本需要获得以下一个或多个范围的授权:

  • https://www.googleapis.com/auth/documents.currentonly
  • https://www.googleapis.com/auth/documents

setSpacingBefore(spacingBefore)

设置元素前的间距(以点为单位)。

参数

名称类型说明
spacingBeforeNumber元素前的间距(以点为单位)

返回

Paragraph - 当前元素

授权

使用此方法的脚本需要获得以下一个或多个范围的授权:

  • https://www.googleapis.com/auth/documents.currentonly
  • https://www.googleapis.com/auth/documents

setText(text)

将段落内容设置为文本。

注意:现有内容会被清除。

参数

名称类型说明
textString新文本内容

授权

使用此方法的脚本需要获得以下一个或多个范围的授权:

  • https://www.googleapis.com/auth/documents.currentonly
  • https://www.googleapis.com/auth/documents

setTextAlignment(textAlignment)

设置文本对齐方式。可用的对齐类型包括 DocumentApp.TextAlignment.NORMALDocumentApp.TextAlignment.SUBSCRIPTDocumentApp.TextAlignment.SUPERSCRIPT

// Make the entire first paragraph in the active tab be superscript.
var documentTab = DocumentApp.getActiveDocument().getActiveTab().asDocumentTab();
var text = documentTab.getBody().getParagraphs()[0].editAsText();
text.setTextAlignment(DocumentApp.TextAlignment.SUPERSCRIPT);

参数

名称类型说明
textAlignmentTextAlignment要应用的文字对齐方式类型

返回

Paragraph - 当前元素

授权

使用此方法的脚本需要获得以下一个或多个范围的授权:

  • https://www.googleapis.com/auth/documents.currentonly
  • https://www.googleapis.com/auth/documents