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
paragraph.setHeading(DocumentApp.ParagraphHeading.HEADING2);
}
}
}

Step-by-Step Guide
Step 1: Open Google Apps Script
- Open your Google Doc.
- Click on
Extensions
>Apps Script
.
Step 2: Add the Script
- Delete any existing code in the script editor and replace it with the provided code.
- Save the script with a descriptive name, such as
ChangeH3ToH2
.
Step 3: Run the Script
- Click the play button (Run) to execute the script.
- Grant the necessary permissions if prompted.
How It Works
- Access the Document: The script starts by getting the active Google Doc and its body.
- Get Paragraphs: It retrieves all the paragraphs in the document body.
- Iterate Through Paragraphs: The script loops through all the paragraphs in the document.
- Identify H3 Headings: It checks if a paragraph is an H3 heading.
- Change H3 to H2: If an H3 heading is found, the script changes the heading to H2.
Benefits
- 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.
Conclusion
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.
