Class Body

內文

Google 文件中分頁的內容。Body 可能包含 ListItemParagraphTableTableOfContents 元素。如要進一步瞭解文件結構,請參閱擴充 Google 文件指南

Body 通常包含分頁的完整內容,但 HeaderSectionFooterSection 和任何 FootnoteSection 元素除外。

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

// Append a paragraph and a page break to the tab's body section directly.
body.appendParagraph('A paragraph.');
body.appendPageBreak();

方法

方法傳回類型簡短說明
appendHorizontalRule()HorizontalRule建立並附加新的 HorizontalRule
appendImage(image)InlineImage從指定的圖片 Blob 建立並附加新的 InlineImage
appendImage(image)InlineImage附加指定的 InlineImage
appendListItem(listItem)ListItem附加指定的 ListItem
appendListItem(text)ListItem建立並附加包含指定文字內容的新 ListItem
appendPageBreak()PageBreak建立並附加新的 PageBreak
appendPageBreak(pageBreak)PageBreak附加指定的 PageBreak
appendParagraph(paragraph)Paragraph附加指定的 Paragraph
appendParagraph(text)Paragraph建立並附加包含指定文字內容的新 Paragraph
appendTable()Table建立並附加新的 Table
appendTable(cells)Table為每個指定的字串值附加新的 Table (內含 TableCell)。
appendTable(table)Table附加指定的 Table
clear()Body清除元素內容。
copy()Body傳回目前元素的深層副本,且該副本已分離。
editAsText()Text取得目前元素的 Text 版本,以供編輯。
findElement(elementType)RangeElement|null搜尋元素內容,找出指定型別的後代。
findElement(elementType, from)RangeElement|null從指定 RangeElement 開始,在元素內容中搜尋指定型別的後代。
findText(searchPattern)RangeElement|null使用規則運算式在元素內容中搜尋指定的文字模式。
findText(searchPattern, from)RangeElement|null從指定的搜尋結果開始,在元素內容中搜尋特定文字模式。
getAttributes()Object擷取元素的屬性。
getChild(childIndex)Element擷取指定子項索引處的子項元素。
getChildIndex(child)Integer擷取指定子項元素的子項索引。
getHeadingAttributes(paragraphHeading)Object擷取所提供 ParagraphHeading 的屬性集。
getImages()InlineImage[]|null擷取該節中包含的所有 InlineImages
getListItems()ListItem[]|null擷取該節中包含的所有 ListItems
getMarginBottom()Number|null以點為單位擷取下邊界。
getMarginLeft()Number|null以點為單位擷取左邊界。
getMarginRight()Number|null擷取右邊界。
getMarginTop()Number|null擷取上邊界。
getNumChildren()Integer擷取子項數量。
getPageHeight()Number|null以點為單位擷取頁面高度。
getPageWidth()Number|null以點為單位擷取頁面寬度。
getParagraphs()Paragraph[]|null擷取區段中包含的所有 Paragraphs (包括 ListItems)。
getParent()ContainerElement|null擷取元素的父項元素。
getTables()Table[]|null擷取該節中包含的所有 Tables
getText()String以文字字串形式擷取元素內容。
getTextAlignment()TextAlignment|null取得文字對齊方式。
getType()ElementType擷取元素的 ElementType
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,其中包含指定的文字內容。
insertPageBreak(childIndex)PageBreak在指定索引處建立並插入新的 PageBreak
insertPageBreak(childIndex, pageBreak)PageBreak在指定索引處插入指定 PageBreak
insertParagraph(childIndex, paragraph)Paragraph在指定索引處插入指定 Paragraph
insertParagraph(childIndex, text)Paragraph在指定索引建立並插入新的 Paragraph,其中包含指定的文字內容。
insertTable(childIndex)Table在指定索引處建立並插入新的 Table
insertTable(childIndex, cells)Table建立並插入新的 Table,其中包含指定索引處的指定儲存格。
insertTable(childIndex, table)Table在指定索引處插入指定 Table
removeChild(child)Body移除指定的子項元素。
replaceText(searchPattern, replacement)Element使用規則運算式,將指定文字模式的所有例項替換為指定替代字串。
setAttributes(attributes)Body設定元素的屬性。
setHeadingAttributes(paragraphHeading, attributes)Body為提供的 ParagraphHeading 設定屬性。
setMarginBottom(marginBottom)Body以點為單位設定下邊界。
setMarginLeft(marginLeft)Body以點為單位設定左邊界。
setMarginRight(marginRight)Body以點為單位設定右邊界。
setMarginTop(marginTop)Body設定上邊界。
setPageHeight(pageHeight)Body以點為單位設定頁面高度。
setPageWidth(pageWidth)Body以點為單位設定頁面寬度。
setText(text)Body將內容設為純文字。
setTextAlignment(textAlignment)Body設定文字對齊方式。

內容詳盡的說明文件

appendHorizontalRule()

建立並附加新的 HorizontalRule

HorizontalRule 會包含在新的 Paragraph 中。

回攻員

HorizontalRule:新的水平線。

授權

使用這個方法的指令碼需要一或多個下列範圍的授權:

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

appendImage(image)

從指定的圖片 Blob 建立並附加新的 InlineImage

圖片會包含在新的 Paragraph 中。

參數

名稱類型說明
imageBlobSource圖片資料。

回攻員

InlineImage:附加的圖片。

授權

使用這個方法的指令碼需要一或多個下列範圍的授權:

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

appendImage(image)

附加指定的 InlineImage

InlineImage 會包含在新的 Paragraph 中。

附加現有 InlineImage 的副本時,請使用這個版本的 appendImage

參數

名稱類型說明
imageInlineImage圖片資料。

回攻員

InlineImage:附加的圖片。

授權

使用這個方法的指令碼需要一或多個下列範圍的授權:

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

appendListItem(listItem)

附加指定的 ListItem

附加現有 ListItem 的副本時,請使用這個版本的 appendListItem

參數

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

appendPageBreak()

建立並附加新的 PageBreak

PageBreak 會包含在新的 Paragraph 中。

回攻員

PageBreak:新的分頁符號。

授權

使用這個方法的指令碼需要一或多個下列範圍的授權:

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

appendPageBreak(pageBreak)

附加指定的 PageBreak

PageBreak 會包含在新的 Paragraph 中。

附加現有 PageBreak 的副本時,請使用這個版本的 appendPageBreak

參數

名稱類型說明
pageBreakPageBreak要附加的分頁符號。

回攻員

PageBreak - 附加的分頁符號。

授權

使用這個方法的指令碼需要一或多個下列範圍的授權:

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

appendParagraph(paragraph)

附加指定的 Paragraph

附加現有 Paragraph 的副本時,請使用這個版本的 appendParagraph

參數

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

由於 Google 文件不能以表格結尾,因此這個方法也會在表格後方附加空白段落。

回攻員

Table:新資料表。

授權

使用這個方法的指令碼需要一或多個下列範圍的授權:

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

appendTable(cells)

為每個指定的字串值附加新的 Table,其中包含 TableCell

由於 Google 文件不能以表格結尾,因此這個方法也會在表格後方附加空白段落。

參數

名稱類型說明
cellsString[][]要新增至新表格的表格儲存格文字內容。

回攻員

Table:附加的資料表。

授權

使用這個方法的指令碼需要一或多個下列範圍的授權:

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

appendTable(table)

附加指定的 Table

附加現有 Table 的副本時,請使用這個版本的 appendTable。 由於 Google 文件不能以表格結尾,因此這個方法也會在表格後方附加空白段落。

參數

名稱類型說明
tableTable要附加的表格。

回攻員

Table:附加的資料表。

授權

使用這個方法的指令碼需要一或多個下列範圍的授權:

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

clear()

清除元素內容。

回攻員

Body:目前的元素。


copy()

傳回目前元素的深層副本,且該副本已分離。

元素中的任何子項元素也會一併複製。新元素沒有父項。

回攻員

Body:新副本。

授權

使用這個方法的指令碼需要一或多個下列範圍的授權:

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

editAsText()

取得目前元素的 Text 版本,以供編輯。

使用 editAsText 將元素內容當做 RTF 文字來操控。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:搜尋結果,指出搜尋文字的位置;如果沒有相符項目,則為空值

授權

使用這個方法的指令碼需要一或多個下列範圍的授權:

  • 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

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

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

getHeadingAttributes(paragraphHeading)

擷取所提供 ParagraphHeading 的屬性集。

參數

名稱類型說明
paragraphHeadingParagraphHeading要擷取屬性的標題。

回攻員

Object:屬性及其目前值的對應。

授權

使用這個方法的指令碼需要一或多個下列範圍的授權:

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

getImages()

擷取該節中包含的所有 InlineImages

回攻員

InlineImage[]|null:區段圖片。

授權

使用這個方法的指令碼需要一或多個下列範圍的授權:

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

getListItems()

擷取該節中包含的所有 ListItems

回攻員

ListItem[]|null:區段清單項目。

授權

使用這個方法的指令碼需要一或多個下列範圍的授權:

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

getMarginBottom()

以點為單位擷取下邊界。

回攻員

Number|null:下邊界 (以點為單位)。

授權

使用這個方法的指令碼需要一或多個下列範圍的授權:

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

getMarginLeft()

以點為單位擷取左邊界。

回攻員

Number|null:左邊界 (以點為單位)。

授權

使用這個方法的指令碼需要一或多個下列範圍的授權:

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

getMarginRight()

擷取右邊界。

回攻員

Number|null:右邊界 (以點為單位)。

授權

使用這個方法的指令碼需要一或多個下列範圍的授權:

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

getMarginTop()

擷取上邊界。

回攻員

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

getPageHeight()

以點為單位擷取頁面高度。

回攻員

Number|null:頁面高度 (以點為單位)。

授權

使用這個方法的指令碼需要一或多個下列範圍的授權:

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

getPageWidth()

以點為單位擷取頁面寬度。

回攻員

Number|null:頁面寬度 (以點為單位)。

授權

使用這個方法的指令碼需要一或多個下列範圍的授權:

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

getParagraphs()

擷取區段中包含的所有 Paragraphs (包括 ListItems)。

回攻員

Paragraph[]|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

getTables()

擷取該節中包含的所有 Tables

回攻員

Table[]|null:章節表格。

授權

使用這個方法的指令碼需要一或多個下列範圍的授權:

  • 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

insertHorizontalRule(childIndex)

在指定索引建立並插入新的 HorizontalRule

HorizontalRule 會包含在新的 Paragraph 中。

參數

名稱類型說明
childIndexInteger要插入元素的索引。

回攻員

HorizontalRule:新的水平線。

授權

使用這個方法的指令碼需要一或多個下列範圍的授權:

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

insertImage(childIndex, image)

在指定索引處,從指定圖片 Blob 建立並插入 InlineImage

參數

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

insertPageBreak(childIndex)

在指定索引建立並插入新的 PageBreak

PageBreak 會包含在新的 Paragraph 中。

參數

名稱類型說明
childIndexInteger要插入元素的索引。

回攻員

PageBreak:新的分頁符號。

授權

使用這個方法的指令碼需要一或多個下列範圍的授權:

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

insertPageBreak(childIndex, pageBreak)

在指定索引處插入指定 PageBreak

PageBreak 會包含在新的 Paragraph 中。

參數

名稱類型說明
childIndexInteger要插入元素的索引。
pageBreakPageBreak要插入的分頁符號。

回攻員

PageBreak:插入的分頁符號。

授權

使用這個方法的指令碼需要一或多個下列範圍的授權:

  • 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

removeChild(child)

移除指定的子項元素。

參數

名稱類型說明
childElement要移除的子元素。

回攻員

Body:目前的元素。

授權

使用這個方法的指令碼需要一或多個下列範圍的授權:

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

設定元素的屬性。

指定的屬性參數必須是物件,其中每個屬性名稱都是 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元素的屬性。

回攻員

Body:目前的元素。

授權

使用這個方法的指令碼需要一或多個下列範圍的授權:

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

setHeadingAttributes(paragraphHeading, attributes)

為提供的 ParagraphHeading 設定屬性。

參數

名稱類型說明
paragraphHeadingParagraphHeading要設定屬性的標題。
attributesObject屬性及其設定值的對應。

回攻員

Body:目前的元素。

授權

使用這個方法的指令碼需要一或多個下列範圍的授權:

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

setMarginBottom(marginBottom)

以點為單位設定下邊界。

參數

名稱類型說明
marginBottomNumber下邊界 (以點為單位)。

回攻員

Body:目前的元素。

授權

使用這個方法的指令碼需要一或多個下列範圍的授權:

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

setMarginLeft(marginLeft)

以點為單位設定左邊界。

參數

名稱類型說明
marginLeftNumber以點為單位的左邊界。

回攻員

Body:目前的元素。

授權

使用這個方法的指令碼需要一或多個下列範圍的授權:

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

setMarginRight(marginRight)

以點為單位設定右邊界。

參數

名稱類型說明
marginRightNumber右邊界。

回攻員

Body:目前的元素。

授權

使用這個方法的指令碼需要一或多個下列範圍的授權:

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

setMarginTop(marginTop)

設定上邊界。

參數

名稱類型說明
marginTopNumber上邊界 (以點為單位)。

回攻員

Body:目前的元素。

授權

使用這個方法的指令碼需要一或多個下列範圍的授權:

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

setPageHeight(pageHeight)

以點為單位設定頁面高度。

參數

名稱類型說明
pageHeightNumber頁面高度 (以點為單位)。

回攻員

Body:目前的元素。

授權

使用這個方法的指令碼需要一或多個下列範圍的授權:

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

setPageWidth(pageWidth)

以點為單位設定頁面寬度。

參數

名稱類型說明
pageWidthNumber頁面寬度 (以點為單位)。

回攻員

Body:目前的元素。

授權

使用這個方法的指令碼需要一或多個下列範圍的授權:

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

setText(text)

將內容設為純文字。

注意:現有內容會遭到清除。

參數

名稱類型說明
textString新文字內容。

回攻員

Body:目前的元素。

授權

使用這個方法的指令碼需要一或多個下列範圍的授權:

  • 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要套用的文字對齊類型

回攻員

Body:目前元素

授權

使用這個方法的指令碼需要一或多個下列範圍的授權:

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

已淘汰的方法