Object reference
Document
A DocumentApp.Document is the Google Doc file itself. In a script opened from a Doc, getActiveDocument().getBody() returns the active tab. In a standalone script, openById(id).getBody() returns the first tab.
DocumentApp.DocumentStart 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 returns the active tab. In a standalone script, call DocumentApp.openById(id). There, getBody() returns the first tab, and getTab(tabId) selects a specific tab.
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
Logs only the number of characters in the body of the active tab, so no document text is copied into the Execution log.
DocumentApp.DocumentShow example code
function logDocumentCharacterCount() {
const document = DocumentApp.getActiveDocument();
Logger.log('Character count: ' + document.getBody().getText().length);
} Log the document URL and paragraph count
Logs the Google Doc URL from getUrl() and the number of paragraphs in getBody().
DocumentApp.DocumentDocumentApp.BodyShow example code
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
Searches the body of the active tab with findText() and logs whether {{Name}} is present.
DocumentApp.DocumentDocumentApp.BodyShow example code
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
Open method details 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
- addBookmark(position: DocumentApp.Position): DocumentApp.Bookmark
addEditor
- addEditor(email: string): DocumentApp.Document
- addEditor(user: User): DocumentApp.Document
addEditors
- addEditors(emailAddresses: string[]): DocumentApp.Document
addFooter
- addFooter(): DocumentApp.FooterSection
addHeader
- addHeader(): DocumentApp.HeaderSection
addNamedRange
- addNamedRange(name: string, range: DocumentApp.Range): DocumentApp.NamedRange
addViewer
- addViewer(emailAddress: string): DocumentApp.Document
- addViewer(user: User): DocumentApp.Document
addViewers
- addViewers(emailAddresses: string[]): DocumentApp.Document
getActiveTab
- getActiveTab(): DocumentApp.Tab | null
getAs
- getAs(contentType: string): Blob
getBlob
- getBlob(): Blob
getBody
- getBody(): DocumentApp.Body
getBookmark
- getBookmark(id: string): DocumentApp.Bookmark | null
getBookmarks
- getBookmarks(): DocumentApp.Bookmark[]
getCursor
- getCursor(): DocumentApp.Position | null
getEditors
- getEditors(): User[]
getFooter
- getFooter(): DocumentApp.FooterSection | null
getFootnotes
- getFootnotes(): DocumentApp.Footnote[]
getHeader
- getHeader(): DocumentApp.HeaderSection | null
getId
- getId(): string
getLanguage
- getLanguage(): string | null
getName
- getName(): string
getNamedRangeById
- getNamedRangeById(id: string): DocumentApp.NamedRange | null
getNamedRanges
- getNamedRanges(): DocumentApp.NamedRange[]
- getNamedRanges(name: string): DocumentApp.NamedRange[]
getSelection
- getSelection(): DocumentApp.Range | null
getSupportedLanguageCodes
- getSupportedLanguageCodes(): string[]
getTab
- getTab(tabId: string): DocumentApp.Tab | null
getTabs
- getTabs(): DocumentApp.Tab[]
getUrl
- getUrl(): string
getViewers
- getViewers(): User[]
newPosition
- newPosition(element: DocumentApp.Element, offset: number): DocumentApp.Position
newRange
- newRange(): DocumentApp.RangeBuilder
removeEditor
- removeEditor(emailAddress: string): DocumentApp.Document
- removeEditor(user: User): DocumentApp.Document
removeViewer
- removeViewer(emailAddress: string): DocumentApp.Document
- removeViewer(user: User): DocumentApp.Document
saveAndClose
- saveAndClose(): void
setActiveTab
- setActiveTab(tabId: string): void
setCursor
- setCursor(position: DocumentApp.Position): DocumentApp.Document
setLanguage
- setLanguage(languageCode: string): DocumentApp.Document
setName
- setName(name: string): DocumentApp.Document
setSelection
- 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