Class ContainerElement

ContainerElement

可能包含其他元素的一般元素。包含子項元素的所有元素 (例如 Paragraph) 都會沿用 ContainerElement 的內容。

方法

方法傳回類型簡短說明
asBody()Body將目前元素做為 Body 傳回。
asEquation()EquationEquation 傳回目前的元素。
asFooterSection()FooterSection將目前元素做為 FooterSection 傳回。
asFootnoteSection()FootnoteSection將目前元素做為 FootnoteSection 傳回。
asHeaderSection()HeaderSection將目前元素做為 HeaderSection 傳回。
asListItem()ListItem將目前元素做為 ListItem 傳回。
asParagraph()Paragraph將目前元素做為 Paragraph 傳回。
asTable()Table將目前元素做為 Table 傳回。
asTableCell()TableCell將目前元素做為 TableCell 傳回。
asTableOfContents()TableOfContents將目前元素做為 TableOfContents 傳回。
asTableRow()TableRow將目前元素做為 TableRow 傳回。
clear()ContainerElement清除元素的內容。
copy()ContainerElement傳回目前元素的卸離深層連結。
editAsText()Text取得目前元素的 Text 版本以進行編輯。
findElement(elementType)RangeElement搜尋元素內容,尋找指定類型子系。
findElement(elementType, from)RangeElement從指定內容的 RangeElement 開始搜尋元素內容的子系。
findText(searchPattern)RangeElement使用規則運算式搜尋元素中的文字內容。
findText(searchPattern, from)RangeElement從指定搜尋結果中,搜尋指定文字模式的元素內容。
getAttributes()Object擷取元素的屬性。
getChild(childIndex)Element擷取指定子項索引的子元素。
getChildIndex(child)Integer擷取指定子元素的子索引。
getLinkUrl()String擷取連結網址。
getNextSibling()Element擷取元素和下一個下一個同層級元素。
getNumChildren()Integer擷取子項數量。
getParent()ContainerElement擷取元素的父項元素。
getPreviousSibling()Element擷取元素的先前同層級元素。
getText()String以文字字串的形式擷取元素內容。
getTextAlignment()TextAlignment取得文字對齊方式。
getType()ElementType擷取元素的 ElementType
isAtDocumentEnd()Boolean判斷元素是否位於 Document 的結尾。
merge()ContainerElement將元素與前相同類型的同層級合併。
removeFromParent()ContainerElement從父項中移除元素。
replaceText(searchPattern, replacement)Element使用規則運算式將特定文字模式的所有出現情況替換為特定替代字串。
setAttributes(attributes)ContainerElement設定元素的屬性。
setLinkUrl(url)ContainerElement設定連結網址。
setTextAlignment(textAlignment)ContainerElement設定文字對齊方式。

內容詳盡的說明文件

asBody()

將目前元素做為 Body 傳回。

如果特定元素屬於特定類型的類型,請使用這個方法,協助自動完成功能。

Return 鍵

Body — 目前的元素。

授權

如果指令碼使用這個方法,就必須符合下列一或多個範圍的授權:

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

asEquation()

將目前元素做為 Equation 傳回。

如果特定元素屬於特定類型的類型,請使用這個方法,協助自動完成功能。

Return 鍵

Equation — 目前的元素。

授權

如果指令碼使用這個方法,就必須符合下列一或多個範圍的授權:

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

asFooterSection()

將目前元素做為 FooterSection 傳回。

如果特定元素屬於特定類型的類型,請使用這個方法,協助自動完成功能。

Return 鍵

FooterSection — 目前的元素。

授權

如果指令碼使用這個方法,就必須符合下列一或多個範圍的授權:

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

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

asListItem()

將目前元素做為 ListItem 傳回。

如果特定元素屬於特定類型的類型,請使用這個方法,協助自動完成功能。

Return 鍵

ListItem — 目前的元素。

授權

如果指令碼使用這個方法,就必須符合下列一或多個範圍的授權:

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

asParagraph()

將目前元素做為 Paragraph 傳回。

如果特定元素屬於特定類型的類型,請使用這個方法,協助自動完成功能。

Return 鍵

Paragraph — 目前的元素。

授權

如果指令碼使用這個方法,就必須符合下列一或多個範圍的授權:

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

asTable()

將目前元素做為 Table 傳回。

如果特定元素屬於特定類型的類型,請使用這個方法,協助自動完成功能。

Return 鍵

Table — 目前的元素。

授權

如果指令碼使用這個方法,就必須符合下列一或多個範圍的授權:

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

asTableCell()

將目前元素做為 TableCell 傳回。

如果特定元素屬於特定類型的類型,請使用這個方法,協助自動完成功能。

Return 鍵

TableCell — 目前的元素。

授權

如果指令碼使用這個方法,就必須符合下列一或多個範圍的授權:

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

asTableOfContents()

將目前元素做為 TableOfContents 傳回。

如果特定元素屬於特定類型的類型,請使用這個方法,協助自動完成功能。

Return 鍵

TableOfContents — 目前的元素。

授權

如果指令碼使用這個方法,就必須符合下列一或多個範圍的授權:

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

asTableRow()

將目前元素做為 TableRow 傳回。

如果特定元素屬於特定類型的類型,請使用這個方法,協助自動完成功能。

Return 鍵

TableRow — 目前的元素。

授權

如果指令碼使用這個方法,就必須符合下列一或多個範圍的授權:

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

clear()

清除元素的內容。

Return 鍵

ContainerElement:目前的元素


copy()

傳回目前元素的卸離深層連結。

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

Return 鍵

ContainerElement — 新副本。

授權

如果指令碼使用這個方法,就必須符合下列一或多個範圍的授權:

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

Return 鍵

Text:目前元素的文字版本


findElement(elementType)

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

參數

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

Return 鍵

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要搜尋的結果

Return 鍵

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

授權

如果指令碼使用這個方法,就必須符合下列一或多個範圍的授權:

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

findText(searchPattern)

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

無法完整支援 JavaScript 規則運算式的子集,例如擷取群組和模式修飾符。

提供的規則運算式模式與目前元素所包含的每個文字區塊各自獨立。

參數

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

Return 鍵

RangeElement:代表搜尋文字位置的搜尋結果,如果沒有比對結果,則傳回空值

授權

如果指令碼使用這個方法,就必須符合下列一或多個範圍的授權:

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

findText(searchPattern, from)

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

無法完整支援 JavaScript 規則運算式的子集,例如擷取群組和模式修飾符。

提供的規則運算式模式與目前元素所包含的每個文字區塊各自獨立。

參數

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

Return 鍵

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

Return 鍵

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

參數

名稱類型說明
childIndexInteger要擷取的子元素索引

Return 鍵

Element:指定索引的子元素

授權

如果指令碼使用這個方法,就必須符合下列一或多個範圍的授權:

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

getChildIndex(child)

擷取指定子元素的子索引。

參數

名稱類型說明
childElement擷取索引的子元素

Return 鍵

Integer:子索引

授權

如果指令碼使用這個方法,就必須符合下列一或多個範圍的授權:

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

getLinkUrl()

擷取連結網址。

Return 鍵

String:連結網址,如果元素包含多個屬性,則傳回空值

授權

如果指令碼使用這個方法,就必須符合下列一或多個範圍的授權:

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

getNextSibling()

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

下一個同層級的父項相同,並沿用目前的元素。

Return 鍵

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

Return 鍵

Integer:子項數量

授權

如果指令碼使用這個方法,就必須符合下列一或多個範圍的授權:

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

getParent()

擷取元素的父項元素。

父項元素包含目前元素。

Return 鍵

ContainerElement:父項元素。

授權

如果指令碼使用這個方法,就必須符合下列一或多個範圍的授權:

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

getPreviousSibling()

擷取該元素之前的同層級元素。

上一個同層級的父項相同,並加上目前元素。

Return 鍵

Element:上一個同層級元素。

授權

如果指令碼使用這個方法,就必須符合下列一或多個範圍的授權:

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

getText()

以文字字串的形式擷取元素內容。

Return 鍵

String:元素內容,以文字字串表示

授權

如果指令碼使用這個方法,就必須符合下列一或多個範圍的授權:

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

getTextAlignment()

取得文字對齊方式。可用的對齊類型為 DocumentApp.TextAlignment.NORMALDocumentApp.TextAlignment.SUBSCRIPTDocumentApp.TextAlignment.SUPERSCRIPT

Return 鍵

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

Return 鍵

ElementType:元素類型,

授權

如果指令碼使用這個方法,就必須符合下列一或多個範圍的授權:

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

isAtDocumentEnd()

判斷元素是否位於 Document 的結尾。

Return 鍵

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

Return 鍵

ContainerElement — 合併的元素。

授權

如果指令碼使用這個方法,就必須符合下列一或多個範圍的授權:

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

ContainerElement — 移除的元素。

授權

如果指令碼使用這個方法,就必須符合下列一或多個範圍的授權:

  • 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要做為替換的文字

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

參數

名稱類型說明
attributesObject元素的屬性。

Return 鍵

ContainerElement — 目前的元素。

授權

如果指令碼使用這個方法,就必須符合下列一或多個範圍的授權:

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

setLinkUrl(url)

設定連結網址。

參數

名稱類型說明
urlString連結網址

Return 鍵

ContainerElement:目前的元素

授權

如果指令碼使用這個方法,就必須符合下列一或多個範圍的授權:

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

Return 鍵

ContainerElement:目前的元素

授權

如果指令碼使用這個方法,就必須符合下列一或多個範圍的授權:

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