简体   繁体   中英

How do I select an empty line in Google Docs using App Script?

I'm trying to make an add on to make underscores and (preferably) new lines red. I would use var line = body.findText("\\nl"); or var line = body.findText("<br>"); , but those select that bit of text and if it worked, would select every new line, empty or not.

Here's the code snippet that I copy and paste for the main parts:

function redLine() {
  var body = DocumentApp.getActiveDocument().getBody();
  var line = body.findText("");
  while (line) {
    var elem = line.getElement();
    var start = line.getStartOffset();
    var end = line.getEndOffsetInclusive();
    elem.setForegroundColor(start, end, "#FF0000");
    line = body.findText("", line);
  }
}

Thanks!

I used a different answer and rewrote that section, and used this code.

  var body = DocumentApp.getActiveDocument().getBody();
  var paragraphs = body.getParagraphs();
  var i = 0;

  for (var i = 0; i < paragraphs.length; i++) {
       if (paragraphs[i].getText() === ""){
          paragraphs[i].setForegroundColor("#FF0000");
       }
  }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM