Google AppScript dies with “Service Unavailable: Docs”

自作多情 提交于 2019-12-11 04:39:29

问题


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?

来源:https://stackoverflow.com/questions/44880981/google-appscript-dies-with-service-unavailable-docs

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!