ben bo
ben bo

Reputation: 1

Google app script search and replace delete empty line in several documents

I use this script to search and replace in multiple doc files but every time i use this there is a blank line does anyone know how I can get rid of the blank line

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

my dokomente look like this

1232888
1114172
8587527
7588696
1112858
5875885

after i run my script it looks like this

1232888

8587527
7588696

5875885

but I would like to have it like this

1232888
8587527
7588696
5875885

Upvotes: 0

Views: 183

Answers (1)

Cooper
Cooper

Reputation: 64032

Try this:

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;
    }
  }
}

Upvotes: 1

Related Questions