Automate Google Docs: Remove Numbering from Heading 2 Using Apps Script

If you work with structured documents, you may encounter headings that are automatically numbered, such as:

📌 “207. Create a Dynamic Weather Widget”

However, in many cases, you might want to remove the numbering while keeping the actual heading text intact. Instead of manually editing each heading, you can automate the process using Google Apps Script.

In this post, we’ll explore a simple Apps Script that scans a Google Doc, finds all Heading 2 elements, and removes the leading number and dot while preserving the rest of the text.


Why Automate Heading Cleanup?

  • Saves Time: Instead of manually editing each heading, you can process an entire document in seconds.
  • Ensures Consistency: The script applies the same formatting rule across all headings.
  • Perfect for Imports: If your document was generated from another source with automatic numbering, this script helps clean it up.

The Apps Script Code

Here’s the script that will remove the numbering from all Heading 2 paragraphs in your Google Doc:

function removeNumberFromH2Headings() {
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
if (paragraph.getHeading() === DocumentApp.ParagraphHeading.HEADING2) {
var text = paragraph.getText();

// Use regex to remove the leading number followed by a dot and space
var updatedText = text.replace(/^\d+\.\s*/, '');

// Update the paragraph text
if (updatedText !== text) {
paragraph.setText(updatedText);
}
}
}
}

How the Script Works

  1. Accesses the Current Document
    • The script retrieves the active Google Document where it will process text.
  2. Loops Through All Paragraphs
    • It scans every paragraph in the document.
  3. Identifies Heading 2 Text
    • It checks if the paragraph is styled as Heading 2 using paragraph.getHeading().
  4. Removes Numbering with Regex
    • The script applies this regular expression:swiftCopyEdit/^\d+\.\s*/ This matches:
      • A number (\d+)
      • A dot (.)
      • A space (\s*)
      • And removes them from the start of the heading.
  5. Updates the Heading Text
    • If a change is detected, the text is replaced with the updated version.

Example Before and After

Before Running ScriptAfter Running Script
207. Create a Dynamic Weather WidgetCreate a Dynamic Weather Widget
15. Understanding JavaScript ClosuresUnderstanding JavaScript Closures
3. AI in Web DevelopmentAI in Web Development

How to Use This Script

Follow these simple steps to implement the script in your own Google Document:

Step 1: Open Google Apps Script

  1. Open your Google Doc.
  2. Click on Extensions > Apps Script.

Step 2: Add the Script

  1. Delete any existing code in the editor.
  2. Copy and paste the script above.

Step 3: Run the Script

  1. Click the Run button () in the Apps Script editor.
  2. If prompted, grant permissions to allow the script to modify the document.

Why This Approach Works Well

Fast Execution: Processes all headings in just a few seconds.
Works on Any Document: Can be used on personal documents, reports, or blog drafts.
Customizable: You can modify it to remove numbering from other heading levels (H1, H3, etc.).


Final Thoughts

This script is an excellent solution for cleaning up heading styles in Google Docs, especially when dealing with auto-generated or imported documents. Instead of manually editing each heading, a single script execution handles everything for you.