Interface Element

Element

A 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().

Implementing classes

NameBrief description
BodyAn element representing a document body.
ContainerElementA generic element that may contain other elements.
EquationAn element representing a mathematical expression.
EquationFunctionAn element representing a function in a mathematical Equation.
EquationFunctionArgumentSeparatorAn element representing a function separator in a mathematical Equation.
EquationSymbolAn element representing a symbol in a mathematical Equation.
FooterSectionAn element representing a footer section.
FootnoteAn element representing a footnote.
FootnoteSectionAn element representing a footnote section.
HeaderSectionAn element representing a header section.
HorizontalRuleAn element representing an horizontal rule.
InlineDrawingAn element representing an embedded drawing.
InlineImageAn element representing an embedded image.
ListItemAn element representing a list item.
PageBreakAn element representing a page break.
ParagraphAn element representing a paragraph.
TableAn element representing a table.
TableCellAn element representing a table cell.
TableOfContentsAn element containing a table of contents.
TableRowAn element representing a table row.
TextAn element representing a rich text region.
UnsupportedElementAn element representing a region that is unknown or cannot be affected by a script, such as a page number.

Methods

MethodReturn typeBrief description
asBody()BodyReturns the current element as a Body.
asEquation()EquationReturns the current element as a Equation.
asEquationFunction()EquationFunctionReturns the current element as a EquationFunction.
asEquationFunctionArgumentSeparator()EquationFunctionArgumentSeparatorReturns the current element as a EquationFunctionArgumentSeparator.
asEquationSymbol()EquationSymbolReturns the current element as a EquationSymbol.
asFooterSection()FooterSectionReturns the current element as a FooterSection.
asFootnote()FootnoteReturns the current element as a Footnote.
asFootnoteSection()FootnoteSectionReturns the current element as a FootnoteSection.
asHeaderSection()HeaderSectionReturns the current element as a HeaderSection.
asHorizontalRule()HorizontalRuleReturns the current element as a HorizontalRule.
asInlineDrawing()InlineDrawingReturns the current element as a InlineDrawing.
asInlineImage()InlineImageReturns the current element as a InlineImage.
asListItem()ListItemReturns the current element as a ListItem.
asPageBreak()PageBreakReturns the current element as a PageBreak.
asParagraph()ParagraphReturns the current element as a Paragraph.
asTable()TableReturns the current element as a Table.
asTableCell()TableCellReturns the current element as a TableCell.
asTableOfContents()TableOfContentsReturns the current element as a TableOfContents.
asTableRow()TableRowReturns the current element as a TableRow.
asText()TextReturns the current element as a Text.
copy()ElementReturns a detached, deep copy of the current element.
getAttributes()ObjectRetrieves the element's attributes.
getNextSibling()ElementRetrieves the element's next sibling element.
getParent()ContainerElementRetrieves the element's parent element.
getPreviousSibling()ElementRetrieves the element's previous sibling element.
getType()ElementTypeRetrieves the element's ElementType.
isAtDocumentEnd()BooleanDetermines whether the element is at the end of the Document.
merge()ElementMerges the element with the preceding sibling of the same type.
removeFromParent()ElementRemoves the element from its parent.
setAttributes(attributes)ElementSets the element's attributes.

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.

Return

Body — the current element

Authorization

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 a Equation.

Use this method to aid auto-complete whenever a given element is known to be of a specific type.

Return

Equation — the current element

Authorization

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.

Return

EquationFunction — the current element

Authorization

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.

Return

EquationFunctionArgumentSeparator — the current element

Authorization

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.

Return

EquationSymbol — the current element

Authorization

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

asFooterSection()

Returns the current element as a FooterSection.

Use this method to aid auto-complete whenever a given element is known to be of a specific type.

Return

FooterSection — the current element

Authorization

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

asFootnote()

Returns the current element as a Footnote.

Use this method to aid auto-complete whenever a given element is known to be of a specific type.

Return

Footnote — the current element

Authorization

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

asFootnoteSection()

Returns the current element as a FootnoteSection.

Use this method to aid auto-complete whenever a given element is known to be of a specific type.

Return

FootnoteSection — the current element

Authorization

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

asHeaderSection()

Returns the current element as a HeaderSection.

Use this method to aid auto-complete whenever a given element is known to be of a specific type.

Return

HeaderSection — the current element

Authorization

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.

Return

HorizontalRule — the current element

Authorization

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.

Return

InlineDrawing — the current element

Authorization

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.

Return

InlineImage — the current element

Authorization

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.

Return

ListItem — the current element

Authorization

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.

Return

PageBreak — the current element

Authorization

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.

Return

Paragraph — the current element

Authorization

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.

Return

Table — the current element

Authorization

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.

Return

TableCell — the current element

Authorization

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.

Return

TableOfContents — the current element

Authorization

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.

Return

TableRow — the current element

Authorization

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.

Return

Text — the current element

Authorization

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 will not have a parent.

Return

Element — the new copy

Authorization

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.

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 — the element's attributes

Authorization

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.

Return

Element — the next sibling element

Authorization

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.

Return

ContainerElement — the parent element

Authorization

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.

Return

Element — the previous sibling element

Authorization

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.

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 — the element type

Authorization

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.

Return

Boolean — whether the element is at the end of the document

Authorization

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 may 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.

var body = DocumentApp.getActiveDocument().getBody();

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

Return

Element — the merged element

Authorization

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.

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

Element — the removed element

Authorization

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.

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

Parameters

NameTypeDescription
attributesObjectthe element's attributes

Return

Element — the current element

Authorization

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

发送以下问题的反馈:

此网页
Apps Script
Apps Script
需要帮助?请访问我们的支持页面