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.BodyStart here
Get this object first
For a script opened from a Google Doc, get the active document and then the active tab’s body.
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.BodyShow example code
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.BodyShow example code
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.BodyShow example code
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
- appendHorizontalRule(): DocumentApp.HorizontalRule
appendImage
- appendImage(image: BlobSource): DocumentApp.InlineImage
- appendImage(image: DocumentApp.InlineImage): DocumentApp.InlineImage
appendListItem
- appendListItem(text: string): DocumentApp.ListItem
- appendListItem(item: DocumentApp.ListItem): DocumentApp.ListItem
appendPageBreak
- appendPageBreak(): DocumentApp.PageBreak
- appendPageBreak(pageBreak: DocumentApp.PageBreak): DocumentApp.PageBreak
appendParagraph
- appendParagraph(text: string): DocumentApp.Paragraph
- appendParagraph(paragraph: DocumentApp.Paragraph): DocumentApp.Paragraph
appendTable
- appendTable(): DocumentApp.Table
- appendTable(cells: string[][]): DocumentApp.Table
- appendTable(table: DocumentApp.Table): DocumentApp.Table
clear
- clear(): DocumentApp.Body
copy
- copy(): DocumentApp.Body
editAsText
- editAsText(): DocumentApp.Text
findElement
- findElement(elementType: DocumentApp.ElementType): DocumentApp.RangeElement | null
- findElement(elementType: DocumentApp.ElementType, from: DocumentApp.RangeElement): DocumentApp.RangeElement | null
findText
- findText(searchPattern: string): DocumentApp.RangeElement | null
- findText(searchPattern: string, from: DocumentApp.RangeElement): DocumentApp.RangeElement | null
getAttributes
- getAttributes(): unknown
getChild
- getChild(childIndex: number): DocumentApp.Element
getChildIndex
- getChildIndex(child: DocumentApp.Element): number
getFootnotes
- getFootnotes(): DocumentApp.Footnote[] | null
getHeadingAttributes
- getHeadingAttributes(paragraphHeading: DocumentApp.ParagraphHeading): unknown
getImages
- getImages(): DocumentApp.InlineImage[] | null
getLinkUrl
- getLinkUrl(): string | null
getListItems
- getListItems(): DocumentApp.ListItem[]
getMarginBottom
- getMarginBottom(): number | null
getMarginLeft
- getMarginLeft(): number | null
getMarginRight
- getMarginRight(): number | null
getMarginTop
- getMarginTop(): number | null
getNextSibling
- getNextSibling(): DocumentApp.Element | null
getNumChildren
- getNumChildren(): number
getPageHeight
- getPageHeight(): number | null
getPageWidth
- getPageWidth(): number | null
getParagraphs
- getParagraphs(): DocumentApp.Paragraph[]
getParent
- getParent(): DocumentApp.ContainerElement | null
getPreviousSibling
- getPreviousSibling(): DocumentApp.Element | null
getTables
- getTables(): DocumentApp.Table[]
getText
- getText(): string
getTextAlignment
- getTextAlignment(): DocumentApp.TextAlignment | null
getType
- getType(): DocumentApp.ElementType
insertHorizontalRule
- insertHorizontalRule(childIndex: number): DocumentApp.HorizontalRule
insertImage
- insertImage(childIndex: number, image: BlobSource): DocumentApp.InlineImage
- insertImage(childIndex: number, image: DocumentApp.InlineImage): DocumentApp.InlineImage
insertListItem
- insertListItem(childIndex: number, listItem: DocumentApp.ListItem): DocumentApp.ListItem
- insertListItem(childIndex: number, text: string): DocumentApp.ListItem
insertPageBreak
- insertPageBreak(childIndex: number): DocumentApp.PageBreak
- insertPageBreak(childIndex: number, pageBreak: DocumentApp.PageBreak): DocumentApp.PageBreak
insertParagraph
- insertParagraph(childIndex: number, paragraph: DocumentApp.Paragraph): DocumentApp.Paragraph
- insertParagraph(childIndex: number, text: string): DocumentApp.Paragraph
insertTable
- insertTable(childIndex: number): DocumentApp.Table
- insertTable(childIndex: number, cells: string[][]): DocumentApp.Table
- insertTable(childIndex: number, table: DocumentApp.Table): DocumentApp.Table
isAtDocumentEnd
- isAtDocumentEnd(): boolean
removeChild
- removeChild(child: DocumentApp.Element): DocumentApp.Body
replaceText
- replaceText(searchPattern: string, replacement: string): DocumentApp.Element
setAttributes
- setAttributes(attributes: unknown): DocumentApp.Body
setHeadingAttributes
- setHeadingAttributes(paragraphHeading: DocumentApp.ParagraphHeading, attributes: unknown): DocumentApp.Body
setLinkUrl
- setLinkUrl(url: string): DocumentApp.Body
setMarginBottom
- setMarginBottom(marginBottom: number): DocumentApp.Body
setMarginLeft
- setMarginLeft(marginLeft: number): DocumentApp.Body
setMarginRight
- setMarginRight(marginRight: number): DocumentApp.Body
setMarginTop
- setMarginTop(marginTop: number): DocumentApp.Body
setPageHeight
- setPageHeight(pageHeight: number): DocumentApp.Body
setPageWidth
- setPageWidth(pageWidth: number): DocumentApp.Body
setText
- setText(text: string): DocumentApp.Body
setTextAlignment
- 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