Automate Text Styling in Google Docs: Bold Questions with Google Apps Script

When working on documents such as quizzes, FAQs, or study guides, it’s common to have questions labeled with a specific format, like “Question 1”, “Question 2”, etc. Manually bolding these question labels can be tedious, especially in lengthy documents. But with Google Apps Script, you can automate this task effortlessly!

In this post, we’ll create a script that identifies all text elements in a Google Docs document starting with the word “Question” followed by a number (e.g., Question 1) and applies bold styling to them.


What is Google Apps Script?

Google Apps Script is a cloud-based JavaScript platform that allows you to enhance and automate Google Workspace apps like Docs, Sheets, and Slides. It’s perfect for customizing workflows and performing repetitive tasks.


The Script: Bold Text Elements Starting with “Question”

Here’s the code to automate the bolding of elements starting with “Question” followed by a number:

javascriptCopy codefunction boldQuestions() {
  const doc = DocumentApp.getActiveDocument(); // Access the current Google Doc
  const body = doc.getBody(); // Get the document's body content
  const paragraphs = body.getParagraphs(); // Get all paragraphs in the document

  const questionPattern = /^Question \d+/; // Regular expression to match "Question 1", "Question 2", etc.

  paragraphs.forEach(paragraph => {
    const text = paragraph.getText(); // Get the text of the paragraph
    if (questionPattern.test(text)) { // Check if the text matches the pattern
      const startIndex = text.indexOf("Question"); // Find the starting position of "Question"
      const endIndex = text.indexOf(' ', startIndex + 9) || text.length; // Find the end of the "Question X" substring
      paragraph.editAsText().setBold(startIndex, endIndex - 1, true); // Apply bold styling
    }
  });

  Logger.log("All 'Question' elements have been bolded.");
}

How It Works

  1. getBody(): Retrieves the body content of the document.
  2. getParagraphs(): Fetches all paragraphs for analysis.
  3. Regular Expression (/^Question \d+/):
    • Matches lines starting with “Question” followed by a number.
  4. editAsText() and setBold():
    • Converts the paragraph to a text object and applies bold styling to the matched substring.

Step-by-Step Guide to Use the Script

1. Open the Apps Script Editor

  • In your Google Docs, go to Extensions > Apps Script.

2. Add the Script

  • Paste the above script into the Apps Script editor.

3. Save and Name Your Project

  • Click the save icon and name the project (e.g., BoldQuestions).

4. Run the Script

  • From the toolbar, select the boldQuestions function and click the play ▶️ button.
  • Grant the necessary permissions when prompted.

5. Check Your Document

  • All text elements starting with Question followed by a number will now be bolded.

Customization Options

  1. Change the Matching Pattern:
    • Modify the regular expression to match different text patterns, such as “Exercise 1”, “Task 1”, etc.
  2. Apply Additional Styling:
    • Add other formatting options like font size, color, or italics using the editAsText() method.
  3. Bold Entire Paragraphs:
    • If you want to bold the entire paragraph instead of just the matched text, use the setBold(true) method on the paragraph object.