Skip to content

Snippets · 11 functions

Google Docs snippets

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

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

Creates a Google Doc and logs a link to it.

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

2

Open a document by ID

Opens an existing Doc by 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

Adds a Heading 1 title and a normal paragraph to the end of the 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

Adds a bulleted list of items to the first tab.

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

Finds placeholder text like {{Name}} in the first tab and replaces 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

Turns a 2D array, the same shape you get from a Sheet, into a table at the end of the 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

Adds a page break and then a paragraph to the end of the first tab, so the paragraph starts on 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

Reads all the text in the 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

Adds a paragraph to the end of the first tab and centers it, as you might for a title.

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

Inserts an image stored in Drive into the 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

Logs the number of paragraphs in the first tab, a quick way to check the length of a document.

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