我写了一个应用程序脚本,它应该将文本墙分成不同的段落。
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;
}
}
}
不幸的是,它给了我一个丑陋的红色警告“Service unavailable:Docs”。在写这篇文章的过程中,我了解到这意味着“发生了一些可疑的事情,你必须弄清楚那是什么,你自己。”这可能是超时,复杂的正则表达式,无限循环(也会产生超时)等等。谷歌的问题跟踪系统有几个。
现在我试图避免所有复杂或非标准的事情,甚至确保在重复次数过多的情况下打破循环,但我仍然得到“服务不可用:文档”。可能导致这种情况的原因,我该如何解决?