Class Table

資料表

代表表格的元素。Table 只能包含 TableRow 元素。如要進一步瞭解文件結構,請參閱擴充 Google 文件指南

建立含有大量資料列或儲存格的 Table 時,請考慮從字串陣列建構,如下列範例所示。

const body =
    DocumentApp.getActiveDocument().getActiveTab().asDocumentTab().getBody();

// Create a two-dimensional array containing the 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.
body.appendTable(cells);

方法

方法傳回類型簡短說明
appendTableRow()TableRow建立並附加新的 TableRow
appendTableRow(tableRow)TableRow附加指定的 TableRow
clear()Table清除元素內容。
copy()Table傳回目前元素的深層副本,且該副本已分離。
editAsText()Text取得目前元素的 Text 版本,以供編輯。
findElement(elementType)RangeElement|null搜尋元素內容,找出指定型別的後代。
findElement(elementType, from)RangeElement|null從指定 RangeElement 開始,在元素內容中搜尋指定型別的後代。
findText(searchPattern)RangeElement|null使用規則運算式在元素內容中搜尋指定的文字模式。
findText(searchPattern, from)RangeElement|null從指定的搜尋結果開始,在元素內容中搜尋特定文字模式。
getAttributes()Object擷取元素的屬性。
getBorderColor()String|null擷取邊框顏色。
getBorderWidth()Number|null擷取框線寬度 (以點為單位)。
getCell(rowIndex, cellIndex)TableCell|null擷取指定列和儲存格索引的 TableCell
getChild(childIndex)Element擷取指定子項索引處的子項元素。
getChildIndex(child)Integer擷取指定子項元素的子項索引。
getColumnWidth(columnIndex)Number|null以點為單位,擷取指定表格欄的寬度。
getLinkUrl()String|null擷取連結網址。
getNextSibling()Element|null擷取元素的下一個同層級元素。
getNumChildren()Integer擷取子項數量。
getNumRows()Integer擷取 TableRows 的數量。
getParent()ContainerElement|null擷取元素的父項元素。
getPreviousSibling()Element|null擷取元素的前一個同層級元素。
getRow(rowIndex)TableRow|null擷取指定列索引的 TableRow
getText()String以文字字串形式擷取元素內容。
getTextAlignment()TextAlignment|null取得文字對齊方式。
getType()ElementType擷取元素的 ElementType
insertTableRow(childIndex)TableRow在指定索引處建立並插入新的 TableRow
insertTableRow(childIndex, tableRow)TableRow在指定索引處插入指定 TableRow
isAtDocumentEnd()Boolean判斷元素是否位於 Document 的結尾。
removeChild(child)Table移除指定的子項元素。
removeFromParent()Table|null從父項移除元素。
removeRow(rowIndex)TableRow移除指定列索引處的 TableRow
replaceText(searchPattern, replacement)Element使用規則運算式,將指定文字模式的所有例項替換為指定替代字串。
setAttributes(attributes)Table設定元素的屬性。
setBorderColor(color)Table設定邊框顏色。
setBorderWidth(width)Table設定框線寬度 (以點為單位)。
setColumnWidth(columnIndex, width)Table以點為單位設定指定資料欄的寬度。
setLinkUrl(url)Table設定連結網址。
setTextAlignment(textAlignment)Table設定文字對齊方式。

內容詳盡的說明文件

appendTableRow()

建立並附加新的 TableRow

回攻員

TableRow:新的表格列元素

授權

使用這個方法的指令碼需要一或多個下列範圍的授權:

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

appendTableRow(tableRow)

附加指定的 TableRow

// Opens the Docs file by its ID. If you created your script from within a
// Google Docs file, you can use DocumentApp.getActiveDocument() instead.
// TODO(developer): Replace the ID with your own.
const doc = DocumentApp.openById('123abc');

// Gets the body contents of the tab by its ID.
// TODO(developer): Replace the ID with your own.
const body = doc.getTab('123abc').asDocumentTab().getBody();

// Gets the first table in the tab and copies the second row.
const table = body.getTables()[0];
const row = table.getChild(1).copy();

// Adds the copied row to the bottom of the table.
const tableRow = table.appendTableRow(row);

參數

名稱類型說明
tableRowTableRow要附加的表格列。

回攻員

TableRow:附加的表格列元素。

授權

使用這個方法的指令碼需要一或多個下列範圍的授權:

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

clear()

清除元素內容。

回攻員

Table:目前的元素。


copy()

傳回目前元素的深層副本,且該副本已分離。

元素中的任何子項元素也會一併複製。新元素沒有父項。

回攻員

Table:新副本。

授權

使用這個方法的指令碼需要一或多個下列範圍的授權:

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

editAsText()

取得目前元素的 Text 版本,以供編輯。

使用 editAsText 將元素內容當做 RTF 文字來操控。editAsText 模式會忽略非文字元素 (例如 InlineImageHorizontalRule)。

如果子項元素完全包含在已刪除的文字範圍內,系統會從元素中移除這些子項元素。

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:目前元素的文字版本


findElement(elementType)

搜尋元素內容,找出指定型別的後代。

參數

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

回攻員

RangeElement|null:搜尋結果,指出搜尋元素的所在位置。

授權

使用這個方法的指令碼需要一或多個下列範圍的授權:

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

findElement(elementType, from)

從指定的 RangeElement 開始,在元素內容中搜尋指定型別的後代。

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

參數

名稱類型說明
elementTypeElementType要搜尋的元素類型。
fromRangeElement要從中搜尋的搜尋結果。

回攻員

RangeElement|null:搜尋結果,指出搜尋元素的下一個位置。

授權

使用這個方法的指令碼需要一或多個下列範圍的授權:

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

findText(searchPattern)

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

系統不完全支援部分 JavaScript 規則運算式功能,例如擷取群組和模式修飾符。

系統會針對目前元素中包含的每個文字區塊,分別比對所提供的規則運算式模式。

參數

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

回攻員

RangeElement|null:搜尋結果,指出搜尋文字的位置;如果沒有相符項目,則為空值

授權

使用這個方法的指令碼需要一或多個下列範圍的授權:

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

findText(searchPattern, from)

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

系統不完全支援部分 JavaScript 規則運算式功能,例如擷取群組和模式修飾符。

系統會針對目前元素中包含的每個文字區塊,分別比對所提供的規則運算式模式。

參數

名稱類型說明
searchPatternString要搜尋的模式
fromRangeElement要從哪個搜尋結果開始搜尋

回攻員

RangeElement|null:搜尋結果,指出搜尋文字的下一個位置;如果沒有相符項目,則為空值

授權

使用這個方法的指令碼需要一或多個下列範圍的授權:

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

getAttributes()

擷取元素的屬性。

結果會是物件,其中包含每個有效元素屬性的屬性,且每個屬性名稱都對應至 DocumentApp.Attribute 列舉中的項目。

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

getBorderColor()

擷取框線顏色。

// Opens the Docs file by its ID. If you created your script from within a
// Google Docs file, you can use DocumentApp.getActiveDocument() instead.
// TODO(developer): Replace the ID with your own.
const doc = DocumentApp.openById('123abc');

// Gets the body contents of the tab by its ID.
// TODO(developer): Replace the ID with your own.
const body = doc.getTab('123abc').asDocumentTab().getBody();

// Gets the first table.
const table = body.getTables()[0];

// Sets the border color of the first table.
table.setBorderColor('#00FF00');

// Logs the border color of the first table to the console.
console.log(table.getBorderColor());

回攻員

String|null:框線顏色,格式為 CSS 標記 (例如 '#ffffff')。

授權

使用這個方法的指令碼需要一或多個下列範圍的授權:

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

getBorderWidth()

擷取框線寬度 (以點為單位)。

// Opens the Docs file by its ID. If you created your script from within a
// Google Docs file, you can use DocumentApp.getActiveDocument() instead.
// TODO(developer): Replace the ID with your own.
const doc = DocumentApp.openById('123abc');

// Gets the body contents of the tab by its ID.
// TODO(developer): Replace the ID with your own.
const body = doc.getTab('123abc').asDocumentTab().getBody();

// Gets the first table.
const table = body.getTables()[0];

// Sets the border width of the first table.
table.setBorderWidth(20);

// Logs the border width of the first table.
console.log(table.getBorderWidth());

回攻員

Number|null:框線寬度,以點為單位。

授權

使用這個方法的指令碼需要一或多個下列範圍的授權:

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

getCell(rowIndex, cellIndex)

擷取指定列和儲存格索引的 TableCell

// Opens the Docs file by its ID. If you created your script from within a
// Google Docs file, you can use DocumentApp.getActiveDocument() instead.
// TODO(developer): Replace the ID with your own.
const doc = DocumentApp.openById('123abc');

// Gets the body contents of the tab by its ID.
// TODO(developer): Replace the ID with your own.
const body = doc.getTab('123abc').asDocumentTab().getBody();

// Gets the first table.
const table = body.getTables()[0];

// Gets the cell of the table's third row and second column.
const cell = table.getCell(2, 1);

// Logs the cell text to the console.
console.log(cell.getText());

參數

名稱類型說明
rowIndexInteger要擷取儲存格的資料列索引。
cellIndexInteger要擷取的儲存格索引。

回攻員

TableCell|null:表格儲存格。

授權

使用這個方法的指令碼需要一或多個下列範圍的授權:

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

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

參數

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

回攻員

Element:指定索引的子項元素。

授權

使用這個方法的指令碼需要一或多個下列範圍的授權:

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

getChildIndex(child)

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

參數

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

回攻員

Integer - 子項索引。

授權

使用這個方法的指令碼需要一或多個下列範圍的授權:

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

getColumnWidth(columnIndex)

以點為單位,擷取指定表格欄的寬度。

// Opens the Docs file by its ID. If you created your script from within a
// Google Docs file, you can use DocumentApp.getActiveDocument() instead.
// TODO(developer): Replace the ID with your own.
const doc = DocumentApp.openById('123abc');

// Gets the body contents of the tab by its ID.
// TODO(developer): Replace the ID with your own.
const body = doc.getTab('123abc').asDocumentTab().getBody();

// Gets the first table.
const table = body.getTables()[0];

// Sets the width of the second column to 100 points.
const columnWidth = table.setColumnWidth(1, 100);

// Gets the width of the second column and logs it to the console.
console.log(columnWidth.getColumnWidth(1));

參數

名稱類型說明
columnIndexInteger資料欄索引。

回攻員

Number|null:欄寬 (以點為單位)。

授權

使用這個方法的指令碼需要一或多個下列範圍的授權:

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

getLinkUrl()

擷取連結網址。

回攻員

String|null - 連結網址,如果元素包含這項屬性的多個值,則為空值

授權

使用這個方法的指令碼需要一或多個下列範圍的授權:

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

getNextSibling()

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

下一個同層級元素與目前元素具有相同的父項,且位於目前元素之後。

回攻員

Element|null:下一個同層級元素。

授權

使用這個方法的指令碼需要一或多個下列範圍的授權:

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

getNumChildren()

擷取子項數量。

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

getNumRows()

擷取 TableRows 的數量。

// Opens the Docs file by its ID. If you created your script from within a
// Google Docs file, you can use DocumentApp.getActiveDocument() instead.
// TODO(developer): Replace the ID with your own.
const doc = DocumentApp.openById('123abc');

// Gets the body contents of the tab by its ID.
// TODO(developer): Replace the ID with your own.
const body = doc.getTab('123abc').asDocumentTab().getBody();

// Gets the first table.
const table = body.getTables()[0];

// Logs the number of rows of the first table to the console.
console.log(table.getNumRows());

回攻員

Integer:資料表列數。

授權

使用這個方法的指令碼需要一或多個下列範圍的授權:

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

getParent()

擷取元素的父項元素。

父項元素包含目前的元素。

回攻員

ContainerElement|null:父項元素。

授權

使用這個方法的指令碼需要一或多個下列範圍的授權:

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

getPreviousSibling()

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

前一個同層級元素與目前元素具有相同的父項,且位於目前元素之前。

回攻員

Element|null:上一個同層級元素。

授權

使用這個方法的指令碼需要一或多個下列範圍的授權:

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

getRow(rowIndex)

擷取指定列索引的 TableRow

// Opens the Docs file by its ID. If you created your script from within a
// Google Docs file, you can use DocumentApp.getActiveDocument() instead.
// TODO(developer): Replace the ID with your own.
const doc = DocumentApp.openById('123abc');

// Gets the body contents of the tab by its ID.
// TODO(developer): Replace the ID with your own.
const body = doc.getTab('123abc').asDocumentTab().getBody();

// Gets the first table and logs the text of first row to the console.
const table = body.getTables()[0];
console.log(table.getRow(0).getText());

參數

名稱類型說明
rowIndexInteger要擷取的資料列索引。

回攻員

TableRow|null - 表格列。

授權

使用這個方法的指令碼需要一或多個下列範圍的授權:

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

getText()

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

回攻員

String:元素內容 (文字字串)

授權

使用這個方法的指令碼需要一或多個下列範圍的授權:

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

getTextAlignment()

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

回攻員

TextAlignment|null:文字對齊類型,或 null (如果文字包含多種文字對齊類型,或從未設定文字對齊方式)

授權

使用這個方法的指令碼需要一或多個下列範圍的授權:

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

getType()

擷取元素的 ElementType

使用 getType() 判斷指定元素的確切類型。

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

回攻員

ElementType:元素類型。

授權

使用這個方法的指令碼需要一或多個下列範圍的授權:

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

insertTableRow(childIndex)

在指定索引處建立並插入新的 TableRow

參數

名稱類型說明
childIndexInteger要插入元素的索引

回攻員

TableRow:目前元素

授權

使用這個方法的指令碼需要一或多個下列範圍的授權:

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

insertTableRow(childIndex, tableRow)

在指定索引處插入指定 TableRow

參數

名稱類型說明
childIndexInteger要插入元素的索引
tableRowTableRow要插入的表格列

回攻員

TableRow:插入的表格列元素

授權

使用這個方法的指令碼需要一或多個下列範圍的授權:

  • 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

removeChild(child)

移除指定的子項元素。

// Opens the Docs file by its ID. If you created your script from within a
// Google Docs file, you can use DocumentApp.getActiveDocument() instead.
// TODO(developer): Replace the ID with your own.
const doc = DocumentApp.openById('123abc');

// Gets the body contents of the tab by its ID.
// TODO(developer): Replace the ID with your own.
const body = doc.getTab('123abc').asDocumentTab().getBody();

// Gets the first table.
const table = body.getTables()[0];

// Finds the first table row and removes it.
const element = table.findElement(DocumentApp.ElementType.TABLE_ROW);
table.removeChild(element.getElement());

參數

名稱類型說明
childElement要移除的子元素。

回攻員

Table:目前的元素。

授權

使用這個方法的指令碼需要一或多個下列範圍的授權:

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

removeFromParent()

從父項移除元素。

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|null:已移除的元素。

授權

使用這個方法的指令碼需要一或多個下列範圍的授權:

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

removeRow(rowIndex)

移除指定列索引處的 TableRow

// Opens the Docs file by its ID. If you created your script from within a
// Google Docs file, you can use DocumentApp.getActiveDocument() instead.
// TODO(developer): Replace the ID with your own.
const doc = DocumentApp.openById('123abc');

// Gets the body contents of the tab by its ID.
// TODO(developer): Replace the ID with your own.
const body = doc.getTab('123abc').asDocumentTab().getBody();

// Gets the first table and removes its second row.
const table = body.getTables()[0];
table.removeRow(1);

參數

名稱類型說明
rowIndexInteger要移除的資料列索引。

回攻員

TableRow:已移除的資料列。

授權

使用這個方法的指令碼需要一或多個下列範圍的授權:

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

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

參數

名稱類型說明
searchPatternString要搜尋的規則運算式模式
replacementString要用來取代的文字

回攻員

Element:目前元素

授權

使用這個方法的指令碼需要一或多個下列範圍的授權:

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

setAttributes(attributes)

設定元素的屬性。

指定的屬性參數必須是物件,其中每個屬性名稱都是 DocumentApp.Attribute 列舉中的項目,每個屬性值都是要套用的新值。

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

參數

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

回攻員

Table:目前的元素。

授權

使用這個方法的指令碼需要一或多個下列範圍的授權:

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

setBorderColor(color)

設定邊框顏色。

// Opens the Docs file by its ID. If you created your script from within a
// Google Docs file, you can use DocumentApp.getActiveDocument() instead.
// TODO(developer): Replace the ID with your own.
const doc = DocumentApp.openById('123abc');

// Gets the body contents of the tab by its ID.
// TODO(developer): Replace the ID with your own.
const body = doc.getTab('123abc').asDocumentTab().getBody();

// Gets the first table.
const table = body.getTables()[0];

// Sets the border color of the table to green.
table.setBorderColor('#00FF00');

參數

名稱類型說明
colorString框線顏色,格式為 CSS 標記 (例如 '#ffffff')。

回攻員

Table:目前的元素。

授權

使用這個方法的指令碼需要一或多個下列範圍的授權:

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

setBorderWidth(width)

設定框線寬度 (以點為單位)。

參數

名稱類型說明
widthNumber框線寬度 (以點為單位)

回攻員

Table:目前元素

授權

使用這個方法的指令碼需要一或多個下列範圍的授權:

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

setColumnWidth(columnIndex, width)

以點為單位設定指定資料欄的寬度。

參數

名稱類型說明
columnIndexInteger資料欄索引
widthNumber框線寬度 (以點為單位)

回攻員

Table:目前元素

授權

使用這個方法的指令碼需要一或多個下列範圍的授權:

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

setLinkUrl(url)

設定連結網址。

參數

名稱類型說明
urlString連結網址

回攻員

Table:目前元素

授權

使用這個方法的指令碼需要一或多個下列範圍的授權:

  • 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 in the active tab be superscript.
const documentTab =
    DocumentApp.getActiveDocument().getActiveTab().asDocumentTab();
const text = documentTab.getBody().getParagraphs()[0].editAsText();
text.setTextAlignment(DocumentApp.TextAlignment.SUPERSCRIPT);

參數

名稱類型說明
textAlignmentTextAlignment要套用的文字對齊類型

回攻員

Table:目前元素

授權

使用這個方法的指令碼需要一或多個下列範圍的授權:

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