I am working with Google documents that contain hundreds of empty paragraphs. I want to remove these blank lines automatically.
In LibreOffice Writer you can use the Find & Replace tool to replace ^$
with nothing, but that didn't work in Google Docs.
My search for ^$
or ^\s*$
returned 0 results even though there should be 3 matches
How can I remove the blank paragraphs with Google Apps Script?
I already tried body.findText("^$");
, but that returns null
function removeBlankParagraphs(doc) {
var body = doc.getBody();
result = body.findText("^$");
}
function myFunction() { var body = DocumentApp.getActiveDocument().getBody(); var paras = body.getParagraphs(); var i = 0; for (var i = 0; i < paras.length; i++) { if (paras[i].getText() === ""){ if (paras[i].findElement(DocumentApp.ElementType.INLINE_IMAGE,null) === null) { paras[i].removeFromParent();} } } }
– Pooley