问题
Suddenly encountered one issue where Google DOCS service fails after making some calls to copy elements. Funny thing is that it actually copies the first element, but when it goes for the second loop within the "for" it fails giving this error: Service unavailable: Documents
This script used to work fine for a few months as we have it published within our company. Suddenly last week it stopped working for all the users regardless of their browser etc...
It is a script linked to a Google Spreadsheet (where it gets the information to generate a Google Docs). But it also fails if I execute it, or debug it, from the Script console as well.
What I've tried after it started failing is to add a couple of lines to saveAndClose the document in case there was an issue with too many calls or buffering.
I've checked and the project does have access to Google Docs, Drive and Maps API.
The elements to be copied are correct, as I can do Logger.Log with them...
It just fails when it has to copy the actual element (appendParagraph, append ListItem, etc...)
I've enabled StackDriver logs and errors but it doesn't show any logs in the console, just "failed".
Any hint or directions will be very much appreciate it!
Thanks!
function copyDescription(ID,newDoc,newDocID){
var otherBody = DocumentApp.openById(ID).getBody();
newDoc.saveAndClose();
newDoc = DocumentApp.openById(newDocID);
var docbody = newDoc.getBody();
var totalElements = otherBody.getNumChildren();
//Run through template document and copies to the new one
for( var j = 0; j < totalElements; ++j ) {
var element = otherBody.getChild(j).copy();
var attributes = otherBody.getChild(j).getAttributes();
var type = element.getType();
if (type == DocumentApp.ElementType.PARAGRAPH) {
if (element.asParagraph().getNumChildren() != 0 && element.asParagraph().getChild(0).getType() == DocumentApp.ElementType.INLINE_IMAGE) {
var pictattr = element.asParagraph().getChild(0).asInlineImage().getAttributes();
var blob = element.asParagraph().getChild(0).asInlineImage().getBlob();
}
else {
docbody.appendParagraph(element);
}
}
else if( type == DocumentApp.ElementType.TABLE )
docbody.appendTable(element);
else if( type == DocumentApp.ElementType.LIST_ITEM )
docbody.appendListItem(element);
else
throw new Error("Unsupported element type: "+type);
newDoc.saveAndClose();
}
}
来源:https://stackoverflow.com/questions/58054839/service-unavailable-docs-when-copying-google-docs