Automate Your Google Docs: Merging H3 Paragraphs with Following Text Using Google Apps Script

If you’ve ever found yourself needing to merge specific headings in your Google Docs with the following paragraph, you’re not alone. Whether it’s for organizing content, creating structured documents, or simply for aesthetic reasons, this task can be tedious when done manually. Fortunately, Google Apps Script offers a powerful way to automate this process. In this blog post, we’ll walk through a script that selects H3 headings in your document, appends the subsequent paragraph’s text to the H3, and removes the now redundant paragraph.

The Script

Here’s a breakdown of the appendFollowingParagraphToH3 function:

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

// Get all the elements in the document
var totalElements = body.getNumChildren();

// Loop through the elements
for (var i = 0; i < totalElements; i++) {
var element = body.getChild(i);

// Check if the element is a paragraph and if it's an H3
if (element.getType() == DocumentApp.ElementType.PARAGRAPH) {
var paragraph = element.asParagraph();
if (paragraph.getHeading() === DocumentApp.ParagraphHeading.HEADING3) {
// Check if the next element exists and is a paragraph
if (i + 1 < totalElements) {
var nextElement = body.getChild(i + 1);
if (nextElement.getType() == DocumentApp.ElementType.PARAGRAPH) {
var nextParagraph = nextElement.asParagraph();

// Append the text of the next paragraph to the H3 paragraph
paragraph.appendText(' ' + nextParagraph.getText());

// Remove the next paragraph

// Decrement the total elements count

Step-by-Step Guide

Step 1: Access Google Apps Script

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

Step 2: Add the Script

  1. Delete any code in the script editor and replace it with the provided code.
  2. Save the script with a descriptive name, like AppendFollowingParagraphToH3.

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. Iterate Through Elements: It then loops through all the elements in the document.
  3. Identify H3 Headings: Within the loop, it checks if an element is a paragraph and if it’s an H3 heading.
  4. Merge Paragraphs: If an H3 heading is found, it checks if the next element is a paragraph. If so, it appends the text of the next paragraph to the H3 and removes the redundant paragraph.
  5. Adjust Index: The script adjusts the total elements count to account for the removed paragraph, ensuring smooth iteration.


  • Time-Saving: Automates a repetitive task, saving you time.
  • Consistency: Ensures consistent formatting throughout your document.
  • Efficiency: Reduces the risk of manual errors.


Using Google Apps Script to automate tasks in Google Docs can significantly enhance your productivity. The appendFollowingParagraphToH3 function is a simple yet powerful script that can streamline your document editing process. Feel free to customize and expand upon this script to suit your specific needs.