Skip to content

Snippets · 11 functions

Google Docs snippets

Eleven short functions for the Docs tasks that come up again and again: creating, writing, and reading content.

How to use these

Set up before you run

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

Create a document and log its URL

Make a brand-new Google Doc and log a link to it, useful the first time you generate a document from a script.

Code.gs
function createNewDocument() {
  const doc = DocumentApp.create('Generated Report');
  Logger.log(doc.getUrl());
}

2

Open a document by ID

Open an existing Doc using its file ID so a script can read or change its content.

Code.gs
function openExistingDocument() {
  const documentId = 'REPLACE_WITH_DOCUMENT_ID';
  const doc = DocumentApp.openById(documentId);
  Logger.log('Opened: ' + doc.getName());
}

3

Append a heading and a paragraph

Add a Heading 1 title followed by a normal paragraph to the end of a document’s first tab.

DocumentApp.BodyDocumentApp.Paragraph
Code.gs
function 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

Append a bullet list

Add a list of items to a document’s first tab, each shown with a bullet glyph.

Code.gs
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

Replace placeholder text

Find placeholder text like {{Name}} in a document’s first tab and replace it with a real value, such as when filling in a template.

Code.gs
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

Insert a table from a 2D array

Turn a 2D array, the same shape you get from a Sheet, into a table at the end of a document’s first tab.

Code.gs
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

Insert a page break

Force the content that follows in a document’s first tab onto a new printed page.

Code.gs
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

Get all body text

Read the entire text content of a document’s first tab as one plain string.

Code.gs
function logAllBodyText() {
  const documentId = 'REPLACE_WITH_DOCUMENT_ID';
  const body = DocumentApp.openById(documentId).getBody();
  Logger.log(body.getText());
}

9

Set a paragraph's alignment

Center a paragraph in a document’s first tab, such as a title, instead of leaving it aligned to the left.

DocumentApp.Paragraph
Code.gs
function 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

Append an image from a Drive file blob

Insert an image stored in Drive into a document’s first tab by passing its file blob.

Code.gs
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

Count paragraphs

Count how many paragraphs a document’s first tab contains, useful for a quick length check.

DocumentApp.BodyDocumentApp.Paragraph
Code.gs
function 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

See these in a complete task

All snippet collections