google-apps-scriptgoogle-docsgoogle-docs-apiinternal-server-error

Internal Error while Batch Updating in Google Docs


I want to replace some text in docs using docsAPI (namedRange & batch update) and my Apps Script function doing well.
But irregularly raise internal error even if i don't change any of my code.
I couldn't find any information about the error message. Does anyone know about this?

error message is like..

GoogleJsonResponseException: API call to docs.documents.batchUpdate failed with error: Internal error encountered.

batch updating code is like..

function replace_named_range(doc_id = '1z8Mp2Twt_DgDe8RqPIRVSSc_VACDHdcQL9-iME75qbg', range_name='{{Replacement}}',replace_text='sample text'){
  var request = {
    "text": replace_text.toString(),
    "namedRangeName": range_name
  };
  var request_array =[];
  request_array.push({"replaceNamedRangeContent": request});
  Docs.Documents.batchUpdate({"requests": request_array},doc_id);
  Logger.log("Replace Request \ndoc_id : %s\nrange_name : %s\nreplace_text : %s",doc_id, range_name, replace_text)
}

Solution

  • Since the Docs.Documents.batchUpdate() call sometimes works fine and sometimes errors out, you should use exponential backoff to retry it a couple of times before throwing an error, like this:

    function replaceNamedRangeWithBackoff(doc_id, range_name, replace_text) {
      call(_ => replace_named_range(doc_id, range_name, replace_text));
    }