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搜尋指定類型的子系元素內容。
findElement(elementType, from)RangeElement從指定的 RangeElement 開始,搜尋指定類型的子系元素內容。
findText(searchPattern)RangeElement使用規則運算式搜尋元素的內容。
findText(searchPattern, from)RangeElement從特定搜尋結果開始,搜尋指定文字模式的元素內容。
getAttributes()Object擷取元素的屬性。
getBackgroundColor()String擷取背景顏色。
getChild(childIndex)Element擷取指定子索引的子元素。
getChildIndex(child)Integer擷取指定子元素的子索引。
getColSpan()Integer擷取資料欄時距,也就是這個儲存格範圍的表格儲存格欄數。
getLinkUrl()String擷取連結網址。
getNextSibling()Element擷取元素的下一個同層級元素。
getNumChildren()Integer擷取子項的數量。
getPaddingBottom()Number擷取點中的底部邊框間距。
getPaddingLeft()Number擷取點中的左側邊框間距。
getPaddingRight()Number擷取點中的右側邊框間距。
getPaddingTop()Number擷取點中的頂部邊框間距。
getParent()ContainerElement擷取元素的父項元素。
getParentRow()TableRow擷取包含目前 TableCellTableRow
getParentTable()Table擷取包含目前 TableCellTable
getPreviousSibling()Element擷取元素先前的同層級元素。
getRowSpan()Integer擷取列跨距,也就是這個儲存格橫跨的表格儲存格列數。
getText()String以文字字串擷取元素內容。
getTextAlignment()TextAlignment取得文字對齊方式。
getType()ElementType擷取元素的 ElementType
getVerticalAlignment()VerticalAlignment擷取 VerticalAlignment
getWidth()Number擷取點,擷取包含儲存格的資料欄寬度。
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將元素與下一個同類型的同層級合併。
removeChild(child)TableCell移除指定的子元素。
removeFromParent()TableCell從父項元素中移除元素。
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 將元素內容視為 RTF 格式。editAsText 模式會忽略非文字元素 (例如 InlineImageHorizontalRule)。

已刪除文字範圍內的子項元素會從元素中移除。

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)

搜尋指定類型的子系元素內容。

參數

名稱類型說明
elementTypeElementType要搜尋的元素類型

回攻員

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

參數

名稱類型說明
elementTypeElementType要搜尋的元素類型
fromRangeElement而要搜尋的內容

回攻員

RangeElement:表示搜尋元素下一個位置的搜尋結果

授權

使用這個方法的指令碼必須取得以下一或多個範圍的授權:

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

findText(searchPattern)

使用規則運算式搜尋元素內容中的指定文字模式。

未完全支援部分 JavaScript 規則運算式功能,例如擷取群組和模式修飾符。

提供的規則運算式模式會與目前元素中包含的每個文字區塊分開。

參數

名稱類型說明
searchPatternString要搜尋的模式

回攻員

RangeElement:搜尋結果,指出搜尋文字的位置;如果沒有相符結果,則傳回空值

授權

使用這個方法的指令碼必須取得以下一或多個範圍的授權:

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

findText(searchPattern, from)

從特定的搜尋結果開始,搜尋指定文字模式的元素內容。

未完全支援部分 JavaScript 規則運算式功能,例如擷取群組和模式修飾符。

提供的規則運算式模式會與目前元素中包含的每個文字區塊分開。

參數

名稱類型說明
searchPatternString要搜尋的模式
fromRangeElement而要搜尋的內容

回攻員

RangeElement:搜尋結果指出搜尋文字的下一個位置,如果沒有相符結果則傳回空值

授權

使用這個方法的指令碼必須取得以下一或多個範圍的授權:

  • 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

getBackgroundColor()

擷取背景顏色。

回攻員

String:背景顏色,採用 CSS 標記法格式 (例如 '#ffffff')

授權

使用這個方法的指令碼必須取得以下一或多個範圍的授權:

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

參數

名稱類型說明
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

授權

使用這個方法的指令碼必須取得以下一或多個範圍的授權:

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

擷取子項的數量。

// 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

getPaddingBottom()

擷取點中的底部邊框間距。

回攻員

Number:底部邊框間距 (以點為單位)

授權

使用這個方法的指令碼必須取得以下一或多個範圍的授權:

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

getPaddingLeft()

擷取點中的左側邊框間距。

回攻員

Number:左側邊框間距,以點表示

授權

使用這個方法的指令碼必須取得以下一或多個範圍的授權:

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

getPaddingRight()

擷取點中的右側邊框間距。

回攻員

Number:右側邊框間距,以點表示

授權

使用這個方法的指令碼必須取得以下一或多個範圍的授權:

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

getPaddingTop()

擷取點中的頂部邊框間距。

回攻員

Number:頂端邊框間距,以點為單位

授權

使用這個方法的指令碼必須取得以下一或多個範圍的授權:

  • 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

getParentRow()

擷取包含目前 TableCellTableRow

回攻員

TableRow:包含目前儲存格的表格列

授權

使用這個方法的指令碼必須取得以下一或多個範圍的授權:

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

getParentTable()

擷取包含目前 TableCellTable

回攻員

Table:內含目前儲存格的表格

授權

使用這個方法的指令碼必須取得以下一或多個範圍的授權:

  • 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

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

授權

使用這個方法的指令碼必須取得以下一或多個範圍的授權:

  • 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

getVerticalAlignment()

擷取 VerticalAlignment

回攻員

VerticalAlignment:垂直對齊

授權

使用這個方法的指令碼必須取得以下一或多個範圍的授權:

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

getWidth()

擷取點,擷取包含儲存格的資料欄寬度。

回攻員

Number:欄的寬度,以點為單位

授權

使用這個方法的指令碼必須取得以下一或多個範圍的授權:

  • 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 的元素。目前元素中包含的任何子項元素都會移至上述同層級元素。

目前元素會從文件中移除。

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

回攻員

TableCell:合併的元素。

授權

使用這個方法的指令碼必須取得以下一或多個範圍的授權:

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

從父項元素中移除元素。

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

回攻員

TableCell - 已移除的元素。

授權

使用這個方法的指令碼必須取得以下一或多個範圍的授權:

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

參數

名稱類型說明
searchPatternString要搜尋的規則運算式模式
replacementString要替換的文字

回攻員

Element:目前的元素

授權

使用這個方法的指令碼必須取得以下一或多個範圍的授權:

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

setAttributes(attributes)

設定元素的屬性。

指定的屬性參數必須是一個物件,其中每個屬性名稱都是 DocumentApp.Attribute 列舉中的一個項目,每個屬性值都是要套用的新值。

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

// Define a custom paragraph style.
var style = {};
style[DocumentApp.Attribute.HORIZONTAL_ALIGNMENT] =
    DocumentApp.HorizontalAlignment.RIGHT;
style[DocumentApp.Attribute.FONT_FAMILY] = 'Calibri';
style[DocumentApp.Attribute.FONT_SIZE] = 18;
style[DocumentApp.Attribute.BOLD] = true;

// Append a plain paragraph.
var par = body.appendParagraph('A paragraph with custom style.');

// Apply the custom style.
par.setAttributes(style);

參數

名稱類型說明
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 be superscript.
var text = DocumentApp.getActiveDocument().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