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:
- Open your Google Document.
- Click on “Extensions” > “Apps Script.”
- Delete any code in the script editor and replace it with the provided script.
- Save the script.
- 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.