8
Get all body text
Reads all the text in the first tab as one plain string.
function logAllBodyText() {
const documentId = 'REPLACE_WITH_DOCUMENT_ID';
const body = DocumentApp.openById(documentId).getBody();
Logger.log(body.getText());
} Snippets · 11 functions
Eleven short functions that create Google Docs and write or read their content. The functions that open a document by ID work on its first tab.
How to use these
Copy a snippet into a script file, read its setup and notes, change the placeholder values, and save. For functions meant to run manually, choose the function name in the toolbar and press Run. Results appear in the Execution log unless the description says otherwise.
1
Creates a Google Doc and logs a link to it.
function createNewDocument() {
const doc = DocumentApp.create('Generated Report');
Logger.log(doc.getUrl());
} 2
Opens an existing Doc by its file ID so a script can read or change its content.
function openExistingDocument() {
const documentId = 'REPLACE_WITH_DOCUMENT_ID';
const doc = DocumentApp.openById(documentId);
Logger.log('Opened: ' + doc.getName());
} 3
Adds a Heading 1 title and a normal paragraph to the end of the first tab.
DocumentApp.BodyDocumentApp.Paragraphfunction appendHeadingAndParagraph() {
const documentId = 'REPLACE_WITH_DOCUMENT_ID';
const body = DocumentApp.openById(documentId).getBody();
body.appendParagraph('Weekly Summary').setHeading(DocumentApp.ParagraphHeading.HEADING1);
body.appendParagraph('This section summarizes the week\'s activity.');
Logger.log('Heading and paragraph added.');
} 4
Adds a bulleted list of items to the first tab.
function appendBulletList() {
const documentId = 'REPLACE_WITH_DOCUMENT_ID';
const body = DocumentApp.openById(documentId).getBody();
const items = ['First item', 'Second item', 'Third item'];
items.forEach((item) => {
body.appendListItem(item).setGlyphType(DocumentApp.GlyphType.BULLET);
});
Logger.log('Added ' + items.length + ' bullet items.');
} 5
Finds placeholder text like {{Name}} in the first tab and replaces it with a real value, such as when filling in a template.
function replaceNamePlaceholder() {
const documentId = 'REPLACE_WITH_DOCUMENT_ID';
const body = DocumentApp.openById(documentId).getBody();
const customerName = 'Jamie Rivera';
body.replaceText('\\{\\{Name\\}\\}', customerName);
Logger.log('Replaced the Name placeholder.');
} Good to know. replaceText treats the search pattern as a regular expression, so curly braces must be escaped as shown.
6
Turns a 2D array, the same shape you get from a Sheet, into a table at the end of the first tab.
function appendTableFromArray() {
const documentId = 'REPLACE_WITH_DOCUMENT_ID';
const body = DocumentApp.openById(documentId).getBody();
const data = [
['Name', 'Amount'],
['Jamie Rivera', '120'],
['Alex Chen', '95']
];
body.appendTable(data);
Logger.log('Table added with ' + data.length + ' rows.');
} 7
Adds a page break and then a paragraph to the end of the first tab, so the paragraph starts on a new printed page.
function appendPageBreak() {
const documentId = 'REPLACE_WITH_DOCUMENT_ID';
const body = DocumentApp.openById(documentId).getBody();
body.appendPageBreak();
body.appendParagraph('This paragraph starts on a new page.');
Logger.log('Page break added.');
} 8
Reads all the text in the first tab as one plain string.
function logAllBodyText() {
const documentId = 'REPLACE_WITH_DOCUMENT_ID';
const body = DocumentApp.openById(documentId).getBody();
Logger.log(body.getText());
} 9
Adds a paragraph to the end of the first tab and centers it, as you might for a title.
DocumentApp.Paragraphfunction centerTitleParagraph() {
const documentId = 'REPLACE_WITH_DOCUMENT_ID';
const body = DocumentApp.openById(documentId).getBody();
const title = body.appendParagraph('Centered Title');
title.setAlignment(DocumentApp.HorizontalAlignment.CENTER);
Logger.log('Title centered.');
} 10
Inserts an image stored in Drive into the first tab by passing its file blob.
function appendImageFromDrive() {
const documentId = 'REPLACE_WITH_DOCUMENT_ID';
const imageFileId = 'REPLACE_WITH_IMAGE_FILE_ID';
const body = DocumentApp.openById(documentId).getBody();
const imageBlob = DriveApp.getFileById(imageFileId).getBlob();
body.appendImage(imageBlob);
Logger.log('Image added.');
} 11
Logs the number of paragraphs in the first tab, a quick way to check the length of a document.
DocumentApp.BodyDocumentApp.Paragraphfunction logParagraphCount() {
const documentId = 'REPLACE_WITH_DOCUMENT_ID';
const body = DocumentApp.openById(documentId).getBody();
const paragraphs = body.getParagraphs();
Logger.log('Paragraph count: ' + paragraphs.length);
} Keep going
Create one short Google Doc for each row on a Projects tab that has no document link yet. The script writes the link to the new Doc back into the row.
Make one filled-in copy of a template Doc for each row on a Letters tab. The script swaps {{Name}}, {{Date}}, and {{Amount}} for the values in the row and writes the link to the copy in column D.
Send a Google Doc to someone as a PDF attachment. Paste the file ID of the Doc into the script, and it builds the PDF and emails it. The Doc itself is not changed.
Before a script can read your sheet or send your mail, you give it permission to access a list of OAuth scopes. This page walks through each authorization screen, explains @OnlyCurrentDoc, and shows how to revoke access.
Apps Script error messages say what failed, for example TypeError: Cannot read properties of null (reading 'getRange') or ReferenceError. Find the exact message below for its cause and fix.