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:
- 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
addBlankLineAboveBoldParagraphs
from the script editor.
This will add a blank line directly above any paragraph that contains bold text in your document.