Google Apps Script to Automatically Update Heading Styles in Google Docs

Do you find yourself manually updating heading styles in your Google Docs to keep things consistent? If so, you’re not alone—and luckily, there’s a simple way to automate the process using Google Apps Script.

In this tutorial, you’ll learn how to write a script that updates all Heading 4 elements to Heading 2, and then updates all Heading 3 elements to Heading 1—ensuring your document follows a proper structure with just one click.


🚀 What You’ll Learn

  • How to programmatically access and modify headings in Google Docs
  • The correct order to perform heading updates to prevent overlap or missed changes
  • How to run your Apps Script directly inside your document

📋 Use Case

Imagine a scenario where your document has been formatted inconsistently:

  • Some section titles use Heading 4 instead of Heading 2
  • Sub-sections use Heading 3 instead of Heading 1

Manually changing these could take time—especially in longer documents. Here’s how to automate it.


🧩 The Script

Below is a Google Apps Script that performs the two-step transformation. It ensures Heading 4 → Heading 2 is completed before transforming Heading 3 → Heading 1.

function updateHeadingsInDoc() {
const body = DocumentApp.getActiveDocument().getBody();
const totalElements = body.getNumChildren();

// Step 1: Update Heading 4 to Heading 2
for (let i = 0; i < totalElements; i++) {
const element = body.getChild(i);
if (element.getType() === DocumentApp.ElementType.PARAGRAPH) {
const paragraph = element.asParagraph();
if (paragraph.getHeading() === DocumentApp.ParagraphHeading.HEADING4) {
paragraph.setHeading(DocumentApp.ParagraphHeading.HEADING2);
}
}
}

// Step 2: Update Heading 3 to Heading 1
for (let i = 0; i < totalElements; i++) {
const element = body.getChild(i);
if (element.getType() === DocumentApp.ElementType.PARAGRAPH) {
const paragraph = element.asParagraph();
if (paragraph.getHeading() === DocumentApp.ParagraphHeading.HEADING3) {
paragraph.setHeading(DocumentApp.ParagraphHeading.HEADING1);
}
}
}
}

🧪 How to Use This Script

  1. Open your Google Docs document.
  2. Go to Extensions > Apps Script.
  3. Delete any existing code in the editor and paste in the script above.
  4. Save the script.
  5. Click the ▶️ Run button and authorize the script if prompted.

That’s it! Your document will now reflect the updated heading structure automatically.


✅ Why This Order Matters

It’s important to update Heading 4 to Heading 2 before changing Heading 3 to Heading 1, so you don’t accidentally convert newly transformed headings again. This order avoids logic conflicts and ensures precise formatting updates.


💡 Bonus Tip

You can extend this script to:

  • Update multiple documents from a Google Drive folder
  • Set custom heading styles
  • Create a menu item inside the Docs UI for one-click formatting

Want a tutorial on that? Drop a comment or reach out!


📚 Final Thoughts

With just a bit of Google Apps Script, you can take full control of how your documents are formatted. Whether you’re managing academic papers, technical documentation, or business reports—automation saves you time and keeps your work clean and consistent.

Happy scripting! ✨