如何使用Google Apps脚本查找并删除Google文档中的空白段落? [英] How to find and remove blank paragraphs in a Google Document with Google Apps Script?

查看:117
本文介绍了如何使用Google Apps脚本查找并删除Google文档中的空白段落?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在处理包含数百个空白段落的Google文档。我想自动删除这些空行。

在LibreOffice Writer中,您可以使用Find&替换工具以将 ^ $ 替换为空,但在Google文档中无法使用。



我搜索 ^ $ ^ \s * $ 即使应该有3个匹配,也会返回0个结果



如何删除空白段落Apps Script?



我已经试过 body.findText(^ $); ,但是返回 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; $(b
$ b for(var i = 0; i< paras.length; i ++){
if(paras [i] .getText()===){
para [i] .removeFromParent()
}
}
}


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("^$");

}

解决方案

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

这篇关于如何使用Google Apps脚本查找并删除Google文档中的空白段落?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆