Class Table  |  Apps Script  |  Google for Developers (original) (raw)

Skip to main content

Class Table

Stay organized with collections Save and categorize content based on your preferences.

Table

An element representing a table. A Table may only contain [TableRow](/apps-script/reference/document/table-row) elements. For more information on document structure, see the guide to extending Google Docs.

When creating a Table that contains a large number of rows or cells, consider building it from a string array, as shown in the following example.

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

Methods

Method Return type Brief description
appendTableRow() TableRow Creates and appends a new TableRow.
appendTableRow(tableRow) TableRow Appends the given TableRow.
clear() Table Clears the contents of the element.
copy() Table Returns a detached, deep copy of the current element.
editAsText() Text Obtains a Text version of the current element, for editing.
findElement(elementType) RangeElement Searches the contents of the element for a descendant of the specified type.
findElement(elementType, from) RangeElement Searches the contents of the element for a descendant of the specified type, starting from the specified RangeElement.
findText(searchPattern) RangeElement Searches the contents of the element for the specified text pattern using regular expressions.
findText(searchPattern, from) RangeElement Searches the contents of the element for the specified text pattern, starting from a given search result.
getAttributes() Object Retrieves the element's attributes.
getBorderColor() String Retrieves the border color.
getBorderWidth() Number Retrieves the border width, in points.
getCell(rowIndex, cellIndex) TableCell Retrieves the TableCell at the specified row and cell indices.
getChild(childIndex) Element Retrieves the child element at the specified child index.
getChildIndex(child) Integer Retrieves the child index for the specified child element.
getColumnWidth(columnIndex) Number Retrieves the width of the specified table column, in points.
getLinkUrl() String Retrieves the link url.
getNextSibling() Element Retrieves the element's next sibling element.
getNumChildren() Integer Retrieves the number of children.
getNumRows() Integer Retrieves the number of TableRows.
getParent() ContainerElement Retrieves the element's parent element.
getPreviousSibling() Element Retrieves the element's previous sibling element.
getRow(rowIndex) TableRow Retrieves the TableRow at the specified row index.
getText() String Retrieves the contents of the element as a text string.
getTextAlignment() TextAlignment Gets the text alignment.
getType() ElementType Retrieves the element's ElementType.
insertTableRow(childIndex) TableRow Creates and inserts a new TableRow at the specified index.
insertTableRow(childIndex, tableRow) TableRow Inserts the given TableRow at the specified index.
isAtDocumentEnd() Boolean Determines whether the element is at the end of the Document.
removeChild(child) Table Removes the specified child element.
removeFromParent() Table Removes the element from its parent.
removeRow(rowIndex) TableRow Removes the TableRow at the specified row index.
replaceText(searchPattern, replacement) Element Replaces all occurrences of a given text pattern with a given replacement string, using regular expressions.
setAttributes(attributes) Table Sets the element's attributes.
setBorderColor(color) Table Sets the border color.
setBorderWidth(width) Table Sets the border width, in points.
setColumnWidth(columnIndex, width) Table Sets the width of the specified column, in points.
setLinkUrl(url) Table Sets the link url.
setTextAlignment(textAlignment) Table Sets the text alignment.

Detailed documentation

appendTableRow()

Creates and appends a new [TableRow](/apps-script/reference/document/table-row).

Return

[TableRow](/apps-script/reference/document/table-row) — the new table row element

Scripts that use this method require authorization with one or more of the following scopes:


appendTableRow(tableRow)

Appends the given [TableRow](/apps-script/reference/document/table-row).

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

Parameters

Name Type Description
tableRow TableRow The table row to append.

Return

[TableRow](/apps-script/reference/document/table-row) — The appended table row element.

Authorization

Scripts that use this method require authorization with one or more of the following scopes:


clear()

Clears the contents of the element.

Return

[Table](#) — The current element.


copy()

Returns a detached, deep copy of the current element.

Any child elements present in the element are also copied. The new element doesn't have a parent.

Return

[Table](#) — The new copy.

Authorization

Scripts that use this method require authorization with one or more of the following scopes:


editAsText()

Obtains a [Text](/apps-script/reference/document/text) version of the current element, for editing.

Use editAsText for manipulating the elements contents as rich text. The editAsText mode ignores non-text elements (such as [InlineImage](/apps-script/reference/document/inline-image) and [HorizontalRule](/apps-script/reference/document/horizontal-rule)).

Child elements fully contained within a deleted text range are removed from the element.

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

Return

[Text](/apps-script/reference/document/text) — a text version of the current element


findElement(elementType)

Searches the contents of the element for a descendant of the specified type.

Parameters

Name Type Description
elementType ElementType The type of element to search for.

Return

[RangeElement](/apps-script/reference/document/range-element) — A search result indicating the position of the search element.

Authorization

Scripts that use this method require authorization with one or more of the following scopes:


findElement(elementType, from)

Searches the contents of the element for a descendant of the specified type, starting from the specified [RangeElement](/apps-script/reference/document/range-element).

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

Parameters

Name Type Description
elementType ElementType The type of element to search for.
from RangeElement The search result to search from.

Return

[RangeElement](/apps-script/reference/document/range-element) — A search result indicating the next position of the search element.

Authorization

Scripts that use this method require authorization with one or more of the following scopes:


findText(searchPattern)

Searches the contents of the element for the specified text pattern using regular expressions.

A subset of the JavaScript regular expression features are not fully supported, such as capture groups and mode modifiers.

The provided regular expression pattern is independently matched against each text block contained in the current element.

Parameters

Name Type Description
searchPattern String the pattern to search for

Return

[RangeElement](/apps-script/reference/document/range-element) — a search result indicating the position of the search text, or null if there is no match

Authorization

Scripts that use this method require authorization with one or more of the following scopes:


findText(searchPattern, from)

Searches the contents of the element for the specified text pattern, starting from a given search result.

A subset of the JavaScript regular expression features are not fully supported, such as capture groups and mode modifiers.

The provided regular expression pattern is independently matched against each text block contained in the current element.

Parameters

Name Type Description
searchPattern String the pattern to search for
from RangeElement the search result to search from

Return

[RangeElement](/apps-script/reference/document/range-element) — a search result indicating the next position of the search text, or null if there is no match

Authorization

Scripts that use this method require authorization with one or more of the following scopes:


getAttributes()

Retrieves the element's attributes.

The result is an object containing a property for each valid element attribute where each property name corresponds to an item in the DocumentApp.Attribute enumeration.

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

Return

Object — The element's attributes.

Authorization

Scripts that use this method require authorization with one or more of the following scopes:


getBorderColor()

Retrieves the border 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 first table. table.setBorderColor('#00FF00');

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

Return

String — The border color, formatted in CSS notation (like '#ffffff').

Authorization

Scripts that use this method require authorization with one or more of the following scopes:


getBorderWidth()

Retrieves the border width, in points.

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

Return

Number — The border width, in points.

Authorization

Scripts that use this method require authorization with one or more of the following scopes:


getCell(rowIndex, cellIndex)

Retrieves the [TableCell](/apps-script/reference/document/table-cell) at the specified row and cell indices.

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

Parameters

Name Type Description
rowIndex Integer The index of the row containing the cell to retrieve.
cellIndex Integer The index of the cell to retrieve.

Return

[TableCell](/apps-script/reference/document/table-cell) — The table cell.

Authorization

Scripts that use this method require authorization with one or more of the following scopes:


getChild(childIndex)

Retrieves the child element at the specified child index.

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

Parameters

Name Type Description
childIndex Integer The index of the child element to retrieve.

Return

[Element](/apps-script/reference/document/element) — The child element at the specified index.

Authorization

Scripts that use this method require authorization with one or more of the following scopes:


getChildIndex(child)

Retrieves the child index for the specified child element.

Parameters

Name Type Description
child Element The child element for which to retrieve the index.

Return

Integer — The child index.

Authorization

Scripts that use this method require authorization with one or more of the following scopes:


getColumnWidth(columnIndex)

Retrieves the width of the specified table column, in points.

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

Parameters

Name Type Description
columnIndex Integer The column index.

Return

Number — The column width, in points.

Authorization

Scripts that use this method require authorization with one or more of the following scopes:


getLinkUrl()

Retrieves the link url.

Return

String — the link url, or null if the element contains multiple values for this attribute

Authorization

Scripts that use this method require authorization with one or more of the following scopes:


getNextSibling()

Retrieves the element's next sibling element.

The next sibling has the same parent and follows the current element.

Return

[Element](/apps-script/reference/document/element) — The next sibling element.

Authorization

Scripts that use this method require authorization with one or more of the following scopes:


getNumChildren()

Retrieves the number of 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.);

Return

Integer — The number of children.

Authorization

Scripts that use this method require authorization with one or more of the following scopes:


getNumRows()

Retrieves the number of [TableRows](/apps-script/reference/document/table-row).

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

Return

Integer — The number of table rows.

Authorization

Scripts that use this method require authorization with one or more of the following scopes:


getParent()

Retrieves the element's parent element.

The parent element contains the current element.

Return

[ContainerElement](/apps-script/reference/document/container-element) — The parent element.

Authorization

Scripts that use this method require authorization with one or more of the following scopes:


getPreviousSibling()

Retrieves the element's previous sibling element.

The previous sibling has the same parent and precedes the current element.

Return

[Element](/apps-script/reference/document/element) — The previous sibling element.

Authorization

Scripts that use this method require authorization with one or more of the following scopes:


getRow(rowIndex)

Retrieves the [TableRow](/apps-script/reference/document/table-row) at the specified row index.

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

Parameters

Name Type Description
rowIndex Integer The index of the row to retrieve.

Return

[TableRow](/apps-script/reference/document/table-row) — The table row.

Authorization

Scripts that use this method require authorization with one or more of the following scopes:


getText()

Retrieves the contents of the element as a text string.

Return

String — the contents of the element as text string

Authorization

Scripts that use this method require authorization with one or more of the following scopes:


getTextAlignment()

Gets the text alignment. The available types of alignment are DocumentApp.TextAlignment.NORMAL, DocumentApp.TextAlignment.SUBSCRIPT, and DocumentApp.TextAlignment.SUPERSCRIPT.

Return

[TextAlignment](/apps-script/reference/document/text-alignment) — the type of text alignment, or null if the text contains multiple types of text alignments or if the text alignment has never been set

Authorization

Scripts that use this method require authorization with one or more of the following scopes:


getType()

Retrieves the element's [ElementType](/apps-script/reference/document/element-type).

Use getType() to determine the exact type of a given element.

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

Return

[ElementType](/apps-script/reference/document/element-type) — The element type.

Authorization

Scripts that use this method require authorization with one or more of the following scopes:


insertTableRow(childIndex)

Creates and inserts a new [TableRow](/apps-script/reference/document/table-row) at the specified index.

Parameters

Name Type Description
childIndex Integer the index at which to insert the element

Return

[TableRow](/apps-script/reference/document/table-row) — the current element

Authorization

Scripts that use this method require authorization with one or more of the following scopes:


insertTableRow(childIndex, tableRow)

Inserts the given [TableRow](/apps-script/reference/document/table-row) at the specified index.

Parameters

Name Type Description
childIndex Integer the index at which to insert the element
tableRow TableRow the table row to insert

Return

[TableRow](/apps-script/reference/document/table-row) — the inserted table row element

Authorization

Scripts that use this method require authorization with one or more of the following scopes:


isAtDocumentEnd()

Determines whether the element is at the end of the [Document](/apps-script/reference/document/document).

Return

Boolean — Whether the element is at the end of the tab.

Authorization

Scripts that use this method require authorization with one or more of the following scopes:


removeChild(child)

Removes the specified child element.

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

Parameters

Name Type Description
child Element The child element to remove.

Return

[Table](#) — The current element.

Authorization

Scripts that use this method require authorization with one or more of the following scopes:


removeFromParent()

Removes the element from its 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(); }

Return

[Table](#) — The removed element.

Authorization

Scripts that use this method require authorization with one or more of the following scopes:


removeRow(rowIndex)

Removes the [TableRow](/apps-script/reference/document/table-row) at the specified row index.

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

Parameters

Name Type Description
rowIndex Integer The index of the row to remove.

Return

[TableRow](/apps-script/reference/document/table-row) — The removed row.

Authorization

Scripts that use this method require authorization with one or more of the following scopes:


replaceText(searchPattern, replacement)

Replaces all occurrences of a given text pattern with a given replacement string, using regular expressions.

The search pattern is passed as a string, not a JavaScript regular expression object. Because of this you'll need to escape any backslashes in the pattern.

This methods uses Google's RE2 regular expression library, which limits the supported syntax.

The provided regular expression pattern is independently matched against each text block contained in the current element.

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

// Clear the text surrounding "Apps Script", with or without text. body.replaceText('^.Apps ?Script.$', 'Apps Script');

Parameters

Name Type Description
searchPattern String the regex pattern to search for
replacement String the text to use as replacement

Return

[Element](/apps-script/reference/document/element) — the current element

Authorization

Scripts that use this method require authorization with one or more of the following scopes:


setAttributes(attributes)

Sets the element's attributes.

The specified attributes parameter must be an object where each property name is an item in the DocumentApp.Attribute enumeration and each property value is the new value to be applied.

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

Parameters

Name Type Description
attributes Object The element's attributes.

Return

[Table](#) — The current element.

Authorization

Scripts that use this method require authorization with one or more of the following scopes:


setBorderColor(color)

Sets the border 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');

Parameters

Name Type Description
color String The border color, formatted in CSS notation (like '#ffffff').

Return

[Table](#) — The current element.

Authorization

Scripts that use this method require authorization with one or more of the following scopes:


setBorderWidth(width)

Sets the border width, in points.

Parameters

Name Type Description
width Number the border width, in points

Return

[Table](#) — the current element

Authorization

Scripts that use this method require authorization with one or more of the following scopes:


setColumnWidth(columnIndex, width)

Sets the width of the specified column, in points.

Parameters

Name Type Description
columnIndex Integer the column index
width Number the border width, in points

Return

[Table](#) — the current element

Authorization

Scripts that use this method require authorization with one or more of the following scopes:


setLinkUrl(url)

Sets the link url.

Parameters

Name Type Description
url String the link url

Return

[Table](#) — the current element

Authorization

Scripts that use this method require authorization with one or more of the following scopes:


setTextAlignment(textAlignment)

Sets the text alignment. The available types of alignment are DocumentApp.TextAlignment.NORMAL, DocumentApp.TextAlignment.SUBSCRIPT, and DocumentApp.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);

Parameters

Name Type Description
textAlignment TextAlignment the type of text alignment to apply

Return

[Table](#) — the current element

Authorization

Scripts that use this method require authorization with one or more of the following scopes:

Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.

Last updated 2024-12-03 UTC.