Stay organized with collections Save and categorize content based on your preferences.
ElementA generic element. Document
contents are represented as elements. For example, ListItem
, Paragraph
, and Table
are elements and inherit all of the methods defined by Element
, such as getType()
.
Body
The content of a tab in a Google Docs document. ContainerElement
A generic element that may contain other elements. Date
An element representing a formatted date Equation
An element representing a mathematical expression. EquationFunction
An element representing a function in a mathematical Equation
. EquationFunctionArgumentSeparator
An element representing a function separator in a mathematical Equation
. EquationSymbol
An element representing a symbol in a mathematical Equation
. FooterSection
An element representing a footer section. Footnote
An element representing a footnote. FootnoteSection
An element representing a footnote section. HeaderSection
An element representing a header section. HorizontalRule
An element representing an horizontal rule. InlineDrawing
An element representing an embedded drawing. InlineImage
An element representing an embedded image. ListItem
An element representing a list item. PageBreak
An element representing a page break. Paragraph
An element representing a paragraph. Person
An element representing a link to a person. RichLink
An element representing a link to a Google resource, such as a Drive file or a YouTube video. Table
An element representing a table. TableCell
An element representing a table cell. TableOfContents
An element containing a table of contents. TableRow
An element representing a table row. Text
An element representing a rich text region. UnsupportedElement
An element representing a region that is unknown or cannot be affected by a script, such as a page number. Detailed documentation asBody()
Returns the current element as a Body
.
Use this method to aid auto-complete whenever a given element is known to be of a specific type.
ReturnBody
— The current element.
Scripts that use this method require authorization with one or more of the following scopes:
https://www.googleapis.com/auth/documents.currentonly
https://www.googleapis.com/auth/documents
asDate()
Returns the current element as a Date
.
When you know an element is a Date
, use this method to set its type as a Date
. Doing so lets autocomplete in the Apps Script editor show you the methods you can use with a Date
.
Date
— The current element with its type set as Date
.
Scripts that use this method require authorization with one or more of the following scopes:
https://www.googleapis.com/auth/documents.currentonly
https://www.googleapis.com/auth/documents
asEquation()
Returns the current element as an Equation
.
Use this method to aid auto-complete whenever a given element is known to be of a specific type.
ReturnEquation
— The current element.
Scripts that use this method require authorization with one or more of the following scopes:
https://www.googleapis.com/auth/documents.currentonly
https://www.googleapis.com/auth/documents
asEquationFunction()
Returns the current element as a EquationFunction
.
Use this method to aid auto-complete whenever a given element is known to be of a specific type.
ReturnEquationFunction
— The current element.
Scripts that use this method require authorization with one or more of the following scopes:
https://www.googleapis.com/auth/documents.currentonly
https://www.googleapis.com/auth/documents
asEquationFunctionArgumentSeparator()
Returns the current element as a EquationFunctionArgumentSeparator
.
Use this method to aid auto-complete whenever a given element is known to be of a specific type.
ReturnEquationFunctionArgumentSeparator
— The current element.
Scripts that use this method require authorization with one or more of the following scopes:
https://www.googleapis.com/auth/documents.currentonly
https://www.googleapis.com/auth/documents
asEquationSymbol()
Returns the current element as a EquationSymbol
.
Use this method to aid auto-complete whenever a given element is known to be of a specific type.
ReturnEquationSymbol
— The current element.
Scripts that use this method require authorization with one or more of the following scopes:
https://www.googleapis.com/auth/documents.currentonly
https://www.googleapis.com/auth/documents
asHorizontalRule()
Returns the current element as a HorizontalRule
.
Use this method to aid auto-complete whenever a given element is known to be of a specific type.
ReturnHorizontalRule
— The current element.
Scripts that use this method require authorization with one or more of the following scopes:
https://www.googleapis.com/auth/documents.currentonly
https://www.googleapis.com/auth/documents
asInlineDrawing()
Returns the current element as a InlineDrawing
.
Use this method to aid auto-complete whenever a given element is known to be of a specific type.
ReturnInlineDrawing
— The current element.
Scripts that use this method require authorization with one or more of the following scopes:
https://www.googleapis.com/auth/documents.currentonly
https://www.googleapis.com/auth/documents
asInlineImage()
Returns the current element as a InlineImage
.
Use this method to aid auto-complete whenever a given element is known to be of a specific type.
ReturnInlineImage
— The current element.
Scripts that use this method require authorization with one or more of the following scopes:
https://www.googleapis.com/auth/documents.currentonly
https://www.googleapis.com/auth/documents
asListItem()
Returns the current element as a ListItem
.
Use this method to aid auto-complete whenever a given element is known to be of a specific type.
ReturnListItem
— The current element.
Scripts that use this method require authorization with one or more of the following scopes:
https://www.googleapis.com/auth/documents.currentonly
https://www.googleapis.com/auth/documents
asPageBreak()
Returns the current element as a PageBreak
.
Use this method to aid auto-complete whenever a given element is known to be of a specific type.
ReturnPageBreak
— The current element.
Scripts that use this method require authorization with one or more of the following scopes:
https://www.googleapis.com/auth/documents.currentonly
https://www.googleapis.com/auth/documents
asParagraph()
Returns the current element as a Paragraph
.
Use this method to aid auto-complete whenever a given element is known to be of a specific type.
ReturnParagraph
— The current element.
Scripts that use this method require authorization with one or more of the following scopes:
https://www.googleapis.com/auth/documents.currentonly
https://www.googleapis.com/auth/documents
asPerson()
Returns the current element as a Person
.
When you know an element is a Person
, use this method to set its type as a person. Doing so lets autocomplete in the Apps Script editor show you the methods you can use with a person element.
Person
— The current element with its type set as Person
.
Scripts that use this method require authorization with one or more of the following scopes:
https://www.googleapis.com/auth/documents.currentonly
https://www.googleapis.com/auth/documents
asRichLink()
Returns the current element as a RichLink
, for example, a link to a Google Sheets file.
When you know an element is a RichLink
, use this method to set its type as a RichLink
. Doing so lets autocomplete in the Apps Script editor show you the methods you can use with a RichLink
.
RichLink
— The current element with its type set as RichLink
.
Scripts that use this method require authorization with one or more of the following scopes:
https://www.googleapis.com/auth/documents.currentonly
https://www.googleapis.com/auth/documents
asTable()
Returns the current element as a Table
.
Use this method to aid auto-complete whenever a given element is known to be of a specific type.
ReturnTable
— The current element.
Scripts that use this method require authorization with one or more of the following scopes:
https://www.googleapis.com/auth/documents.currentonly
https://www.googleapis.com/auth/documents
asTableCell()
Returns the current element as a TableCell
.
Use this method to aid auto-complete whenever a given element is known to be of a specific type.
ReturnTableCell
— The current element.
Scripts that use this method require authorization with one or more of the following scopes:
https://www.googleapis.com/auth/documents.currentonly
https://www.googleapis.com/auth/documents
asTableOfContents()
Returns the current element as a TableOfContents
.
Use this method to aid auto-complete whenever a given element is known to be of a specific type.
ReturnTableOfContents
— The current element.
Scripts that use this method require authorization with one or more of the following scopes:
https://www.googleapis.com/auth/documents.currentonly
https://www.googleapis.com/auth/documents
asTableRow()
Returns the current element as a TableRow
.
Use this method to aid auto-complete whenever a given element is known to be of a specific type.
ReturnTableRow
— The current element.
Scripts that use this method require authorization with one or more of the following scopes:
https://www.googleapis.com/auth/documents.currentonly
https://www.googleapis.com/auth/documents
asText()
Returns the current element as a Text
.
Use this method to aid auto-complete whenever a given element is known to be of a specific type.
ReturnText
— The current element.
Scripts that use this method require authorization with one or more of the following scopes:
https://www.googleapis.com/auth/documents.currentonly
https://www.googleapis.com/auth/documents
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.
ReturnElement
— The new copy.
Scripts that use this method require authorization with one or more of the following scopes:
https://www.googleapis.com/auth/documents.currentonly
https://www.googleapis.com/auth/documents
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.
Scripts that use this method require authorization with one or more of the following scopes:
https://www.googleapis.com/auth/documents.currentonly
https://www.googleapis.com/auth/documents
getNextSibling()
Retrieves the element's next sibling element.
The next sibling has the same parent and follows the current element.
ReturnElement
— The next sibling element.
Scripts that use this method require authorization with one or more of the following scopes:
https://www.googleapis.com/auth/documents.currentonly
https://www.googleapis.com/auth/documents
getParent()
Retrieves the element's parent element.
The parent element contains the current element.
ReturnContainerElement
— The parent element.
Scripts that use this method require authorization with one or more of the following scopes:
https://www.googleapis.com/auth/documents.currentonly
https://www.googleapis.com/auth/documents
getPreviousSibling()
Retrieves the element's previous sibling element.
The previous sibling has the same parent and precedes the current element.
ReturnElement
— The previous sibling element.
Scripts that use this method require authorization with one or more of the following scopes:
https://www.googleapis.com/auth/documents.currentonly
https://www.googleapis.com/auth/documents
getType()
Retrieves the element's ElementType
.
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
— The element type.
Scripts that use this method require authorization with one or more of the following scopes:
https://www.googleapis.com/auth/documents.currentonly
https://www.googleapis.com/auth/documents
isAtDocumentEnd()
Determines whether the element is at the end of the Document
.
Boolean
— Whether the element is at the end of the tab.
Scripts that use this method require authorization with one or more of the following scopes:
https://www.googleapis.com/auth/documents.currentonly
https://www.googleapis.com/auth/documents
merge()
Merges the element with the preceding sibling of the same type.
Only elements of the same ElementType
can be merged. Any child elements contained in the current element are moved to the preceding sibling element.
The current element is removed from the document.
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();Return
Element
— The merged element.
Scripts that use this method require authorization with one or more of the following scopes:
https://www.googleapis.com/auth/documents.currentonly
https://www.googleapis.com/auth/documents
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
Element
— The removed element.
Scripts that use this method require authorization with one or more of the following scopes:
https://www.googleapis.com/auth/documents.currentonly
https://www.googleapis.com/auth/documents
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
Element
— The current element.
Scripts that use this method require authorization with one or more of the following scopes:
https://www.googleapis.com/auth/documents.currentonly
https://www.googleapis.com/auth/documents
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.
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Missing the information I need","missingTheInformationINeed","thumb-down"],["Too complicated / too many steps","tooComplicatedTooManySteps","thumb-down"],["Out of date","outOfDate","thumb-down"],["Samples / code issue","samplesCodeIssue","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2024-12-03 UTC."],[[["The `Element` class in Google Apps Script represents any content piece within a Google Doc, such as paragraphs, tables, images, and lists."],["Specific element types like Paragraph or Table inherit properties and methods from the generic `Element` class, offering specialized functionalities."],["Numerous methods are available for manipulating elements, including casting to specific types (`as[ElementType]()`), copying (`copy()`), getting and setting attributes (`getAttributes()`, `setAttributes()`), navigating document structure (`getNextSibling()`, `getPreviousSibling()`, `getParent()`), and removing elements (`removeFromParent()`)."],["Most `Element` methods require authorization scopes like `documents.currentonly` or `documents` to access and modify Google Docs."],["Using the `Element` class and its subtypes, developers can programmatically interact with and manipulate Google Doc content within Apps Script."]]],["Google Docs documents consist of `Element` types like `Paragraphs`, `Tables`, and `ListItems`, all inheriting from a base `Element`. `as...()` methods cast an `Element` to a specific type (e.g., `asTable()`), enabling type-specific method access. Other methods include `copy()` to create duplicates, `getAttributes()` to retrieve attributes, `getType()` to identify the element, `merge()` to combine with the preceding sibling and `removeFromParent()` to remove the element. These methods require specific authorization scopes.\n"]]
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4