Automate Text Formatting in Google Docs with Google Apps Script

Editing text in Google Docs can sometimes become repetitive and time-consuming, especially when you need to apply specific formatting to certain elements throughout the entire document. Luckily, Google Apps Script allows you to automate many tasks, saving you valuable time and ensuring consistency. Today, we’ll look at a script that does just that: it automatically finds paragraphs beginning with specific words like “Objective:” or “Explanation:” and makes the first word bold, while keeping the rest of the text untouched.

The Purpose of the Script

The function boldParagraphStarters() is designed to enhance the readability of documents by highlighting key phrases, specifically those starting with “Objective:” or “Explanation:”. This is especially helpful when creating structured documents, tutorials, or instructional content where important sections need to stand out.

By using Google Apps Script, you can easily automate this type of formatting and avoid the painstaking process of manually editing each paragraph. This script identifies paragraphs that start with specific key terms and makes the first word bold, helping to visually organize the document for readers.

The Code Walkthrough

Here is the Google Apps Script that performs this task:

function boldParagraphStarters() {
  const doc = DocumentApp.getActiveDocument();
  const body = doc.getBody();
  const paragraphs = body.getParagraphs();

  const targetWords = ['Objective:', 'Explanation:'];
  const pattern = new RegExp(`^(${targetWords.join('|')})\s*(.*)`, 'i');

  paragraphs.forEach(paragraph => {
    let text = paragraph.getText();
    const match = text.match(pattern);
    if (match) {
      const starterWord = match[1].split(':')[0];
      const remainingText = text.substring(starterWord.length + 1).trim();
      paragraph.clear();
      paragraph.appendText(starterWord).setBold(true);
      paragraph.appendText(`: ${remainingText}`);
    }
  });
}

Let’s break down what the script does step-by-step:

  1. Access the Document: The function begins by accessing the active Google Doc using DocumentApp.getActiveDocument(). This retrieves the document where you want to make the changes.
  2. Get the Document Body: Then, the body of the document is accessed, allowing us to manipulate its contents. All paragraphs are retrieved using body.getParagraphs().
  3. Define Target Words: The script focuses on two specific phrases: “Objective:” and “Explanation:”. These are stored in the targetWords array.
  4. Set Up a Regular Expression: To identify the paragraphs that start with one of these phrases, the code creates a regular expression (pattern). This regular expression will match any paragraph starting with “Objective:” or “Explanation:” followed by optional whitespace and other text.
  5. Loop Through the Paragraphs: The script iterates through all paragraphs. If the paragraph matches the specified pattern, the following happens:
    • Extract Starter Word: The matched starter word is extracted and the colon is removed.
    • Clear and Rebuild Paragraph: The paragraph content is cleared, and the starter word is added back with bold formatting. After that, the remaining text is appended, ensuring the original content structure is preserved, but the starter word now stands out.

Benefits of Using This Script

This script is extremely helpful for anyone who needs to create well-structured documents quickly and consistently. Here are some specific benefits:

  • Time-Saving: Instead of manually scrolling through the document and applying formatting, this script completes the process in seconds.
  • Consistency: Ensures that all key sections are formatted the same way, improving the overall quality and readability of the document.
  • Ease of Use: By running this script, you can instantly apply changes to any Google Doc without the need for extensive manual editing.

How to Use the Script

To use this script in Google Docs, follow these simple steps:

  1. Open the Google Doc you want to modify.
  2. Click on Extensions > Apps Script to open the Google Apps Script editor.
  3. Paste the code into the editor and click the Run button.
  4. Authorize the script if prompted.

Once the script has run, you will see that paragraphs beginning with “Objective:” or “Explanation:” now have their initial word in bold, making the document much easier to navigate.

Final Thoughts

Google Apps Script is a powerful tool for automating tasks and boosting productivity when working with Google Docs, Sheets, and other Google Workspace tools. The boldParagraphStarters() function provides a great example of how to automate formatting tasks in a document, saving time while ensuring consistency throughout your work.

If you’d like to further enhance this script or need help with other Google Apps Script tasks, feel free to reach out! Automating repetitive tasks is a great way to streamline your workflow and focus on what matters most.