How to find and remove blank paragraphs in a Google Document with Google Apps Script?

后端 未结 2 814
梦如初夏
梦如初夏 2020-12-21 03:55

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 th

2条回答
  •  醉话见心
    2020-12-21 04:34

    I think there has to be a last empty paragraph but this seems to work.

    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()
           }
    }
    }
    

提交回复
热议问题