Skip to content

Object reference

Document body

The body holds the main text of one tab in a Google Doc, as a list of paragraphs, headings, tables, and other elements. In a bound script, it is the active tab’s body.

DocumentApp.Body

Start here

Get this object first

For a script opened from a Google Doc, get the active document and then the active tab’s body.

Code.gs
function showBodyParagraphCount() {
  const body = DocumentApp.getActiveDocument().getBody();
  Logger.log(body.getParagraphs().length);
}

Then use it

Common actions

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

Append a heading and a paragraph (this changes the document)

This adds a new heading and a new paragraph to the end of the active tab. It does not remove or edit any existing text.

DocumentApp.Body
Show example code
Code.gs
function appendHeadingAndParagraph() {
  const body = DocumentApp.getActiveDocument().getBody();
  body.appendParagraph('Meeting notes').setHeading(DocumentApp.ParagraphHeading.HEADING1);
  body.appendParagraph('Add your notes below this line.');
}

Replace placeholder text (this changes the document)

This finds every {{Name}} placeholder in the active tab and replaces it with a fixed value. replaceText reads a search pattern, where a brace has a special meaning, so each brace gets a backslash in front of it. Only matching placeholders in that tab are changed.

DocumentApp.Body
Show example code
Code.gs
function replaceNamePlaceholder() {
  const body = DocumentApp.getActiveDocument().getBody();
  body.replaceText('\\{\\{Name\\}\\}', 'Jordan Lee');
}

Count the paragraphs

This counts the paragraphs in the active tab and does not change any of them.

DocumentApp.Body
Show example code
Code.gs
function logParagraphCount() {
  const body = DocumentApp.getActiveDocument().getBody();
  Logger.log('Paragraph count: ' + body.getParagraphs().length);
}

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.Body
Object
Body
Runtime
From signature data

Methods and signatures

Browse 54 methods

appendHorizontalRule

From signature data Google source
  • appendHorizontalRule(): DocumentApp.HorizontalRule

appendImage

From signature data Google source
  • appendImage(image: BlobSource): DocumentApp.InlineImage
  • appendImage(image: DocumentApp.InlineImage): DocumentApp.InlineImage

appendListItem

From signature data Google source
  • appendListItem(text: string): DocumentApp.ListItem
  • appendListItem(item: DocumentApp.ListItem): DocumentApp.ListItem

appendPageBreak

From signature data Google source
  • appendPageBreak(): DocumentApp.PageBreak
  • appendPageBreak(pageBreak: DocumentApp.PageBreak): DocumentApp.PageBreak

appendParagraph

From signature data Google source
  • appendParagraph(text: string): DocumentApp.Paragraph
  • appendParagraph(paragraph: DocumentApp.Paragraph): DocumentApp.Paragraph

appendTable

From signature data Google source
  • appendTable(): DocumentApp.Table
  • appendTable(cells: string[][]): DocumentApp.Table
  • appendTable(table: DocumentApp.Table): DocumentApp.Table

clear

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

copy

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

editAsText

From signature data Google source
  • editAsText(): DocumentApp.Text

findElement

From signature data Google source
  • findElement(elementType: DocumentApp.ElementType): DocumentApp.RangeElement | null
  • findElement(elementType: DocumentApp.ElementType, from: DocumentApp.RangeElement): DocumentApp.RangeElement | null

findText

From signature data Google source
  • findText(searchPattern: string): DocumentApp.RangeElement | null
  • findText(searchPattern: string, from: DocumentApp.RangeElement): DocumentApp.RangeElement | null

getAttributes

From signature data Google source
  • getAttributes(): unknown

getChild

From signature data Google source
  • getChild(childIndex: number): DocumentApp.Element

getChildIndex

From signature data Google source
  • getChildIndex(child: DocumentApp.Element): number

getFootnotes

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

getHeadingAttributes

From signature data Google source
  • getHeadingAttributes(paragraphHeading: DocumentApp.ParagraphHeading): unknown

getImages

From signature data Google source
  • getImages(): DocumentApp.InlineImage[] | null

getLinkUrl

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

getListItems

From signature data Google source
  • getListItems(): DocumentApp.ListItem[]

getMarginBottom

From signature data Google source
  • getMarginBottom(): number | null

getMarginLeft

From signature data Google source
  • getMarginLeft(): number | null

getMarginRight

From signature data Google source
  • getMarginRight(): number | null

getMarginTop

From signature data Google source
  • getMarginTop(): number | null

getNextSibling

From signature data Google source
  • getNextSibling(): DocumentApp.Element | null

getNumChildren

From signature data Google source
  • getNumChildren(): number

getPageHeight

From signature data Google source
  • getPageHeight(): number | null

getPageWidth

From signature data Google source
  • getPageWidth(): number | null

getParagraphs

From signature data Google source
  • getParagraphs(): DocumentApp.Paragraph[]

getParent

From signature data Google source
  • getParent(): DocumentApp.ContainerElement | null

getPreviousSibling

From signature data Google source
  • getPreviousSibling(): DocumentApp.Element | null

getTables

From signature data Google source
  • getTables(): DocumentApp.Table[]

getText

From signature data Google source
  • getText(): string

getTextAlignment

From signature data Google source
  • getTextAlignment(): DocumentApp.TextAlignment | null

getType

From signature data Google source
  • getType(): DocumentApp.ElementType

insertHorizontalRule

From signature data Google source
  • insertHorizontalRule(childIndex: number): DocumentApp.HorizontalRule

insertImage

From signature data Google source
  • insertImage(childIndex: number, image: BlobSource): DocumentApp.InlineImage
  • insertImage(childIndex: number, image: DocumentApp.InlineImage): DocumentApp.InlineImage

insertListItem

From signature data Google source
  • insertListItem(childIndex: number, listItem: DocumentApp.ListItem): DocumentApp.ListItem
  • insertListItem(childIndex: number, text: string): DocumentApp.ListItem

insertPageBreak

From signature data Google source
  • insertPageBreak(childIndex: number): DocumentApp.PageBreak
  • insertPageBreak(childIndex: number, pageBreak: DocumentApp.PageBreak): DocumentApp.PageBreak

insertParagraph

From signature data Google source
  • insertParagraph(childIndex: number, paragraph: DocumentApp.Paragraph): DocumentApp.Paragraph
  • insertParagraph(childIndex: number, text: string): DocumentApp.Paragraph

insertTable

From signature data Google source
  • insertTable(childIndex: number): DocumentApp.Table
  • insertTable(childIndex: number, cells: string[][]): DocumentApp.Table
  • insertTable(childIndex: number, table: DocumentApp.Table): DocumentApp.Table

isAtDocumentEnd

From signature data Google source
  • isAtDocumentEnd(): boolean

removeChild

From signature data Google source
  • removeChild(child: DocumentApp.Element): DocumentApp.Body

replaceText

From signature data Google source
  • replaceText(searchPattern: string, replacement: string): DocumentApp.Element

setAttributes

From signature data Google source
  • setAttributes(attributes: unknown): DocumentApp.Body

setHeadingAttributes

From signature data Google source
  • setHeadingAttributes(paragraphHeading: DocumentApp.ParagraphHeading, attributes: unknown): DocumentApp.Body

setLinkUrl

From signature data Google source
  • setLinkUrl(url: string): DocumentApp.Body

setMarginBottom

From signature data Google source
  • setMarginBottom(marginBottom: number): DocumentApp.Body

setMarginLeft

From signature data Google source
  • setMarginLeft(marginLeft: number): DocumentApp.Body

setMarginRight

From signature data Google source
  • setMarginRight(marginRight: number): DocumentApp.Body

setMarginTop

From signature data Google source
  • setMarginTop(marginTop: number): DocumentApp.Body

setPageHeight

From signature data Google source
  • setPageHeight(pageHeight: number): DocumentApp.Body

setPageWidth

From signature data Google source
  • setPageWidth(pageWidth: number): DocumentApp.Body

setText

From signature data Google source
  • setText(text: string): DocumentApp.Body

setTextAlignment

From signature data Google source
  • setTextAlignment(textAlignment: DocumentApp.TextAlignment): DocumentApp.Body

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