Automatically Add a Horizontal Line Above Each H3 Heading in Google Docs Using Apps Script

Adding a horizontal line with spacing above headings can make a document visually appealing and easier to navigate. This can be especially helpful in long Google Docs where distinct section dividers improve readability. Instead of adding these lines manually, you can automate the task using Google Apps Script!

In this guide, we’ll walk through an Apps Script that finds all H3 headings in your Google Doc and inserts a horizontal line with spacing above each one.

Why Use Google Apps Script?

Google Apps Script is a powerful tool that enables automation and customization within Google Workspace apps, like Google Docs. With just a few lines of code, you can enhance your document structure, add elements, and improve overall formatting.

Updated Apps Script Code to Add a Horizontal Line with Space Above H3 Headings

Here’s the updated code to insert a horizontal line with spacing above each H3 heading:

function addLineAboveH3() {
const doc = DocumentApp.getActiveDocument();
const body = doc.getBody();
const paragraphs = body.getParagraphs();

// Loop from the end to the beginning to avoid shifting issues
for (let i = paragraphs.length - 1; i >= 0; i--) {
const paragraph = paragraphs[i];
// Check if the paragraph is an H3 heading
if (paragraph.getHeading() === DocumentApp.ParagraphHeading.HEADING3) {
// Insert a blank paragraph for spacing above the H3 heading
body.insertParagraph(i, "");
// Insert a horizontal line above the blank paragraph
body.insertHorizontalRule(i);
}
}
}

How the Updated Script Works

  1. Access the Document: The script retrieves the active Google Doc and accesses its main body content.
  2. Loop Through Paragraphs: It iterates over each paragraph in the document.
  3. Identify H3 Headings: The script uses getHeading() to check if a paragraph is an H3 heading (DocumentApp.ParagraphHeading.HEADING3).
  4. Insert Horizontal Line and Spacing: When an H3 heading is found, the script inserts a blank paragraph above for spacing, followed by a horizontal line. This gives a clean, visually separated heading structure.

Setting Up the Script

To use this updated script in Google Docs, follow these steps:

  1. Open Google Docs: Open the Google Doc where you want to add horizontal lines above each H3 heading.
  2. Access Google Apps Script:
    • Go to Extensions > Apps Script to open the Apps Script editor in a new tab.
  3. Paste the Code: Copy the updated script code above and paste it into the editor.
  4. Save and Run:
    • Save your project with a relevant name, like “Add Line Above H3 Headings.”
    • Select addLineAboveH3 from the dropdown and click the Run button.
  5. Authorize Permissions: When prompted, authorize the script to make changes to your Google Docs.

Example Use Case

Imagine your document looks like this:

Introduction
Section 1

H3 Heading
Content of the section.

Another Section

Another H3 Heading
Content continues.

After running the script, a horizontal line with spacing will be added above each H3 heading:

Introduction
Section 1

———————
H3 Heading
Content of the section.

Another Section

———————
Another H3 Heading
Content continues.

This separation makes each section more distinct and enhances the document’s visual flow.

Customizing the Script

If you want to add more or less spacing, you can add multiple blank paragraphs or remove the spacing paragraph. For example, to remove the spacing paragraph:

// Only insert the horizontal line
body.insertHorizontalRule(index);

Conclusion

This simple Google Apps Script allows you to quickly add a horizontal line with spacing above every H3 heading in Google Docs, creating a structured and professional look. This enhancement improves document readability and navigation, especially in longer documents with multiple sections.