一般元素。Document
內容以元素表示。舉例來說,ListItem
、Paragraph
和 Table
都是元素,並沿用 Element
定義的所有方法,例如 getType()
。
實作類別
名稱 | 簡短說明 |
---|---|
Body | 代表文件內文的元素。 |
ContainerElement | 可能包含其他元素的一般元素。 |
Date | 代表格式化日期的元素 |
Equation | 代表數學運算式的元素。 |
EquationFunction | 代表數學 Equation 中函式的元素。 |
EquationFunctionArgumentSeparator | 代表數學 Equation 中函式分隔符的元素。 |
EquationSymbol | 代表數學 Equation 中符號的元素。 |
FooterSection | 代表頁尾區段的元素。 |
Footnote | 代表註腳的元素。 |
FootnoteSection | 代表註腳區段的元素。 |
HeaderSection | 代表標題區段的元素。 |
HorizontalRule | 代表水平規則的元素。 |
InlineDrawing | 代表內嵌繪圖的元素。 |
InlineImage | 代表嵌入圖片的元素。 |
ListItem | 代表清單項目的元素。 |
PageBreak | 代表分頁符號的元素。 |
Paragraph | 代表段落的元素。 |
Person | 代表人物連結的元素。 |
RichLink | 代表 Google 資源連結的元素,例如雲端硬碟檔案或 YouTube 影片。 |
Table | 代表資料表的元素。 |
TableCell | 代表表格儲存格的元素。 |
TableOfContents | 包含目錄的元素。 |
TableRow | 代表表格列的元素。 |
Text | 代表複合式文字區域的元素。 |
UnsupportedElement | 代表某個區域不明或無法受到指令碼影響的元素,例如頁碼。 |
方法
內容詳盡的說明文件
asBody()
asDate()
asEquation()
asEquationFunction()
將目前元素做為 EquationFunction
傳回。
如果特定元素屬於特定類型的類型,請使用這個方法,協助自動完成功能。
Return 鍵
EquationFunction
— 目前的元素。
授權
如果指令碼使用這個方法,就必須符合下列一或多個範圍的授權:
-
https://www.googleapis.com/auth/documents.currentonly
-
https://www.googleapis.com/auth/documents
asEquationFunctionArgumentSeparator()
將目前元素做為 EquationFunctionArgumentSeparator
傳回。
如果特定元素屬於特定類型的類型,請使用這個方法,協助自動完成功能。
Return 鍵
EquationFunctionArgumentSeparator
— 目前的元素。
授權
如果指令碼使用這個方法,就必須符合下列一或多個範圍的授權:
-
https://www.googleapis.com/auth/documents.currentonly
-
https://www.googleapis.com/auth/documents
asEquationSymbol()
將目前元素做為 EquationSymbol
傳回。
如果特定元素屬於特定類型的類型,請使用這個方法,協助自動完成功能。
Return 鍵
EquationSymbol
— 目前的元素。
授權
如果指令碼使用這個方法,就必須符合下列一或多個範圍的授權:
-
https://www.googleapis.com/auth/documents.currentonly
-
https://www.googleapis.com/auth/documents
asFootnote()
asFootnoteSection()
將目前元素做為 FootnoteSection
傳回。
如果特定元素屬於特定類型的類型,請使用這個方法,協助自動完成功能。
Return 鍵
FootnoteSection
— 目前的元素。
授權
如果指令碼使用這個方法,就必須符合下列一或多個範圍的授權:
-
https://www.googleapis.com/auth/documents.currentonly
-
https://www.googleapis.com/auth/documents
asHeaderSection()
將目前元素做為 HeaderSection
傳回。
如果特定元素屬於特定類型的類型,請使用這個方法,協助自動完成功能。
Return 鍵
HeaderSection
— 目前的元素。
授權
如果指令碼使用這個方法,就必須符合下列一或多個範圍的授權:
-
https://www.googleapis.com/auth/documents.currentonly
-
https://www.googleapis.com/auth/documents
asHorizontalRule()
將目前元素做為 HorizontalRule
傳回。
如果特定元素屬於特定類型的類型,請使用這個方法,協助自動完成功能。
Return 鍵
HorizontalRule
— 目前的元素。
授權
如果指令碼使用這個方法,就必須符合下列一或多個範圍的授權:
-
https://www.googleapis.com/auth/documents.currentonly
-
https://www.googleapis.com/auth/documents
asInlineDrawing()
將目前元素做為 InlineDrawing
傳回。
如果特定元素屬於特定類型的類型,請使用這個方法,協助自動完成功能。
Return 鍵
InlineDrawing
— 目前的元素。
授權
如果指令碼使用這個方法,就必須符合下列一或多個範圍的授權:
-
https://www.googleapis.com/auth/documents.currentonly
-
https://www.googleapis.com/auth/documents
asInlineImage()
將目前元素做為 InlineImage
傳回。
如果特定元素屬於特定類型的類型,請使用這個方法,協助自動完成功能。
Return 鍵
InlineImage
— 目前的元素。
授權
如果指令碼使用這個方法,就必須符合下列一或多個範圍的授權:
-
https://www.googleapis.com/auth/documents.currentonly
-
https://www.googleapis.com/auth/documents
asListItem()
asPageBreak()
asParagraph()
asPerson()
asRichLink()
asTable()
asTableCell()
asTableOfContents()
將目前元素做為 TableOfContents
傳回。
如果特定元素屬於特定類型的類型,請使用這個方法,協助自動完成功能。
Return 鍵
TableOfContents
— 目前的元素。
授權
如果指令碼使用這個方法,就必須符合下列一或多個範圍的授權:
-
https://www.googleapis.com/auth/documents.currentonly
-
https://www.googleapis.com/auth/documents
asTableRow()
asText()
copy()
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]); }
Return 鍵
Object
— 元素的屬性。
授權
如果指令碼使用這個方法,就必須符合下列一或多個範圍的授權:
-
https://www.googleapis.com/auth/documents.currentonly
-
https://www.googleapis.com/auth/documents
getNextSibling()
getParent()
擷取元素的父項元素。
父項元素包含目前元素。
Return 鍵
ContainerElement
:父項元素。
授權
如果指令碼使用這個方法,就必須符合下列一或多個範圍的授權:
-
https://www.googleapis.com/auth/documents.currentonly
-
https://www.googleapis.com/auth/documents
getPreviousSibling()
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.'); }
Return 鍵
ElementType
:元素類型,
授權
如果指令碼使用這個方法,就必須符合下列一或多個範圍的授權:
-
https://www.googleapis.com/auth/documents.currentonly
-
https://www.googleapis.com/auth/documents
isAtDocumentEnd()
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();
Return 鍵
Element
— 合併的元素。
授權
如果指令碼使用這個方法,就必須符合下列一或多個範圍的授權:
-
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(); }
Return 鍵
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 | 元素的屬性。 |
Return 鍵
Element
— 目前的元素。
授權
如果指令碼使用這個方法,就必須符合下列一或多個範圍的授權:
-
https://www.googleapis.com/auth/documents.currentonly
-
https://www.googleapis.com/auth/documents