Class TableCell

TableCell

表示表格单元格的元素。TableCell 始终包含在 TableRow 中,并且可以包含 ListItemParagraphTable 元素。如需详细了解文档结构,请参阅扩展 Google 文档指南

方法

方法返回类型简介
appendHorizontalRule()HorizontalRule创建并附加新的 HorizontalRule
appendImage(image)InlineImage根据指定的图片 blob 创建并附加新的 InlineImage
appendImage(image)InlineImage附加给定的 InlineImage
appendListItem(listItem)ListItem附加给定的 ListItem
appendListItem(text)ListItem创建并附加新的 ListItem
appendParagraph(paragraph)Paragraph附加给定的 Paragraph
appendParagraph(text)Paragraph创建并附加新的 Paragraph
appendTable()Table创建并附加新的 Table
appendTable(cells)Table附加一个包含指定单元格的新 Table
appendTable(table)Table附加给定的 Table
clear()TableCell清除元素的内容。
copy()TableCell返回当前元素的已分离深层副本。
editAsText()Text获取当前元素的 Text 版本以进行修改。
findElement(elementType)RangeElement|null在元素的内容中搜索指定类型的后代。
findElement(elementType, from)RangeElement|null从指定的 RangeElement 开始,在元素的内容中搜索指定类型的后代。
findText(searchPattern)RangeElement|null使用正则表达式在元素的内容中搜索指定的文本模式。
findText(searchPattern, from)RangeElement|null从给定的搜索结果开始,在元素的内容中搜索指定的文本模式。
getAttributes()Object检索元素的属性。
getBackgroundColor()String|null检索背景颜色。
getChild(childIndex)Element检索指定子索引处的子元素。
getChildIndex(child)Integer检索指定子元素的子级索引。
getColSpan()Integer检索列跨度,即此单元格跨越的表格单元格列数。
getLinkUrl()String|null检索链接网址。
getNextSibling()Element|null检索元素的下一个同级元素。
getNumChildren()Integer检索子项的数量。
getPaddingBottom()Number|null检索底部内边距(以点为单位)。
getPaddingLeft()Number|null检索左侧内边距(以点为单位)。
getPaddingRight()Number|null检索右侧内边距(以点为单位)。
getPaddingTop()Number|null检索顶部内边距(以点为单位)。
getParent()ContainerElement|null检索元素的父元素。
getParentRow()TableRow|null检索包含当前 TableCellTableRow
getParentTable()Table|null检索包含当前 TableCellTable
getPreviousSibling()Element|null检索元素的前一个同级元素。
getRowSpan()Integer检索行跨度,即此单元格跨越的表格单元格的行数。
getText()String以文本字符串形式检索元素的内容。
getTextAlignment()TextAlignment|null获取文本对齐方式。
getType()ElementType检索元素的 ElementType
getVerticalAlignment()VerticalAlignment|null检索 VerticalAlignment
getWidth()Number|null检索包含相应单元格的列的宽度(以磅为单位)。
insertHorizontalRule(childIndex)HorizontalRule在指定索引处创建并插入新的 HorizontalRule
insertImage(childIndex, image)InlineImage在指定索引处,根据指定的图片 blob 创建并插入 InlineImage
insertImage(childIndex, image)InlineImage在指定索引处插入给定的 InlineImage
insertListItem(childIndex, listItem)ListItem在指定索引处插入给定的 ListItem
insertListItem(childIndex, text)ListItem在指定索引处创建并插入新的 ListItem
insertParagraph(childIndex, paragraph)Paragraph在指定索引处插入给定的 Paragraph
insertParagraph(childIndex, text)Paragraph在指定索引处创建并插入新的 Paragraph
insertTable(childIndex)Table在指定索引处创建并插入新的 Table
insertTable(childIndex, cells)Table在指定索引处创建并插入包含指定单元格的新 Table
insertTable(childIndex, table)Table在指定索引处插入给定的 Table
isAtDocumentEnd()Boolean确定元素是否位于 Document 的末尾。
merge()TableCell|null将元素与同一类型的前一个同级元素合并。
removeChild(child)TableCell移除指定的子元素。
removeFromParent()TableCell|null从其父级中移除元素。
replaceText(searchPattern, replacement)Element使用正则表达式将指定文本模式的所有匹配项替换为指定的替换字符串。
setAttributes(attributes)TableCell设置元素的属性。
setBackgroundColor(color)TableCell设置背景颜色。
setLinkUrl(url)TableCell设置链接网址。
setPaddingBottom(paddingBottom)TableCell设置底部内边距(以点为单位)。
setPaddingLeft(paddingLeft)TableCell设置左侧内边距(以点为单位)。
setPaddingRight(paddingRight)TableCell设置右侧内边距(以点为单位)。
setPaddingTop(paddingTop)TableCell设置顶部内边距(以点为单位)。
setText(text)TableCell将内容设置为纯文本。
setTextAlignment(textAlignment)TableCell设置文本对齐方式。
setVerticalAlignment(alignment)TableCell设置垂直对齐方式。
setWidth(width)TableCell设置包含当前单元格的列的宽度(以磅为单位)。

详细文档

appendHorizontalRule()

创建并附加新的 HorizontalRule

HorizontalRule 将包含在新的 Paragraph 中。

返回

HorizontalRule - 新的水平分隔线

授权

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

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

appendImage(image)

根据指定的图片 blob 创建并附加新的 InlineImage

InlineImage 将包含在新的 Paragraph 中。

参数

名称类型说明
imageBlobSource图片数据

返回

InlineImage - 新图片

授权

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

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

appendImage(image)

附加给定的 InlineImage

InlineImage 将包含在新的 Paragraph 中。

参数

名称类型说明
imageInlineImage图片数据

返回

InlineImage - 附加的图片

授权

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

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

appendListItem(listItem)

附加给定的 ListItem

参数

名称类型说明
listItemListItem要附加的列表项

返回

ListItem - 附加的列表项

授权

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

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

appendListItem(text)

创建并附加新的 ListItem

参数

名称类型说明
textString文本内容

返回

ListItem - 新的列表项

授权

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

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

appendParagraph(paragraph)

附加给定的 Paragraph

参数

名称类型说明
paragraphParagraph要附加的段落

返回

Paragraph - 附加的段落

授权

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

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

appendParagraph(text)

创建并附加新的 Paragraph

参数

名称类型说明
textString文本内容

返回

Paragraph - 新段落

授权

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

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

appendTable()

创建并附加新的 Table

返回

Table - 新表格

授权

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

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

appendTable(cells)

附加一个包含指定单元格的新 Table

参数

名称类型说明
cellsString[][]要添加到新表格的表格单元格的文本内容

返回

Table - 追加的表

授权

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

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

appendTable(table)

附加给定的 Table

参数

名称类型说明
tableTable要附加的表

返回

Table - 追加的表

授权

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

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

clear()

清除元素的内容。

返回

TableCell - 当前元素。


copy()

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

元素中包含的所有子元素也会被复制。新元素没有父元素。

返回

TableCell - 新文案。

授权

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

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

editAsText()

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

使用 editAsText 将元素内容作为富文本进行操作。editAsText 模式会忽略非文本元素(例如 InlineImageHorizontalRule)。

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

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


findElement(elementType)

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

参数

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

返回

RangeElement|null - 一种搜索结果,用于指示搜索元素的位置。

授权

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

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

findElement(elementType, from)

从指定的 RangeElement 开始,在元素的内容中搜索指定类型的后代。

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;
  }
}

参数

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

返回

RangeElement|null - 一种搜索结果,用于指示搜索元素的下一个位置。

授权

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

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

findText(searchPattern)

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

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

系统会针对当前元素中包含的每个文本块独立匹配所提供的正则表达式模式。

参数

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

返回

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

授权

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

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

findText(searchPattern, from)

从给定的搜索结果开始,在元素的内容中搜索指定的文本模式。

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

系统会针对当前元素中包含的每个文本块独立匹配所提供的正则表达式模式。

参数

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

返回

RangeElement|null - 指示搜索文本的下一个位置的搜索结果,如果没有匹配项,则为 null

授权

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

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

getAttributes()

检索元素的属性。

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

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

getBackgroundColor()

检索背景颜色。

返回

String|null - 背景颜色,采用 CSS 表示法(例如 '#ffffff')设置格式

授权

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

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

getChild(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.');
}

参数

名称类型说明
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

getColSpan()

检索列跨度,即此单元格跨越的表格单元格列数。 未合并的单元格的列跨度为 1。

返回

Integer - 列跨度,如果单元格被其他单元格合并,则为 0

授权

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

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

getLinkUrl()

检索链接网址。

返回

String|null - 链接网址,如果元素包含此属性的多个值,则为 null

授权

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

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

getNextSibling()

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

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

返回

Element|null - 下一个同级元素。

授权

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

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

getNumChildren()

检索子项的数量。

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

getPaddingBottom()

检索底部内边距(以点为单位)。

返回

Number|null - 底部内边距(以点为单位)

授权

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

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

getPaddingLeft()

检索左侧内边距(以点为单位)。

返回

Number|null - 左侧内边距(以点为单位)

授权

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

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

getPaddingRight()

检索右侧内边距(以点为单位)。

返回

Number|null - 右侧内边距(以点为单位)

授权

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

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

getPaddingTop()

检索顶部内边距(以点为单位)。

返回

Number|null - 顶部内边距(以点为单位)

授权

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

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

getParent()

检索元素的父元素。

父元素包含当前元素。

返回

ContainerElement|null - 父元素。

授权

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

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

getParentRow()

检索包含当前 TableCellTableRow

返回

TableRow|null - 包含当前单元格的表格行

授权

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

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

getParentTable()

检索包含当前 TableCellTable

返回

Table|null - 包含当前单元格的表格

授权

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

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

getPreviousSibling()

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

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

返回

Element|null - 上一个同级元素。

授权

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

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

getRowSpan()

检索行跨度,即此单元格跨越的表格单元格的行数。未合并的单元格的行跨度为 1。

返回

Integer - 行跨度,如果单元格被其他单元格合并,则为 0

授权

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

  • 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 - 文本对齐类型,如果文本包含多种文本对齐类型或从未设置过文本对齐方式,则为 null

授权

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

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

getType()

检索元素的 ElementType

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

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.');
}

返回

ElementType - 元素类型。

授权

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

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

getVerticalAlignment()

检索 VerticalAlignment

返回

VerticalAlignment|null - 垂直对齐

授权

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

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

getWidth()

检索包含相应单元格的列的宽度(以磅为单位)。

返回

Number|null - 列宽(以磅为单位)

授权

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

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

insertHorizontalRule(childIndex)

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

水平线将包含在新的 Paragraph 中。

参数

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

返回

HorizontalRule - 新的水平分隔线

授权

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

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

insertImage(childIndex, image)

在指定索引处,根据指定的图片 blob 创建并插入 InlineImage

图片将包含在新的 Paragraph 中。

参数

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

返回

InlineImage - 插入的内嵌图片

授权

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

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

insertImage(childIndex, image)

在指定索引处插入给定的 InlineImage

图片将包含在新的 Paragraph 中。

参数

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

返回

InlineImage - 插入的内嵌图片

授权

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

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

insertListItem(childIndex, listItem)

在指定索引处插入给定的 ListItem

参数

名称类型说明
childIndexInteger要插入元素的索引
listItemListItem要插入的列表项

返回

ListItem - 插入了新的清单项

授权

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

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

insertListItem(childIndex, text)

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

参数

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

返回

ListItem - 新的列表项

授权

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

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

insertParagraph(childIndex, paragraph)

在指定索引处插入给定的 Paragraph

参数

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

返回

Paragraph - 插入的段落

授权

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

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

insertParagraph(childIndex, text)

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

参数

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

返回

Paragraph - 新段落

授权

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

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

insertTable(childIndex)

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

参数

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

返回

Table - 新表格

授权

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

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

insertTable(childIndex, cells)

在指定索引处创建并插入包含指定单元格的新 Table

参数

名称类型说明
childIndexInteger要插入的索引
cellsString[][]要添加到新表格的表格单元格的文本内容

返回

Table - 新表格

授权

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

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

insertTable(childIndex, table)

在指定索引处插入给定的 Table

参数

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

返回

Table - 插入的表格

授权

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

  • 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

merge()

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

只有相同 ElementType 的元素才能合并。当前元素中包含的所有子元素都会移至前面的同级元素。

从文档中移除当前元素。

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

返回

TableCell|null - 合并后的元素。

授权

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

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

removeChild(child)

移除指定的子元素。

参数

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

返回

TableCell - 当前元素

授权

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

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

removeFromParent()

从父级中移除元素。

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

返回

TableCell|null - 移除的元素。

授权

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

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

replaceText(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');

参数

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

返回

Element - 当前元素

授权

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

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

setAttributes(attributes)

设置元素的属性。

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

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

参数

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

返回

TableCell - 当前元素。

授权

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

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

setBackgroundColor(color)

设置背景颜色。

参数

名称类型说明
colorString背景颜色,采用 CSS 表示法(例如 '#ffffff')设置格式

返回

TableCell - 当前元素

授权

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

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

setLinkUrl(url)

设置链接网址。

参数

名称类型说明
urlString链接网址

返回

TableCell - 当前元素

授权

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

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

setPaddingBottom(paddingBottom)

设置底部内边距(以点为单位)。

参数

名称类型说明
paddingBottomNumber底部内边距(以点为单位)

返回

TableCell - 当前元素

授权

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

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

setPaddingLeft(paddingLeft)

设置左侧内边距(以点为单位)。

参数

名称类型说明
paddingLeftNumber左侧内边距(以点为单位)

返回

TableCell - 当前元素

授权

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

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

setPaddingRight(paddingRight)

设置右侧内边距(以点为单位)。

参数

名称类型说明
paddingRightNumber右侧内边距(以点为单位)。

返回

TableCell - 当前元素。

授权

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

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

setPaddingTop(paddingTop)

设置顶部内边距(以点为单位)。

参数

名称类型说明
paddingTopNumber顶部内边距(以点为单位)

返回

TableCell - 当前元素

授权

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

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

setText(text)

将内容设置为纯文本。

注意:系统会清除现有内容。

参数

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

返回

TableCell - 当前元素

授权

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

  • 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.
const documentTab =
    DocumentApp.getActiveDocument().getActiveTab().asDocumentTab();
const text = documentTab.getBody().getParagraphs()[0].editAsText();
text.setTextAlignment(DocumentApp.TextAlignment.SUPERSCRIPT);

参数

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

返回

TableCell - 当前元素

授权

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

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

setVerticalAlignment(alignment)

设置垂直对齐方式。

参数

名称类型说明
alignmentVerticalAlignment垂直对齐方式

返回

TableCell - 当前元素

授权

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

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

setWidth(width)

设置包含当前单元格的列的宽度(以磅为单位)。

参数

名称类型说明
widthNumber列宽(以磅为单位)

返回

TableCell - 当前元素

授权

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

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