Automate Heading Updates in Google Docs with Google Apps Script

Large documents often contain structured sections with headings. If you decide to change formatting or restructure your document, manually updating each heading can be a hassle. Google Apps Script lets you:

Automate repetitive tasks – No more manual editing.
Ensure consistency – Keep document formatting uniform.
Save time – Update headings instantly.


🔹 Script 1: Convert “Exercise” Headings from H2 to H3

The following script scans a Google Doc and updates all Heading 2 elements that start with “Exercise”, changing them to Heading 3.

📌 Code:

function updateHeading2ToHeading3() {
var doc = DocumentApp.getActiveDocument();
var body = doc.getBody();
var paragraphs = body.getParagraphs();

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

// Check if the paragraph is Heading 2 and starts with "Exercise"
if (paragraph.getHeading() === DocumentApp.ParagraphHeading.HEADING2 &&
paragraph.getText().startsWith("Exercise")) {
paragraph.setHeading(DocumentApp.ParagraphHeading.HEADING3);
}
}

Logger.log("Updated all Heading 2s that start with 'Exercise' to Heading 3.");
}

🛠 How It Works:

  • The script retrieves the active document.
  • It loops through all paragraphs in the document.
  • It checks if a paragraph:
    • Is Heading 2.
    • Starts with “Exercise”.
  • If both conditions are met, it updates the heading to Heading 3.

🔹 Script 2: Convert “Explanation” Headings from H3 to H4

The next script updates all Heading 3 elements that contain only the text “Explanation”, changing them to Heading 4.

📌 Code:

function updateHeading3ToHeading4() {
var doc = DocumentApp.getActiveDocument();
var body = doc.getBody();
var paragraphs = body.getParagraphs();

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

// Check if the paragraph is Heading 3 and has the exact text "Explanation"
if (paragraph.getHeading() === DocumentApp.ParagraphHeading.HEADING3 &&
paragraph.getText().trim() === "Explanation") {
paragraph.setHeading(DocumentApp.ParagraphHeading.HEADING4);
}
}

Logger.log("Updated all Heading 3s with 'Explanation' to Heading 4.");
}

🛠 How It Works:

  • The script retrieves the active Google Doc.
  • It loops through all paragraphs.
  • It checks if a paragraph:
    • Is Heading 3.
    • Contains exactly the text “Explanation”.
  • If both conditions are met, it updates the heading to Heading 4.

🔥 How to Use These Scripts

  1. Open your Google Doc.
  2. Click Extensions > Apps Script.
  3. Delete any existing code and paste one of the scripts above.
  4. Click Run (play button) to execute the script.
  5. Check the Logger (View > Logs) to confirm the updates.

🏆 Benefits of Using These Scripts

Increased Productivity – No need to manually scan and edit headings.
Consistent Formatting – Ensures uniform heading levels across your document.
Quick and Easy Implementation – No prior coding knowledge required!

By automating heading updates, you can focus on content creation while letting Google Apps Script handle the formatting. 🚀

Have More Formatting Tasks?

Google Apps Script can be extended to automate many other text and formatting updates. If you need custom automation, feel free to experiment with these scripts or ask for more enhancements!