Class DocumentTab

  • A DocumentTab is a rich text container within a document, capable of holding elements like tables and lists.

  • You can retrieve a DocumentTab usingDocument.getTabs()[tabIndex].asDocumentTab() orDocument.getTab(tabId).asDocumentTab().

  • DocumentTab provides methods to manage bookmarks, headers, footers, named ranges, and access the main body content of the tab.

  • Key methods allow adding and retrieving bookmarks, adding headers and footers, adding and getting named ranges by name or ID, and accessing the main content body and footnotes.

  • You can create new Position and Range objects relative to elements within the DocumentTab for various manipulation purposes.

DocumentTab

A document tab, containing rich text and elements such as tables and lists.

Retrieve a document tab usingDocument.getTabs()[tabIndex].asDocumentTab().

// Get a specific document tab based on the tab ID.// TODO(developer): Replace the IDs with your own.constdocumentTab=DocumentApp.openById('123abc').getTab('123abc').asDocumentTab();

Methods

MethodReturn typeBrief description
addBookmark(position)BookmarkAdds aBookmark at the givenPosition.
addFooter()FooterSectionAdds a tab footer section, if none exists.
addHeader()HeaderSectionAdds a tab header section, if none exists.
addNamedRange(name, range)NamedRangeAdds aNamedRange, which is aRange that has a name and ID to use forlater retrieval.
getBody()BodyRetrieves the tab'sBody.
getBookmark(id)Bookmark|nullGets theBookmark with the given ID.
getBookmarks()Bookmark[]Gets allBookmark objects in the tab.
getFooter()FooterSection|nullRetrieves the tab's footer section, if one exists.
getFootnotes()Footnote[]|nullRetrieves all theFootnote elements in the tab's body.
getHeader()HeaderSection|nullRetrieves the tab's header section, if one exists.
getNamedRangeById(id)NamedRange|nullGets theNamedRange with the given ID.
getNamedRanges()NamedRange[]Gets allNamedRange objects in the tab.
getNamedRanges(name)NamedRange[]Gets allNamedRange objects in the tab with the given name.
newPosition(element, offset)PositionCreates a newPosition, which is a reference to a location in the tab, relative to aspecific element.
newRange()RangeBuilderCreates a builder used to constructRange objects from tab elements.

Detailed documentation

addBookmark(position)

Adds aBookmark at the givenPosition.

// Opens the Docs file and retrieves the tab by its IDs. If you created your// script from within a Google Docs file, you can use// DocumentApp.getActiveDocument().getActiveTab() instead.// TODO(developer): Replace the IDs with your own.constdocumentTab=DocumentApp.openById('123abc').getTab('123abc').asDocumentTab();// Gets the tab body and adds a paragraph.constparagraph=documentTab.getBody().appendParagraph('My new paragraph.');// Creates a position at the first character of the paragraph text.constposition=documentTab.newPosition(paragraph.getChild(0),0);// Adds a bookmark at the first character of the paragraph text.constbookmark=documentTab.addBookmark(position);// Logs the bookmark ID to the console.console.log(bookmark.getId());

Parameters

NameTypeDescription
positionPositionThe position of the new bookmark.

Return

Bookmark — The new bookmark.

Authorization

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

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

addFooter()

Adds a tab footer section, if none exists.

// Opens the Docs file and retrieves the tab by its IDs. If you created your// script from within a Google Docs file, you can use// DocumentApp.getActiveDocument().getActiveTab() instead.// TODO(developer): Replace the IDs with your own.constdocumentTab=DocumentApp.openById('123abc').getTab('123abc').asDocumentTab();// Adds a footer to the tab.constfooter=documentTab.addFooter();// Sets the footer text to 'This is a footer.'footer.setText('This is a footer');

Return

FooterSection — The tab footer.

Authorization

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

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

addHeader()

Adds a tab header section, if none exists.

// Opens the Docs file and retrieves the tab by its IDs. If you created your// script from within a Google Docs file, you can use// DocumentApp.getActiveDocument().getActiveTab() instead.// TODO(developer): Replace the IDs with your own.constdocumentTab=DocumentApp.openById('123abc').getTab('123abc').asDocumentTab();// Adds a header to the tab.constheader=documentTab.addHeader();// Sets the header text to 'This is a header.'header.setText('This is a header');

Return

HeaderSection — The tab header.

Authorization

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

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

addNamedRange(name, range)

Adds aNamedRange, which is aRange that has a name and ID to use forlater retrieval. Names aren't necessarily unique, even across tabs; several different ranges inthe same document can share the same name, much like a class in HTML. By contrast, IDs areunique within the document, like an ID in HTML. After you add aNamedRange you can'tmodify it, you can only remove it.

Any script that accesses the tab can access aNamedRange. To avoid unintendedconflicts between scripts, consider prefixing range names with a unique string.

// Creates a named range that includes every table in a tab by its ID.// TODO(developer): Replace the IDs with your own.constdocumentTab=DocumentApp.openById('123abc').getTab('123abc').asDocumentTab();constrangeBuilder=documentTab.newRange();consttables=documentTab.getBody().getTables();for(leti=0;i <tables.length;i++){rangeBuilder.addElement(tables[i]);}documentTab.addNamedRange('Tab t.0 tables',rangeBuilder.build());

Parameters

NameTypeDescription
nameStringThe name for the range, which doesn't need to be unique; range names must be between 1-256 characters.
rangeRangeThe range of elements to associate with the name; the range can be asearch result or manually constructed withnewRange().

Return

NamedRange — TheNamedRange.

Authorization

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

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

getBody()

Retrieves the tab'sBody.

Tabs may contain different types of sections (for example,HeaderSection,FooterSection). The active section for a tab is theBody.

Element methods inDocumentTab delegate to theBody.

// Opens the Docs file and retrieves the tab by its IDs. If you created your// script from within a Google Docs file, you can use// DocumentApp.getActiveDocument().getActiveTab() instead.// TODO(developer): Replace the IDs with your own.constdocumentTab=DocumentApp.openById('123abc').getTab('123abc').asDocumentTab();// Gets the tab body.constbody=documentTab.getBody();// Gets the body text and logs it to the console.console.log(body.getText());

Return

Body — The tab's body section.

Authorization

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

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

getBookmark(id)

Gets theBookmark with the given ID. This method returnsnull if no suchBookmark exists within this tab.

// Opens the Docs file and retrieves the tab by its IDs. If you created your// script from within a Google Docs file, you can use// DocumentApp.getActiveDocument().getActiveTab() instead.// TODO(developer): Replace the IDs with your own.constdocumentTab=DocumentApp.openById('123abc').getTab('123abc').asDocumentTab();// Gets the bookmark by its ID.constbookmark=documentTab.getBookmark('id.xyz654321');// If the bookmark exists within the tab, logs the character offset of its// position to the console. Otherwise, logs 'No bookmark exists with the given// ID.' to the console.if(bookmark){console.log(bookmark.getPosition().getOffset());}else{console.log('No bookmark exists with the given ID.');}

Parameters

NameTypeDescription
idStringThe ID for theBookmark.

Return

Bookmark|null — TheBookmark with the given ID, ornull if no suchBookmark exists within the tab.

Authorization

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

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

getBookmarks()

Gets allBookmark objects in the tab.

// Opens the Docs file and retrieves the tab by its IDs. If you created your// script from within a Google Docs file, you can use// DocumentApp.getActiveDocument().getActiveTab() instead.// TODO(developer): Replace the IDs with your own.constdocumentTab=DocumentApp.openById('123abc').getTab('123abc').asDocumentTab();// Gets all of the bookmarks in the tab.constbookmarks=documentTab.getBookmarks();// Logs the number of bookmarks in the tab to the console.console.log(bookmarks.length);

Return

Bookmark[] — An array of theBookmark objects in the tab.

Authorization

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

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

getFooter()

Retrieves the tab's footer section, if one exists.

// Opens the Docs file and retrieves the tab by its IDs. If you created your// script from within a Google Docs file, you can use// DocumentApp.getActiveDocument().getActiveTab() instead.// TODO(developer): Replace the IDs with your own.constdocumentTab=DocumentApp.openById('123abc').getTab('123abc').asDocumentTab();// Gets the text of the tab's footer and logs it to the console.console.log(documentTab.getFooter().getText());

Return

FooterSection|null — The tab's footer.

Authorization

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

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

getFootnotes()

Retrieves all theFootnote elements in the tab's body.

Calls togetFootnotes cause an iteration over the tab's elements. For large tabs,avoid unnecessary calls to this method.

// Opens the Docs file and retrieves the tab by its IDs. If you created your// script from within a Google Docs file, you can use// DocumentApp.getActiveDocument().getActiveTab() instead.// TODO(developer): Replace the IDs with your own.constdocumentTab=DocumentApp.openById('123abc').getTab('123abc').asDocumentTab();// Gets the first footnote.constfootnote=documentTab.getFootnotes()[0];// Logs footnote contents to the console.console.log(footnote.getFootnoteContents().getText());

Return

Footnote[]|null — The tab's footnotes.

Authorization

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

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

getHeader()

Retrieves the tab's header section, if one exists.

// Opens the Docs file and retrieves the tab by its IDs. If you created your// script from within a Google Docs file, you can use// DocumentApp.getActiveDocument().getActiveTab() instead.// TODO(developer): Replace the IDs with your own.constdocumentTab=DocumentApp.openById('123abc').getTab('123abc').asDocumentTab();// Gets the text of the tab's header and logs it to the console.console.log(documentTab.getHeader().getText());

Return

HeaderSection|null — The tab's header.

Authorization

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

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

getNamedRangeById(id)

Gets theNamedRange with the given ID. This method returnsnull if no suchNamedRange exists in the tab. Names are not necessarily unique, even across tabs;several different ranges in the same document may share the same name, much like a class inHTML. By contrast, IDs are unique within the tab, like an ID in HTML.

Parameters

NameTypeDescription
idStringThe range's ID, which is unique within the tab.

Return

NamedRange|null — TheNamedRange with the given ID, ornull if no such range exists in the tab.

Authorization

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

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

getNamedRanges()

Gets allNamedRange objects in the tab.

ANamedRange can be accessed by any script that accesses the tab. To avoidunintended conflicts between scripts, consider prefixing range names with a unique string.

Return

NamedRange[] — An array of theNamedRange objects in the tab, possibly including multiple ranges with the same name.

Authorization

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

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

getNamedRanges(name)

Gets allNamedRange objects in the tab with the given name. Names are not necessarilyunique, even across tabs; several different ranges in the same document may share the samename, much like a class in HTML. By contrast, IDs are unique within the tab, like an ID inHTML.

ANamedRange can be accessed by any script that accesses the tab. To avoidunintended conflicts between scripts, consider prefixing range names with a unique string.

Parameters

NameTypeDescription
nameStringThe range's name, which is not necessarily unique.

Return

NamedRange[] — An array of theNamedRange objects in the tab with the given name.

Authorization

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

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

newPosition(element, offset)

Creates a newPosition, which is a reference to a location in the tab, relative to aspecific element. The user's cursor is represented as aPosition, among other uses.

// Append a paragraph, then place the user's cursor after the first word of the// new paragraph.// TODO(developer): Replace the IDs with your own.constdoc=DocumentApp.openById('123abc');constdocumentTab=doc.getTab('123abc').asDocumentTab();constparagraph=documentTab.getBody().appendParagraph('My new paragraph.');constposition=documentTab.newPosition(paragraph.getChild(0),2);doc.setCursor(position);

Parameters

NameTypeDescription
elementElementThe element that contains the newly createdPosition to; this must be either aText element or a container element likeParagraph.
offsetIntegerForText elements, the number of characters before thePosition; for other elements, the number of child elements before thePosition within the same container element.

Return

Position — The newPosition.

Authorization

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

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

newRange()

Creates a builder used to constructRange objects from tab elements.

// Change the user's selection to a range that includes every table in the tab.// TODO(developer): Replace the IDs with your own.constdoc=DocumentApp.openById('123abc');constdocumentTab=doc.getTab('123abc').asDocumentTab();constrangeBuilder=documentTab.newRange();consttables=documentTab.getBody().getTables();for(leti=0;i <tables.length;i++){rangeBuilder.addElement(tables[i]);}doc.setSelection(rangeBuilder.build());

Return

RangeBuilder — The new builder.

Authorization

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

  • 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 theCreative Commons Attribution 4.0 License, and code samples are licensed under theApache 2.0 License. For details, see theGoogle Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.

Last updated 2025-12-11 UTC.