Google AppScript dies with “Service Unavailable: D

2019-07-29 06:33发布

I wrote an app script that should break down a wall of text into separate paragraphs.

function onOpen() {
  DocumentApp.getUi()
  .createMenu('Formatting tool')
  .addItem('Make Paragraphs', 'breakIntoParagraphs')
  .addToUi();
}

function breakIntoParagraphs() {
  var body = DocumentApp.getActiveDocument().getBody();
  var counter = 0;
  body.replaceText("\\v\\v+", "°"); // the ° is more convenient to handle

  var rangeElement = body.findText("°");
  while (rangeElement != null) {
    var start = rangeElement.getStartOffset();
    var paragraph = rangeElement.getElement().getParent();
    var childIndex = body.getChildIndex(paragraph);
    var endRangeElement = body.findText("°", rangeElement);

    if (endRangeElement != null) {
      var end =  endRangeElement.getStartOffset();
      var endParagraph = endRangeElement.getElement().getParent();
      var endChildIndex = body.getChildIndex(endParagraph);

      if ( childIndex !=  endChildIndex) {
        Logger.log("this spans paragraphs!"); // deal with this case later
      }

      Logger.log(paragraph.asText());
      var text = body.editAsText().deleteText(start, end - 1 ); // -1, so the concluding ° remains
      Logger.log("deleted text: \"" + text + "\"");
      var newParagraph = body.insertParagraph(childIndex, text);
      newParagraph.editAsText.replaceText("°", ""); // remove markers
    }
    rangeElement = body.findText("°", rangeElement);
    counter++;
    if (counter > 2) {
      break; 
    }
  }  
}

Unfortunately, it gives me an ugly red warning "Service unavailable: Docs". In the process of writing this, I learned that this means as much as "something fishy happened, and you have to figure out what that is, yourself." That can be timeouts, complex regular expressions, infinite loops (which give timeouts, too) etc. Google's issue tracking system has several of those.

Now I tried to avoid every complex or non-standard thing, and even made sure to break the loop in case of too many repetitions, but I still get the "Service unavailable: Docs". What could be causing this, and how can I fix it?

0条回答
登录 后发表回答