Automate Your Google Docs: Changing H3 Headings to H2 Headings with Google Apps Script

Maintaining a consistent heading structure in your Google Docs is crucial for readability and organization, especially in lengthy documents. If you find yourself needing to change all H3 headings to H2 headings, doing it manually can be tedious and time-consuming. Luckily, Google Apps Script allows you to automate this process. In this blog post, we’ll show you how to use a simple script to update all H3 headings to H2 headings in your Google Docs.

Here’s the complete changeH3ToH2 function:

function changeH3ToH2() {
// Get the active document
var doc = DocumentApp.getActiveDocument();
var body = doc.getBody();

// Get all the paragraphs in the document
var paragraphs = body.getParagraphs();

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

// Check if the paragraph is an H3
if (paragraph.getHeading() === DocumentApp.ParagraphHeading.HEADING3) {
// Change the heading to H2

Step-by-Step Guide

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 script editor and replace it with the provided code.
  2. Save the script with a descriptive name, such as ChangeH3ToH2.

Step 3: Run the Script

  1. Click the play button (Run) to execute the script.
  2. Grant the necessary permissions if prompted.

How It Works

  1. Access the Document: The script starts by getting the active Google Doc and its body.
  2. Get Paragraphs: It retrieves all the paragraphs in the document body.
  3. Iterate Through Paragraphs: The script loops through all the paragraphs in the document.
  4. Identify H3 Headings: It checks if a paragraph is an H3 heading.
  5. Change H3 to H2: If an H3 heading is found, the script changes the heading to H2.


  • Efficiency: Automates the repetitive task of changing heading styles.
  • Consistency: Ensures consistent heading formatting throughout your document.
  • Time-Saving: Reduces the time spent on manual editing, allowing you to focus on more important tasks.


Using Google Apps Script to automate tasks in Google Docs can significantly enhance your productivity. The changeH3ToH2 function is a simple yet effective script that helps you maintain a well-organized and consistently formatted document effortlessly. Feel free to customize and expand this script to suit your specific needs.