【问题标题】:Google app script search and replace delete empty line in several documents谷歌应用脚​​本搜索和替换删除多个文档中的空行
【发布时间】:2021-05-06 17:03:26
【问题描述】:

我使用此脚本在多个 doc 文件中搜索和替换,但每次我使用此脚本时都会出现一个空行,有人知道我如何摆脱空行

function myFunction() {
var files = DriveApp.getFolderById("1ZMxrX5CKmB5RIiE9hY94h4WuMoRqkj").getFiles();
while (files.hasNext()) {
var file = files.next();
var body = DocumentApp.openById(file.getId());
body.replaceText( ".*(111).*", "");
Logger.log(file.getName())
}
Logger.log("Done")
}

我的文件看起来像这样

1232888
1114172
8587527
7588696
1112858
5875885

在我运行我的脚本后,它看起来像这样

1232888

8587527
7588696

5875885

但我想要这样

1232888
8587527
7588696
5875885

【问题讨论】:

    标签: regex google-apps-script google-docs


    【解决方案1】:

    试试这个:

    function myFunction() {
      let doc = DocumentApp.getActiveDocument();
      let body = doc.getBody();
      let n=body.getNumChildren();
      for(let i=0;i<n;i++) {
        let child = body.getChild(i);
        if(child.asText().findText(".*(111).*")) {
          child.removeFromParent();
          --n;
        }
      }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-11-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-10-17
      相关资源
      最近更新 更多