Google Apps Script offers powerful capabilities for automating tasks within Google Docs. Imagine automatically generating reports, creating standardized documents, or applying consistent formatting across multiple files. This section will guide you through the essentials of programmatically creating and formatting Google Docs, saving you significant time and reducing manual effort.
The core of interacting with Google Docs in Apps Script is the DocumentApp service. This service provides access to the user's Google Docs and allows you to create, open, and manipulate them. We'll start by learning how to create a brand new document.
function createNewDocument() {
var doc = DocumentApp.create('My New Automated Document');
Logger.log('Created document: ' + doc.getUrl());
}When you run this script, a new Google Doc titled 'My New Automated Document' will be created in your Google Drive. The doc.getUrl() method is incredibly useful for quickly accessing the newly created document.
Once a document is created, you'll want to add content. Google Docs are structured with a body, which contains paragraphs, headings, lists, and other elements. The DocumentApp service provides methods to access and manipulate this body.
function addContentToDocument() {
var doc = DocumentApp.create('Document with Content');
var body = doc.getBody();
body.appendParagraph('This is the first paragraph.');
body.appendParagraph('This is a second paragraph, added automatically.');
Logger.log('Document with content created: ' + doc.getUrl());
}The appendParagraph() method is your go-to for adding new text blocks to the document. You can call it multiple times to build up your document's content.
Formatting is where Apps Script truly shines in streamlining workflows. You can control font styles, sizes, colors, alignment, and even create lists and tables. Let's explore some common formatting options.
function formatDocument() {
var doc = DocumentApp.create('Formatted Document');
var body = doc.getBody();
var title = body.appendParagraph('Automated Report Title');
title.setHeading(DocumentApp.ParagraphHeading.TITLE);
var sectionHeading = body.appendParagraph('Key Findings');
sectionHeading.setHeading(DocumentApp.ParagraphHeading.HEADING1);
var normalText = body.appendParagraph('This text is in the default font and size.');
normalText.setFontFamily('Arial').setFontSize(12).setForegroundColor('#3366cc');
var emphasizedText = body.appendParagraph('This text is bold and italic.');
emphasizedText.setBold(true).setItalic(true);
Logger.log('Formatted document created: ' + doc.getUrl());
}In this example, we use methods like setHeading(), setFontFamily(), setFontSize(), setForegroundColor(), setBold(), and setItalic() to apply various styles. You can get a specific paragraph or text element and then apply these formatting methods to it.
Applying formatting to specific ranges of text is also common. You can get a range of text within a paragraph and format it individually.
function formatSpecificText() {
var doc = DocumentApp.create('Specific Text Formatting');
var body = doc.getBody();
var paragraph = body.appendParagraph('This is a sentence with some important words highlighted.');
// Find the range for 'important words'
var keywordRange = paragraph.editAsText().findText('important words');
if (keywordRange) {
var textElement = keywordRange.getElement().asText();
textElement.setBold(true);
textElement.setForegroundColor('#ff0000'); // Red color
}
Logger.log('Document with specific text formatting created: ' + doc.getUrl());
}The editAsText() method allows you to treat the paragraph as a mutable text object, enabling methods like findText() to locate specific substrings. Once found, you can format that range as needed.
graph TD
A[Start Script] --> B{Create Document};
B --> C{Get Document Body};
C --> D[Append Paragraphs];
D --> E[Apply Formatting];
E --> F{Save Document};
F --> G[End Script];
By mastering these techniques, you can move beyond simple document creation to building sophisticated automated workflows that populate Google Docs with pre-formatted, data-driven content. This is a foundational step towards significant productivity gains.