Interface Element

元素

一般元素。Document 內容會以元素表示。舉例來說,ListItemParagraphTable 都是元素,會沿用 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代表 RTF 區域的元素。
UnsupportedElement代表未知區域的元素,或無法透過指令碼影響的區域,例如頁碼。

方法

方法傳回類型簡短說明
asBody()Body將目前元素傳回為 Body
asDate()Date將目前元素傳回為 Date
asEquation()Equation將目前元素傳回為 Equation
asEquationFunction()EquationFunction將目前元素傳回為 EquationFunction
asEquationFunctionArgumentSeparator()EquationFunctionArgumentSeparator將目前元素傳回為 EquationFunctionArgumentSeparator
asEquationSymbol()EquationSymbol將目前元素傳回為 EquationSymbol
asFooterSection()FooterSection將目前元素傳回為 FooterSection
asFootnote()Footnote將目前元素傳回為 Footnote
asFootnoteSection()FootnoteSection將目前元素傳回為 FootnoteSection
asHeaderSection()HeaderSection將目前元素傳回為 HeaderSection
asHorizontalRule()HorizontalRule將目前元素傳回為 HorizontalRule
asInlineDrawing()InlineDrawing將目前元素傳回為 InlineDrawing
asInlineImage()InlineImage將目前元素傳回為 InlineImage
asListItem()ListItem將目前元素傳回為 ListItem
asPageBreak()PageBreak將目前元素傳回為 PageBreak
asParagraph()Paragraph將目前元素傳回為 Paragraph
asPerson()Person將目前元素傳回為 Person
asRichLink()RichLink會以 RichLink 的形式傳回目前元素,例如 Google 試算表檔案的連結。
asTable()Table將目前元素傳回為 Table
asTableCell()TableCell將目前元素傳回為 TableCell
asTableOfContents()TableOfContents將目前元素傳回為 TableOfContents
asTableRow()TableRow將目前元素傳回為 TableRow
asText()Text將目前元素傳回為 Text
copy()Element傳回目前元素的卸離的深度副本。
getAttributes()Object擷取元素的屬性。
getNextSibling()Element擷取元素的下一個同層級元素。
getParent()ContainerElement擷取元素的父項元素。
getPreviousSibling()Element擷取元素先前的同層級元素。
getType()ElementType擷取元素的 ElementType
isAtDocumentEnd()Boolean判斷元素是否位於 Document 的結尾。
merge()Element將元素與下一個同類型的同層級合併。
removeFromParent()Element從父項元素中移除元素。
setAttributes(attributes)Element設定元素的屬性。

內容詳盡的說明文件

asBody()

將目前元素傳回為 Body

只要使用這個方法,就能在已知特定元素是特定類型時自動完成。

回攻員

Body - 目前的元素。

授權

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

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

asDate()

將目前元素傳回為 Date

如果您知道元素是 Date,請使用此方法將其類型設為 Date。這樣做可讓 Apps Script 編輯器中的自動完成功能顯示可搭配 Date 使用的方法。

回攻員

Date:目前元素的類型設為 Date

授權

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

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

asEquation()

將目前元素傳回為 Equation

只要使用這個方法,就能在已知特定元素是特定類型時自動完成。

回攻員

Equation - 目前的元素。

授權

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

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

asEquationFunction()

將目前元素傳回為 EquationFunction

只要使用這個方法,就能在已知特定元素是特定類型時自動完成。

回攻員

EquationFunction - 目前的元素。

授權

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

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

asEquationFunctionArgumentSeparator()

將目前元素傳回為 EquationFunctionArgumentSeparator

只要使用這個方法,就能在已知特定元素是特定類型時自動完成。

回攻員

EquationFunctionArgumentSeparator - 目前的元素。

授權

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

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

asEquationSymbol()

將目前元素傳回為 EquationSymbol

只要使用這個方法,就能在已知特定元素是特定類型時自動完成。

回攻員

EquationSymbol - 目前的元素。

授權

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

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

asFooterSection()

將目前元素傳回為 FooterSection

只要使用這個方法,就能在已知特定元素是特定類型時自動完成。

回攻員

FooterSection - 目前的元素。

授權

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

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

asFootnote()

將目前元素傳回為 Footnote

只要使用這個方法,就能在已知特定元素是特定類型時自動完成。

回攻員

Footnote - 目前的元素。

授權

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

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

asFootnoteSection()

將目前元素傳回為 FootnoteSection

只要使用這個方法,就能在已知特定元素是特定類型時自動完成。

回攻員

FootnoteSection - 目前的元素。

授權

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

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

asHeaderSection()

將目前元素傳回為 HeaderSection

只要使用這個方法,就能在已知特定元素是特定類型時自動完成。

回攻員

HeaderSection - 目前的元素。

授權

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

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

asHorizontalRule()

將目前元素傳回為 HorizontalRule

只要使用這個方法,就能在已知特定元素是特定類型時自動完成。

回攻員

HorizontalRule - 目前的元素。

授權

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

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

asInlineDrawing()

將目前元素傳回為 InlineDrawing

只要使用這個方法,就能在已知特定元素是特定類型時自動完成。

回攻員

InlineDrawing - 目前的元素。

授權

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

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

asInlineImage()

將目前元素傳回為 InlineImage

只要使用這個方法,就能在已知特定元素是特定類型時自動完成。

回攻員

InlineImage - 目前的元素。

授權

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

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

asListItem()

將目前元素傳回為 ListItem

只要使用這個方法,就能在已知特定元素是特定類型時自動完成。

回攻員

ListItem - 目前的元素。

授權

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

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

asPageBreak()

將目前元素傳回為 PageBreak

只要使用這個方法,就能在已知特定元素是特定類型時自動完成。

回攻員

PageBreak - 目前的元素。

授權

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

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

asParagraph()

將目前元素傳回為 Paragraph

只要使用這個方法,就能在已知特定元素是特定類型時自動完成。

回攻員

Paragraph - 目前的元素。

授權

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

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

asPerson()

將目前元素傳回為 Person

如果您知道元素是 Person,請使用此方法將類型設為使用者。這麼做可讓 Apps Script 編輯器的自動完成功能顯示可搭配使用者元素使用的方法。

回攻員

Person:目前元素的類型設為 Person

授權

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

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

RichLink 的形式傳回目前元素,例如 Google 試算表檔案的連結。

如果您知道元素是 RichLink,請使用此方法將其類型設為 RichLink。這樣做可讓 Apps Script 編輯器中的自動完成功能顯示可搭配 RichLink 使用的方法。

回攻員

RichLink:目前元素的類型設為 RichLink

授權

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

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

asTable()

將目前元素傳回為 Table

只要使用這個方法,就能在已知特定元素是特定類型時自動完成。

回攻員

Table - 目前的元素。

授權

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

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

asTableCell()

將目前元素傳回為 TableCell

只要使用這個方法,就能在已知特定元素是特定類型時自動完成。

回攻員

TableCell - 目前的元素。

授權

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

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

asTableOfContents()

將目前元素傳回為 TableOfContents

只要使用這個方法,就能在已知特定元素是特定類型時自動完成。

回攻員

TableOfContents - 目前的元素。

授權

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

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

asTableRow()

將目前元素傳回為 TableRow

只要使用這個方法,就能在已知特定元素是特定類型時自動完成。

回攻員

TableRow - 目前的元素。

授權

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

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

asText()

將目前元素傳回為 Text

只要使用這個方法,就能在已知特定元素是特定類型時自動完成。

回攻員

Text - 目前的元素。

授權

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

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

copy()

傳回目前元素的卸離的深層副本。

系統也會複製元素中顯示的所有子元素。新元素沒有父項。

回攻員

Element:新副本。

授權

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

  • 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

getNextSibling()

擷取元素的下一個同層級元素。

下一個同層級具有相同的父項,並緊跟目前元素。

回攻員

Element — 下一個同層級元素。

授權

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

  • 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

getPreviousSibling()

擷取元素先前的同層級元素。

上一個同層級具有相同的父項,位於目前元素的前面。

回攻員

Element — 上一個同層級元素。

授權

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

  • 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

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

回攻員

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

回攻員

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元素的屬性。

回攻員

Element - 目前的元素。

授權

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

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