代表文件內文的元素。Body
可能包含 ListItem
、Paragraph
、Table
和 TableOfContents
元素。如要進一步瞭解文件結構,請參閱擴充 Google 文件指南。
Body
通常包含完整的文件內容,但 HeaderSection
、FooterSection
和任何 FootnoteSection
元素除外。
var doc = DocumentApp.getActiveDocument(); var body = doc.getBody(); // Append a paragraph and a page break to the document body section directly. body.appendParagraph("A paragraph."); body.appendPageBreak();
方法
內容詳盡的說明文件
appendHorizontalRule()
建立並附加新的 HorizontalRule
。
HorizontalRule
將納入新的 Paragraph
中。
回攻員
HorizontalRule
:新的水平規則
授權
使用這個方法的指令碼必須取得以下一或多個範圍的授權:
-
https://www.googleapis.com/auth/documents.currentonly
-
https://www.googleapis.com/auth/documents
appendImage(image)
從指定映像檔 blob 建立及附加新的 InlineImage
。
圖片將包含在新的 Paragraph
中。
參數
名稱 | 類型 | 說明 |
---|---|---|
image | BlobSource | 圖片資料 |
回攻員
InlineImage
:附加的圖片
授權
使用這個方法的指令碼必須取得以下一或多個範圍的授權:
-
https://www.googleapis.com/auth/documents.currentonly
-
https://www.googleapis.com/auth/documents
appendImage(image)
附加指定的 InlineImage
。
InlineImage
會包含在新的 Paragraph
中。
當您附加現有 InlineImage
的副本時,請使用這個版本的 appendImage
。
參數
名稱 | 類型 | 說明 |
---|---|---|
image | InlineImage | 圖片資料 |
回攻員
InlineImage
:附加的圖片
授權
使用這個方法的指令碼必須取得以下一或多個範圍的授權:
-
https://www.googleapis.com/auth/documents.currentonly
-
https://www.googleapis.com/auth/documents
appendListItem(listItem)
appendListItem(text)
appendPageBreak()
appendPageBreak(pageBreak)
appendParagraph(paragraph)
appendParagraph(text)
appendTable()
appendTable(cells)
appendTable(table)
clear()
copy()
editAsText()
取得目前元素的 Text
版本,以供編輯。
使用 editAsText
將元素內容視為 RTF 格式。editAsText
模式會忽略非文字元素 (例如 InlineImage
和 HorizontalRule
)。
已刪除文字範圍內的子項元素會從元素中移除。
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)
搜尋指定類型的子系元素內容。
參數
名稱 | 類型 | 說明 |
---|---|---|
elementType | ElementType | 要搜尋的元素類型 |
回攻員
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; } }
參數
名稱 | 類型 | 說明 |
---|---|---|
elementType | ElementType | 要搜尋的元素類型 |
from | RangeElement | 而要搜尋的內容 |
回攻員
RangeElement
:表示搜尋元素下一個位置的搜尋結果
授權
使用這個方法的指令碼必須取得以下一或多個範圍的授權:
-
https://www.googleapis.com/auth/documents.currentonly
-
https://www.googleapis.com/auth/documents
findText(searchPattern)
使用規則運算式搜尋元素內容中的指定文字模式。
未完全支援部分 JavaScript 規則運算式功能,例如擷取群組和模式修飾符。
提供的規則運算式模式會與目前元素中包含的每個文字區塊分開。
參數
名稱 | 類型 | 說明 |
---|---|---|
searchPattern | String | 要搜尋的模式 |
回攻員
RangeElement
:搜尋結果,指出搜尋文字的位置;如果沒有相符結果,則傳回空值
授權
使用這個方法的指令碼必須取得以下一或多個範圍的授權:
-
https://www.googleapis.com/auth/documents.currentonly
-
https://www.googleapis.com/auth/documents
findText(searchPattern, from)
從特定的搜尋結果開始,搜尋指定文字模式的元素內容。
未完全支援部分 JavaScript 規則運算式功能,例如擷取群組和模式修飾符。
提供的規則運算式模式會與目前元素中包含的每個文字區塊分開。
參數
名稱 | 類型 | 說明 |
---|---|---|
searchPattern | String | 要搜尋的模式 |
from | RangeElement | 而要搜尋的內容 |
回攻員
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
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."); }
參數
名稱 | 類型 | 說明 |
---|---|---|
childIndex | Integer | 要擷取的子元素索引 |
回攻員
Element
:指定索引處的子元素
授權
使用這個方法的指令碼必須取得以下一或多個範圍的授權:
-
https://www.googleapis.com/auth/documents.currentonly
-
https://www.googleapis.com/auth/documents
getChildIndex(child)
getHeadingAttributes(paragraphHeading)
擷取所提供 ParagraphHeading
的屬性組合。
參數
名稱 | 類型 | 說明 |
---|---|---|
paragraphHeading | ParagraphHeading | 要擷取其屬性的標題 |
回攻員
Object
:屬性及其目前值的對應
授權
使用這個方法的指令碼必須取得以下一或多個範圍的授權:
-
https://www.googleapis.com/auth/documents.currentonly
-
https://www.googleapis.com/auth/documents
getImages()
擷取區段包含的所有 InlineImages
。
回攻員
InlineImage[]
:區段圖片
授權
使用這個方法的指令碼必須取得以下一或多個範圍的授權:
-
https://www.googleapis.com/auth/documents.currentonly
-
https://www.googleapis.com/auth/documents
getListItems()
擷取區段包含的所有 ListItems
。
回攻員
ListItem[]
:區段清單項目
授權
使用這個方法的指令碼必須取得以下一或多個範圍的授權:
-
https://www.googleapis.com/auth/documents.currentonly
-
https://www.googleapis.com/auth/documents
getMarginBottom()
擷取點中的下邊界。
回攻員
Number
— 底部邊界,以點為單位
授權
使用這個方法的指令碼必須取得以下一或多個範圍的授權:
-
https://www.googleapis.com/auth/documents.currentonly
-
https://www.googleapis.com/auth/documents
getMarginLeft()
擷取點中的左側邊界。
回攻員
Number
— 左邊界,以點為單位
授權
使用這個方法的指令碼必須取得以下一或多個範圍的授權:
-
https://www.googleapis.com/auth/documents.currentonly
-
https://www.googleapis.com/auth/documents
getMarginRight()
擷取右側邊界。
回攻員
Number
— 右邊界,以點為單位
授權
使用這個方法的指令碼必須取得以下一或多個範圍的授權:
-
https://www.googleapis.com/auth/documents.currentonly
-
https://www.googleapis.com/auth/documents
getMarginTop()
擷取上邊界。
回攻員
Number
— 上邊界,以點為單位
授權
使用這個方法的指令碼必須取得以下一或多個範圍的授權:
-
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
getPageHeight()
以時間點擷取頁面高度。
回攻員
Number
:頁面高度,以點為單位
授權
使用這個方法的指令碼必須取得以下一或多個範圍的授權:
-
https://www.googleapis.com/auth/documents.currentonly
-
https://www.googleapis.com/auth/documents
getPageWidth()
以時間點擷取頁面寬度。
回攻員
Number
:頁面寬度,以點為單位
授權
使用這個方法的指令碼必須取得以下一或多個範圍的授權:
-
https://www.googleapis.com/auth/documents.currentonly
-
https://www.googleapis.com/auth/documents
getParagraphs()
擷取區段中包含的所有 Paragraphs
(包括 ListItems
)。
回攻員
Paragraph[]
:段落段落
授權
使用這個方法的指令碼必須取得以下一或多個範圍的授權:
-
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
getTables()
getText()
以文字字串擷取元素內容。
回攻員
String
:以文字字串表示元素的內容
授權
使用這個方法的指令碼必須取得以下一或多個範圍的授權:
-
https://www.googleapis.com/auth/documents.currentonly
-
https://www.googleapis.com/auth/documents
getTextAlignment()
取得文字對齊方式。可用的對齊類型包括 DocumentApp.TextAlignment.NORMAL
、DocumentApp.TextAlignment.SUBSCRIPT
和 DocumentApp.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
insertHorizontalRule(childIndex)
在指定的索引建立並插入新的 HorizontalRule
。
HorizontalRule
將納入新的 Paragraph
中。
參數
名稱 | 類型 | 說明 |
---|---|---|
childIndex | Integer | 要插入元素的索引 |
回攻員
HorizontalRule
:新的水平規則
授權
使用這個方法的指令碼必須取得以下一或多個範圍的授權:
-
https://www.googleapis.com/auth/documents.currentonly
-
https://www.googleapis.com/auth/documents
insertImage(childIndex, image)
在指定索引中建立並插入指定映像檔 blob 的 InlineImage
。
參數
名稱 | 類型 | 說明 |
---|---|---|
childIndex | Integer | 要插入元素的索引 |
image | BlobSource | 圖片資料 |
回攻員
InlineImage
:插入的內嵌圖片
授權
使用這個方法的指令碼必須取得以下一或多個範圍的授權:
-
https://www.googleapis.com/auth/documents.currentonly
-
https://www.googleapis.com/auth/documents
insertImage(childIndex, image)
在指定的索引處插入指定的 InlineImage
。
圖片將包含在新的 Paragraph
中。
參數
名稱 | 類型 | 說明 |
---|---|---|
childIndex | Integer | 要插入元素的索引 |
image | InlineImage | 要插入的圖片 |
回攻員
InlineImage
:插入的內嵌圖片
授權
使用這個方法的指令碼必須取得以下一或多個範圍的授權:
-
https://www.googleapis.com/auth/documents.currentonly
-
https://www.googleapis.com/auth/documents
insertListItem(childIndex, listItem)
insertListItem(childIndex, text)
insertPageBreak(childIndex)
insertPageBreak(childIndex, pageBreak)
insertParagraph(childIndex, paragraph)
insertParagraph(childIndex, text)
insertTable(childIndex)
insertTable(childIndex, cells)
insertTable(childIndex, table)
removeChild(child)
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");
參數
名稱 | 類型 | 說明 |
---|---|---|
searchPattern | String | 要搜尋的規則運算式模式 |
replacement | String | 要替換的文字 |
回攻員
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);
參數
名稱 | 類型 | 說明 |
---|---|---|
attributes | Object | 元素的屬性。 |
回攻員
Body
- 目前的元素。
授權
使用這個方法的指令碼必須取得以下一或多個範圍的授權:
-
https://www.googleapis.com/auth/documents.currentonly
-
https://www.googleapis.com/auth/documents
setHeadingAttributes(paragraphHeading, attributes)
設定所提供 ParagraphHeading
的屬性。
參數
名稱 | 類型 | 說明 |
---|---|---|
paragraphHeading | ParagraphHeading | 要設定其屬性的標題 |
attributes | Object | 對應屬性及要設定的值 |
回攻員
Body
:目前的元素
授權
使用這個方法的指令碼必須取得以下一或多個範圍的授權:
-
https://www.googleapis.com/auth/documents.currentonly
-
https://www.googleapis.com/auth/documents
setMarginBottom(marginBottom)
setMarginLeft(marginLeft)
setMarginRight(marginRight)
setMarginTop(marginTop)
setPageHeight(pageHeight)
setPageWidth(pageWidth)
setText(text)
setTextAlignment(textAlignment)
設定文字對齊方式。可用的對齊類型包括 DocumentApp.TextAlignment.NORMAL
、DocumentApp.TextAlignment.SUBSCRIPT
和 DocumentApp.TextAlignment.SUPERSCRIPT
。
// Make the entire first paragraph be superscript. var text = DocumentApp.getActiveDocument().getBody().getParagraphs()[0].editAsText(); text.setTextAlignment(DocumentApp.TextAlignment.SUPERSCRIPT);
參數
名稱 | 類型 | 說明 |
---|---|---|
textAlignment | TextAlignment | 要套用的文字對齊類型 |
回攻員
Body
:目前的元素
授權
使用這個方法的指令碼必須取得以下一或多個範圍的授權:
-
https://www.googleapis.com/auth/documents.currentonly
-
https://www.googleapis.com/auth/documents