Apps script to add blank paragraph above all paragraphs that end with a question mark

You can achieve this by iterating through all paragraphs, checking if they end with a question mark, and if so, inserting a blank paragraph above them. Here’s the script:

function addBlankParagraphAboveQuestionParagraphs() {
  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();
    
    // Check if the paragraph ends with a question mark
    if (text.slice(-1) === '?') {
      // Insert a blank paragraph above
      body.insertParagraph(i, '');
    }
  }
}

This script iterates through all paragraphs in the document. If a paragraph ends with a question mark, it inserts a blank paragraph 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 addBlankParagraphAboveQuestionParagraphs from the script editor.

This will add a blank paragraph above all paragraphs that end with a question mark in your document.