Skip to content

Object reference

Document

A DocumentApp.Document is the Google Doc file itself. In a bound script, getActiveDocument().getBody() reaches the active tab; in a standalone script, openById(id).getBody() uses the first tab.

DocumentApp.Document

Start here

Get this object first

For a script opened from a Google Doc, call DocumentApp.getActiveDocument() to work with that open file; its getBody() method uses the active tab. In a standalone script, call DocumentApp.openById(id), where getBody() uses the first tab or getTab(tabId) selects a specific tab.

Code.gs
function showDocumentTitle() {
  const document = DocumentApp.getActiveDocument();
  Logger.log(document.getName());
}

Then use it

Common actions

Choose an action, then open its example only when you need the code.

Count characters in the active tab

This records only the character count in the active tab’s body. It does not copy document text into the execution log or edit the document.

DocumentApp.Document
Show example code
Code.gs
function logDocumentCharacterCount() {
  const document = DocumentApp.getActiveDocument();
  Logger.log('Character count: ' + document.getBody().getText().length);
}

Log the document URL and paragraph count

This reads the Google Doc URL with getUrl() and counts the paragraphs in getBody() without modifying the document.

DocumentApp.DocumentDocumentApp.Body
Show example code
Code.gs
function logDocumentUrlAndParagraphs() {
  const document = DocumentApp.getActiveDocument();
  const paragraphs = document.getBody().getParagraphs().length;
  Logger.log(document.getUrl() + ' (' + paragraphs + ' paragraphs)');
}

Check whether a placeholder appears in the body

This searches the active tab body with findText() and logs whether {{Name}} is present without modifying the document.

DocumentApp.DocumentDocumentApp.Body
Show example code
Code.gs
function checkForNamePlaceholder() {
  const document = DocumentApp.getActiveDocument();
  const match = document.getBody().findText('\{\{Name\}\}');
  Logger.log(match ? 'Found {{Name}} placeholder' : 'No {{Name}} placeholder found');
}

Technical reference

Exact names and signatures

Generated runtime and signature facts stay separate from the beginner actions above. Open method details only when you need them.

At a glance

Service
DocumentApp
Reference symbol
DocumentApp.Document
Object
Document
Runtime
From signature data

Methods and signatures

Browse 40 methods

addBookmark

From signature data Google source
  • addBookmark(position: DocumentApp.Position): DocumentApp.Bookmark

addEditor

From signature data Google source
  • addEditor(email: string): DocumentApp.Document
  • addEditor(user: User): DocumentApp.Document

addEditors

From signature data Google source
  • addEditors(emailAddresses: string[]): DocumentApp.Document

addFooter

From signature data Google source
  • addFooter(): DocumentApp.FooterSection

addHeader

From signature data Google source
  • addHeader(): DocumentApp.HeaderSection

addNamedRange

From signature data Google source
  • addNamedRange(name: string, range: DocumentApp.Range): DocumentApp.NamedRange

addViewer

From signature data Google source
  • addViewer(emailAddress: string): DocumentApp.Document
  • addViewer(user: User): DocumentApp.Document

addViewers

From signature data Google source
  • addViewers(emailAddresses: string[]): DocumentApp.Document

getActiveTab

From signature data Google source
  • getActiveTab(): DocumentApp.Tab | null

getAs

From signature data Google source
  • getAs(contentType: string): Blob

getBlob

From signature data Google source
  • getBlob(): Blob

getBody

From signature data Google source
  • getBody(): DocumentApp.Body

getBookmark

From signature data Google source
  • getBookmark(id: string): DocumentApp.Bookmark | null

getBookmarks

From signature data Google source
  • getBookmarks(): DocumentApp.Bookmark[]

getCursor

From signature data Google source
  • getCursor(): DocumentApp.Position | null

getEditors

From signature data Google source
  • getEditors(): User[]

getFooter

From signature data Google source
  • getFooter(): DocumentApp.FooterSection | null

getFootnotes

From signature data Google source
  • getFootnotes(): DocumentApp.Footnote[]

getHeader

From signature data Google source
  • getHeader(): DocumentApp.HeaderSection | null

getId

From signature data Google source
  • getId(): string

getLanguage

From signature data Google source
  • getLanguage(): string | null

getName

From signature data Google source
  • getName(): string

getNamedRangeById

From signature data Google source
  • getNamedRangeById(id: string): DocumentApp.NamedRange | null

getNamedRanges

From signature data Google source
  • getNamedRanges(): DocumentApp.NamedRange[]
  • getNamedRanges(name: string): DocumentApp.NamedRange[]

getSelection

From signature data Google source
  • getSelection(): DocumentApp.Range | null

getSupportedLanguageCodes

From signature data Google source
  • getSupportedLanguageCodes(): string[]

getTab

From signature data Google source
  • getTab(tabId: string): DocumentApp.Tab | null

getTabs

From signature data Google source
  • getTabs(): DocumentApp.Tab[]

getUrl

From signature data Google source
  • getUrl(): string

getViewers

From signature data Google source
  • getViewers(): User[]

newPosition

From signature data Google source
  • newPosition(element: DocumentApp.Element, offset: number): DocumentApp.Position

newRange

From signature data Google source
  • newRange(): DocumentApp.RangeBuilder

removeEditor

From signature data Google source
  • removeEditor(emailAddress: string): DocumentApp.Document
  • removeEditor(user: User): DocumentApp.Document

removeViewer

From signature data Google source
  • removeViewer(emailAddress: string): DocumentApp.Document
  • removeViewer(user: User): DocumentApp.Document

saveAndClose

From signature data Google source
  • saveAndClose(): void

setActiveTab

From signature data Google source
  • setActiveTab(tabId: string): void

setCursor

From signature data Google source
  • setCursor(position: DocumentApp.Position): DocumentApp.Document

setLanguage

From signature data Google source
  • setLanguage(languageCode: string): DocumentApp.Document

setName

From signature data Google source
  • setName(name: string): DocumentApp.Document

setSelection

From signature data Google source
  • setSelection(range: DocumentApp.Range): DocumentApp.Document

Need the whole workflow?

Return to a task guide

Task guides cover setup, permissions, expected results, and troubleshooting as well as object methods.

Browse Google Docs tasks