Apps script to add a blank line directly above any paragraph that is bold

You can achieve this by iterating through all paragraphs, checking if they contain bold text, and if so, inserting a blank paragraph directly above them. Here’s the script:

function addBlankLineAboveBoldParagraphs() {
  var doc = DocumentApp.getActiveDocument();
  var body = doc.getBody();
  var paragraphs = body.getParagraphs();

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

    // Check if the paragraph contains bold text
    if (textStyle[DocumentApp.Attribute.BOLD]) {
      // Insert a blank paragraph directly above
      body.insertParagraph(i, '');
    }
  }
}


This script iterates through all paragraphs in the document. If a paragraph contains bold text, it inserts a blank paragraph directly above it.

To use this script:

  1. Open your Google Document.
  2. Click on “Extensions” > “Apps Script.”
  3. Delete any code in the script editor and replace it with the provided script.
  4. Save the script.
  5. Run the function addBlankLineAboveBoldParagraphs from the script editor.

This will add a blank line directly above any paragraph that contains bold text in your document.