How do I find and delete blank paragraphs in a Google Doc using Google Apps Script?

I work with Google docs that contain hundreds of blank paragraphs. I want to delete these empty lines automatically.

In LibreOffice Writer, you can use the Find and Replace tool to replace ^$with anything, but this did not work in Google Docs.

My search ^$or ^\s*$returned 0 results, although there should be 3 matches

How to delete blank paragraphs using Google Apps Script?

I already tried body.findText("^$");but returnsnull

function removeBlankParagraphs(doc) {
    var body = doc.getBody();
    result = body.findText("^$");

}
+4
source share
1 answer

, , , , .

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() === ""){
          paras[i].removeFromParent()
       }
}
}
+3

Source: https://habr.com/ru/post/1657299/


All Articles