Automating Document Formatting: Convert and Update Heading 3 to Heading 2 in Google Docs

Organizing a Google Doc can often involve repetitive tasks like updating headings or restructuring content. One such common task is converting specific headings to a different style while modifying their text content. In this blog post, we’ll explore a handy Google Apps Script that automatically updates Heading 3 elements to Heading 2, transforms their text format, and ensures consistency across your document.


The Problem: Managing Unstructured Headings

Imagine a document with multiple Heading 3 elements that follow a format like 22. Security Practices. You might want to:

  • Remove the leading number and period.
  • Prepend the text with Category:.
  • Update the heading style to Heading 2 for uniformity.

Manually performing these tasks in a large document is tedious. This is where automation comes in.


The Solution: Google Apps Script

The following Google Apps Script takes care of this task, identifying Heading 3 elements, reformatting their text, and converting them to Heading 2.


The Code

function updateHeading3ToCategoryHeading2() {
const DOCID = 'YOUR_DOCUMENT_ID_HERE'; // Replace with your Document ID
const doc = DocumentApp.openById(DOCID);
const body = doc.getBody();
const paragraphs = body.getParagraphs();

for (let i = 0; i < paragraphs.length; i++) {
const paragraph = paragraphs[i];

// Check if the paragraph is Heading 3
if (paragraph.getHeading() === DocumentApp.ParagraphHeading.HEADING3) {
const text = paragraph.getText().trim();

// Match patterns like "22. Security Practices" (number, period, space, text)
const match = text.match(/^(\d+)\.\s+(.*)$/);
if (match) {
const newText = `Category: ${match[2]}`; // Remove number and period, prepend 'Category: '

paragraph.setText(newText); // Update the text
paragraph.setHeading(DocumentApp.ParagraphHeading.HEADING2); // Change to Heading 2

Logger.log(`Updated: "${text}" to "${newText}" and set as Heading 2`);
}
}
}

Logger.log("Processed all Heading 3 paragraphs.");
}

How It Works

  1. Target Heading 3:
    • The script identifies paragraphs styled as Heading 3 using paragraph.getHeading().
  2. Regex Matching:
    • It uses the regex ^(\d+)\.\s+(.*)$ to identify headings starting with a number followed by a period and space (e.g., 22. Security Practices).
  3. Text Transformation:
    • Removes the number and period, then prepends Category: to the remaining text.
  4. Update Heading Style:
    • Changes the heading style from Heading 3 to Heading 2.
  5. Logging:
    • Logs each transformation for easy verification.

Example

Before:

Heading 3: 22. Security Practices
Heading 3: 45. JavaScript Basics

After:

Heading 2: Category: Security Practices
Heading 2: Category: JavaScript Basics

How to Use

  1. Set Up Apps Script:
    • Open your Google Drive, click on Extensions > Apps Script, and paste the code into the script editor.
  2. Provide the Document ID:
    • Replace YOUR_DOCUMENT_ID_HERE with the ID of your Google Doc. The ID is the part of the document URL between /d/ and /edit.
  3. Run the Script:
    • Click the Run button in the Apps Script editor. You may need to authorize the script on first use.
  4. Verify Changes:
    • Open your Google Doc to see the updated headings.

Why Automate?

Automation eliminates repetitive, error-prone tasks, saving you time and effort. By using Google Apps Script, you can ensure consistent formatting in your documents, especially when dealing with large amounts of content.


Takeaway

This script is a perfect example of how simple automation can solve tedious problems in document management. Whether you’re working on technical documentation, educational materials, or reports, automating tasks like heading updates ensures efficiency and consistency.