Automatically Format Numbered Elements as Headings in Google Docs Using Apps Script

Formatting documents in Google Docs can be tedious, especially when dealing with consistent patterns like numbered lists or structured data. If you’ve ever needed to transform all lines, start with a number and then add a period (e.g., 1., 2., etc.) into properly formatted headings, this blog post is for you.

Using Google Apps Script, you can automate this process, converting such lines into H3 elements with just a few clicks.


What is Google Apps Script?

Google Apps Script is a powerful tool for automating and extending Google Workspace applications. By writing simple JavaScript-based scripts, you can enhance your workflow, saving time and effort on repetitive tasks.


The Script: Transform Numbered Lines into H3 Headings

Here’s the code to identify and format all lines in a Google Docs document that start with a number followed by a period as H3 elements:

function formatNumberedElementsAsHeadings() {
const doc = DocumentApp.getActiveDocument(); // Access the active Google Doc
const body = doc.getBody(); // Get the document's body content
const paragraphs = body.getParagraphs(); // Get all paragraphs in the document

const numberedPattern = /^\d+\./; // Regular expression to match lines starting with a number and period

paragraphs.forEach(paragraph => {
const text = paragraph.getText(); // Get the text of the paragraph
if (numberedPattern.test(text)) { // Check if the text matches the pattern
paragraph.setHeading(DocumentApp.ParagraphHeading.HEADING3); // Convert the paragraph to an H3 heading
}
});

Logger.log("All numbered elements have been formatted as H3 headings.");
}

How It Works

  1. getBody(): Retrieves the body content of the document.
  2. getParagraphs(): Fetches all paragraphs in the document for processing.
  3. Regular Expression (^\d+\.):
    • Matches lines starting with one or more digits followed by a period.
  4. setHeading(): Converts matching paragraphs to H3 headings.

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 provided code into the Apps Script editor.

3. Save and Name Your Project

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

4. Run the Script

  • Select the formatNumberedElementsAsHeadings function from the toolbar and click the play ▶️ button.
  • Grant the necessary permissions.

5. Verify Changes

  • After running the script, all paragraphs starting with a number followed by a period will be transformed into H3 headings.

Practical Applications

  • Organizing Outlines: Quickly structure documents with numbered lists.
  • Formatting Notes: Convert informal notes into structured headings.
  • Consistent Styling: Ensure all numbered elements have a unified appearance.

Customization Tips

  1. Change the Heading Level:
    • Replace HEADING3 with HEADING1, HEADING2, or any other desired level.
  2. Modify the Pattern:
    • Use a different regular expression to match custom patterns, such as Roman numerals (^(I|II|III|IV)\.).
  3. Add Styling:
    • Combine this script with other formatting options like setting font styles or colors.

Automate Further with Triggers

To run this script automatically:

  1. Go to the Apps Script editor.
  2. Click the clock icon ⏰ (Triggers).
  3. Set up a trigger to execute the function on document edits or at regular intervals.