How to Add Space and a Horizontal Line Above All H1 Headings in Google Docs Using Google Apps Script

If you’re looking to enhance the formatting of your Google Docs by adding a space and a horizontal line above all H1 headings, you’re in the right place! In this tutorial, we’ll walk you through creating a Google Apps Script to automate this task.

function addSpaceAndLineAboveH1() {
  var doc = DocumentApp.getActiveDocument();
  var body = doc.getBody();
  
  var paragraphs = body.getParagraphs();
  
  for (var i = 0; i < paragraphs.length; i++) {
    var paragraph = paragraphs[i];
    
    if (paragraph.getHeading() == DocumentApp.ParagraphHeading.HEADING1) {
      var index = body.getChildIndex(paragraph);
      
      // Add a blank paragraph (space) above the H1
      var space = body.insertParagraph(index, "");
      
      // Add a horizontal line above the space
      var horizontalLine = body.insertHorizontalRule(index);
      
      // Set the space paragraph to NORMAL text (not heading)
      space.setHeading(DocumentApp.ParagraphHeading.NORMAL);
    }
  }
}

Step-by-Step Guide

  1. Open Your Google Doc: Start by opening the Google Doc you want to modify.
  2. Access Google Apps Script: Navigate to Extensions -> Apps Script. This will open the script editor.
  3. Delete Existing Code: If there’s any pre-existing code in the script editor, you can delete it to start fresh.
  4. Add the Script: Copy and paste the following code into the script editor:function addSpaceAndLineAboveH1() { var doc = DocumentApp.getActiveDocument(); var body = doc.getBody(); var paragraphs = body.getParagraphs(); for (var i = 0; i < paragraphs.length; i++) { var paragraph = paragraphs[i]; if (paragraph.getHeading() == DocumentApp.ParagraphHeading.HEADING1) { var index = body.getChildIndex(paragraph); // Add a blank paragraph (space) above the H1 var space = body.insertParagraph(index, ""); // Add a horizontal line above the space var horizontalLine = body.insertHorizontalRule(index); // Set the space paragraph to NORMAL text (not heading) space.setHeading(DocumentApp.ParagraphHeading.NORMAL); } } }
  5. Save the Script: Save your script with a descriptive name like “AddSpaceAndLineAboveH1”.
  6. Run the Script: Click the play button (triangle icon) in the toolbar to run the script. You may be prompted to authorize the script to make changes to your document. Follow the authorization steps.

How the Script Works

This script iterates through all the paragraphs in your document. It checks if each paragraph is an H1 heading. If an H1 heading is found, the script inserts a blank paragraph (acting as space) and a horizontal line above it. The blank paragraph is set to normal text to ensure it doesn’t inherit any heading properties.

Here’s a breakdown of the main parts of the script:

  • Get the Active Document:var doc = DocumentApp.getActiveDocument(); var body = doc.getBody();
  • Loop Through Paragraphs:var paragraphs = body.getParagraphs();
  • Check for H1 Headings and Insert Elements:if (paragraph.getHeading() == DocumentApp.ParagraphHeading.HEADING1) { var index = body.getChildIndex(paragraph); var space = body.insertParagraph(index, ""); var horizontalLine = body.insertHorizontalRule(index); space.setHeading(DocumentApp.ParagraphHeading.NORMAL); }

Conclusion

By following these steps, you can easily add a space and a horizontal line above all H1 headings in your Google Docs, making your document look cleaner and more organized. This Google Apps Script automation saves you time and ensures consistent formatting throughout your document.