代表表格儲存格的元素。Table 一律包含在 Table 中,且可能包含 List、Paragraph 或 Table 元素。如要進一步瞭解文件結構,請參閱擴充 Google 文件指南。
方法
內容詳盡的說明文件
append Horizontal Rule()
建立並附加新的 Horizontal。
Horizontal 會包含在新的 Paragraph 中。
回攻員
Horizontal:新的水平線
授權
使用這個方法的指令碼需要一或多個下列範圍的授權:
-
https://www.googleapis.com/auth/documents.currentonly -
https://www.googleapis.com/auth/documents
append Image(image)
從指定的圖片 Blob 建立並附加新的 Inline。
Inline 會包含在新的 Paragraph 中。
參數
| 名稱 | 類型 | 說明 |
|---|---|---|
image | Blob | 圖片資料 |
回攻員
Inline - 新圖片
授權
使用這個方法的指令碼需要一或多個下列範圍的授權:
-
https://www.googleapis.com/auth/documents.currentonly -
https://www.googleapis.com/auth/documents
append Image(image)
附加指定的 Inline。
Inline 會包含在新的 Paragraph 中。
參數
| 名稱 | 類型 | 說明 |
|---|---|---|
image | Inline | 圖片資料 |
回攻員
Inline:附加的圖片
授權
使用這個方法的指令碼需要一或多個下列範圍的授權:
-
https://www.googleapis.com/auth/documents.currentonly -
https://www.googleapis.com/auth/documents
append List Item(listItem)
append List Item(text)
append Paragraph(paragraph)
append Paragraph(text)
append Table()
append Table(cells)
append Table(table)
clear()
copy()
edit As Text()
取得目前元素的 Text 版本,以供編輯。
使用 edit 將元素內容當做 RTF 文字來操控。edit 模式會忽略非文字元素 (例如 Inline 和 Horizontal)。
如果子項元素完全包含在已刪除的文字範圍內,系統會從元素中移除這些子項元素。
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:目前元素的文字版本
find Element(elementType)
搜尋元素內容,找出指定型別的後代。
參數
| 名稱 | 類型 | 說明 |
|---|---|---|
element | Element | 要搜尋的元素類型。 |
回攻員
Range:搜尋結果,指出搜尋元素的所在位置。
授權
使用這個方法的指令碼需要一或多個下列範圍的授權:
-
https://www.googleapis.com/auth/documents.currentonly -
https://www.googleapis.com/auth/documents
find Element(elementType, from)
從指定的 Range 開始,在元素內容中搜尋指定型別的後代。
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; } }
參數
| 名稱 | 類型 | 說明 |
|---|---|---|
element | Element | 要搜尋的元素類型。 |
from | Range | 要從中搜尋的搜尋結果。 |
回攻員
Range:搜尋結果,指出搜尋元素的下一個位置。
授權
使用這個方法的指令碼需要一或多個下列範圍的授權:
-
https://www.googleapis.com/auth/documents.currentonly -
https://www.googleapis.com/auth/documents
find Text(searchPattern)
使用規則運算式在元素內容中搜尋指定的文字模式。
系統不完全支援部分 JavaScript 規則運算式功能,例如擷取群組和模式修飾符。
系統會針對目前元素中包含的每個文字區塊,分別比對所提供的規則運算式模式。
參數
| 名稱 | 類型 | 說明 |
|---|---|---|
search | String | 要搜尋的模式 |
回攻員
Range:搜尋結果,指出搜尋文字的位置;如果沒有相符項目,則為空值
授權
使用這個方法的指令碼需要一或多個下列範圍的授權:
-
https://www.googleapis.com/auth/documents.currentonly -
https://www.googleapis.com/auth/documents
find Text(searchPattern, from)
從指定的搜尋結果開始,在元素內容中搜尋特定文字模式。
系統不完全支援部分 JavaScript 規則運算式功能,例如擷取群組和模式修飾符。
系統會針對目前元素中包含的每個文字區塊,分別比對所提供的規則運算式模式。
參數
| 名稱 | 類型 | 說明 |
|---|---|---|
search | String | 要搜尋的模式 |
from | Range | 要從哪個搜尋結果開始搜尋 |
回攻員
Range:搜尋結果,指出搜尋文字的下一個位置;如果沒有相符項目,則為空值
授權
使用這個方法的指令碼需要一或多個下列範圍的授權:
-
https://www.googleapis.com/auth/documents.currentonly -
https://www.googleapis.com/auth/documents
get Attributes()
擷取元素的屬性。
結果會是物件,其中包含每個有效元素屬性的屬性,且每個屬性名稱都對應至 Document 列舉中的項目。
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
get Background Color()
擷取背景顏色。
回攻員
String|null:背景顏色,格式為 CSS 標記 (例如 '#ffffff')
授權
使用這個方法的指令碼需要一或多個下列範圍的授權:
-
https://www.googleapis.com/auth/documents.currentonly -
https://www.googleapis.com/auth/documents
get Child(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.'); }
參數
| 名稱 | 類型 | 說明 |
|---|---|---|
child | Integer | 要擷取的子項元素索引。 |
回攻員
Element:指定索引的子項元素。
授權
使用這個方法的指令碼需要一或多個下列範圍的授權:
-
https://www.googleapis.com/auth/documents.currentonly -
https://www.googleapis.com/auth/documents
get Child Index(child)
get Col Span()
擷取欄跨度,也就是這個儲存格跨越的資料表儲存格欄數。 未合併的儲存格的 col span 為 1。
回攻員
Integer:資料欄跨度,或 0 (如果儲存格是由另一個儲存格合併而來)
授權
使用這個方法的指令碼需要一或多個下列範圍的授權:
-
https://www.googleapis.com/auth/documents.currentonly -
https://www.googleapis.com/auth/documents
get Link Url()
擷取連結網址。
回攻員
String|null - 連結網址,如果元素包含這項屬性的多個值,則為空值
授權
使用這個方法的指令碼需要一或多個下列範圍的授權:
-
https://www.googleapis.com/auth/documents.currentonly -
https://www.googleapis.com/auth/documents
get Next Sibling()
get Num Children()
擷取子項數量。
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
get Padding Bottom()
以點為單位擷取底部邊框間距。
回攻員
Number|null:以點為單位的底部邊框間距
授權
使用這個方法的指令碼需要一或多個下列範圍的授權:
-
https://www.googleapis.com/auth/documents.currentonly -
https://www.googleapis.com/auth/documents
get Padding Left()
以點為單位擷取左側邊框間距。
回攻員
Number|null:左側邊框間距 (以點為單位)
授權
使用這個方法的指令碼需要一或多個下列範圍的授權:
-
https://www.googleapis.com/auth/documents.currentonly -
https://www.googleapis.com/auth/documents
get Padding Right()
以點為單位擷取右側邊框間距。
回攻員
Number|null:右側邊框間距 (以點為單位)
授權
使用這個方法的指令碼需要一或多個下列範圍的授權:
-
https://www.googleapis.com/auth/documents.currentonly -
https://www.googleapis.com/auth/documents
get Padding Top()
以點為單位擷取頂端邊框間距。
回攻員
Number|null:頂端邊框間距 (以點為單位)
授權
使用這個方法的指令碼需要一或多個下列範圍的授權:
-
https://www.googleapis.com/auth/documents.currentonly -
https://www.googleapis.com/auth/documents
get Parent()
擷取元素的父項元素。
父項元素包含目前的元素。
回攻員
Container:父項元素。
授權
使用這個方法的指令碼需要一或多個下列範圍的授權:
-
https://www.googleapis.com/auth/documents.currentonly -
https://www.googleapis.com/auth/documents
get Parent Row()
get Parent Table()
get Previous Sibling()
get Row Span()
擷取列跨度,也就是這個儲存格跨越的表格儲存格列數。未合併的儲存格的列跨度為 1。
回攻員
Integer:資料列範圍,或儲存格的 0 (如果該儲存格是由另一個儲存格合併而來)
授權
使用這個方法的指令碼需要一或多個下列範圍的授權:
-
https://www.googleapis.com/auth/documents.currentonly -
https://www.googleapis.com/auth/documents
get Text()
以文字字串形式擷取元素內容。
回攻員
String:元素內容 (文字字串)
授權
使用這個方法的指令碼需要一或多個下列範圍的授權:
-
https://www.googleapis.com/auth/documents.currentonly -
https://www.googleapis.com/auth/documents
get Text Alignment()
取得文字對齊方式。可用的對齊類型包括 Document、Document 和 Document。
回攻員
Text:文字對齊類型,或 null (如果文字包含多種文字對齊類型,或從未設定文字對齊方式)
授權
使用這個方法的指令碼需要一或多個下列範圍的授權:
-
https://www.googleapis.com/auth/documents.currentonly -
https://www.googleapis.com/auth/documents
get Type()
擷取元素的 Element。
使用 get 判斷指定元素的確切類型。
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.'); }
回攻員
Element:元素類型。
授權
使用這個方法的指令碼需要一或多個下列範圍的授權:
-
https://www.googleapis.com/auth/documents.currentonly -
https://www.googleapis.com/auth/documents
get Vertical Alignment()
回攻員
Vertical - 垂直對齊
授權
使用這個方法的指令碼需要一或多個下列範圍的授權:
-
https://www.googleapis.com/auth/documents.currentonly -
https://www.googleapis.com/auth/documents
get Width()
以點為單位,擷取含有儲存格的資料欄寬度。
回攻員
Number|null:欄寬 (以點為單位)
授權
使用這個方法的指令碼需要一或多個下列範圍的授權:
-
https://www.googleapis.com/auth/documents.currentonly -
https://www.googleapis.com/auth/documents
insert Horizontal Rule(childIndex)
在指定索引建立並插入新的 Horizontal。
水平規則會包含在新的 Paragraph 中。
參數
| 名稱 | 類型 | 說明 |
|---|---|---|
child | Integer | 要插入元素的索引 |
回攻員
Horizontal:新的水平線
授權
使用這個方法的指令碼需要一或多個下列範圍的授權:
-
https://www.googleapis.com/auth/documents.currentonly -
https://www.googleapis.com/auth/documents
insert Image(childIndex, image)
在指定索引處,從指定圖片 Blob 建立並插入 Inline。
圖片會包含在新的 Paragraph 中。
參數
| 名稱 | 類型 | 說明 |
|---|---|---|
child | Integer | 要插入元素的索引 |
image | Blob | 圖片資料 |
回攻員
Inline:插入的內嵌圖片
授權
使用這個方法的指令碼需要一或多個下列範圍的授權:
-
https://www.googleapis.com/auth/documents.currentonly -
https://www.googleapis.com/auth/documents
insert Image(childIndex, image)
在指定索引處插入指定 Inline。
圖片會包含在新的 Paragraph 中。
參數
| 名稱 | 類型 | 說明 |
|---|---|---|
child | Integer | 要插入元素的索引 |
image | Inline | 要插入的圖片 |
回攻員
Inline:插入的內嵌圖片
授權
使用這個方法的指令碼需要一或多個下列範圍的授權:
-
https://www.googleapis.com/auth/documents.currentonly -
https://www.googleapis.com/auth/documents
insert List Item(childIndex, listItem)
insert List Item(childIndex, text)
insert Paragraph(childIndex, paragraph)
insert Paragraph(childIndex, text)
insert Table(childIndex)
insert Table(childIndex, cells)
insert Table(childIndex, table)
is At Document End()
merge()
將元素與相同類型的前一個同層級元素合併。
只有相同 Element 的元素才能合併。目前元素中包含的所有子項元素都會移至前一個同層級元素。
目前元素會從文件中移除。
const doc = DocumentApp.getActiveDocument(); const documentTab = doc.getActiveTab().asDocumentTab(); const body = documentTab.getBody(); // Example 1: Merge paragraphs // Append two paragraphs to the document's active tab. const par1 = body.appendParagraph('Paragraph 1.'); const 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. const cells = [ ['Row 1, Cell 1', 'Row 1, Cell 2'], ['Row 2, Cell 1', 'Row 2, Cell 2'], ]; // Build a table from the array. const table = body.appendTable(cells); // Get the first row in the table. const row = table.getRow(0); // Get the two cells in this row. const cell1 = row.getCell(0); const cell2 = row.getCell(1); // Merge the current cell into its preceding sibling element. const merged = cell2.merge();
回攻員
Table:合併的元素。
授權
使用這個方法的指令碼需要一或多個下列範圍的授權:
-
https://www.googleapis.com/auth/documents.currentonly -
https://www.googleapis.com/auth/documents
remove Child(child)
remove From Parent()
從父項移除元素。
const doc = DocumentApp.getActiveDocument(); const documentTab = doc.getActiveTab().asDocumentTab(); const body = documentTab.getBody(); // Remove all images in the active tab's body. const imgs = body.getImages(); for (let i = 0; i < imgs.length; i++) { imgs[i].removeFromParent(); }
回攻員
Table:已移除的元素。
授權
使用這個方法的指令碼需要一或多個下列範圍的授權:
-
https://www.googleapis.com/auth/documents.currentonly -
https://www.googleapis.com/auth/documents
replace Text(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');
參數
| 名稱 | 類型 | 說明 |
|---|---|---|
search | String | 要搜尋的規則運算式模式 |
replacement | String | 要用來取代的文字 |
回攻員
Element:目前元素
授權
使用這個方法的指令碼需要一或多個下列範圍的授權:
-
https://www.googleapis.com/auth/documents.currentonly -
https://www.googleapis.com/auth/documents
set Attributes(attributes)
設定元素的屬性。
指定的屬性參數必須是物件,其中每個屬性名稱都是 Document 列舉中的項目,每個屬性值都是要套用的新值。
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);
參數
| 名稱 | 類型 | 說明 |
|---|---|---|
attributes | Object | 元素的屬性。 |
回攻員
Table:目前的元素。
授權
使用這個方法的指令碼需要一或多個下列範圍的授權:
-
https://www.googleapis.com/auth/documents.currentonly -
https://www.googleapis.com/auth/documents
set Background Color(color)
set Link Url(url)
set Padding Bottom(paddingBottom)
set Padding Left(paddingLeft)
set Padding Right(paddingRight)
set Padding Top(paddingTop)
set Text(text)
set Text Alignment(textAlignment)
設定文字對齊方式。可用的對齊類型包括 Document、Document 和 Document。
// 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);
參數
| 名稱 | 類型 | 說明 |
|---|---|---|
text | Text | 要套用的文字對齊類型 |
回攻員
Table:目前元素
授權
使用這個方法的指令碼需要一或多個下列範圍的授權:
-
https://www.googleapis.com/auth/documents.currentonly -
https://www.googleapis.com/auth/documents
set Vertical Alignment(alignment)
設定垂直對齊方式。
參數
| 名稱 | 類型 | 說明 |
|---|---|---|
alignment | Vertical | 垂直對齊方式 |
回攻員
Table:目前元素
授權
使用這個方法的指令碼需要一或多個下列範圍的授權:
-
https://www.googleapis.com/auth/documents.currentonly -
https://www.googleapis.com/auth/documents