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

Time:11-12

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!

CodePudding user response:

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");
       }
  }
  • Related