Search code examples
google-apps-scriptgoogle-docs

How to fix "Service Documents failed while accessing document" while inserting a lot of data?


This is a follow-up question derivated from How to solve error when adding big number of tables

With the code below, I get the following message when, for 500 tables. BUt it works fine for 200 for example. Exception: Service Documents failed while accessing document with id

The error happens on line 22, inside de if body = DocumentApp.getActiveDocument().getBody();

You also have the table template id to try, but here is an image

Image Table Template


function RequirementTemplate_Copy() {
  var templatedoc = DocumentApp.openById("1oJt02MfOIQPFptdWCwDpj5j-zFdO_Wrq-I48mUq9I-w");

  return templatedoc.getBody().getChild(1).copy()
}


function insertSpecification_withSection(){
 
  // Retuns a Table Template Copied from another Document
  reqTableItem = RequirementTemplate_Copy();
  
  var body = DocumentApp.getActiveDocument().getBody();
  // Creates X number of separated tables from the template
  for (var i = 1; i < 501; i++){
    table = reqTableItem.copy().replaceText("#Title#",String(i))
    body.appendTable(table);
    
    if((i % 100) === 0) {
      DocumentApp.getActiveDocument().saveAndClose();
      body = DocumentApp.getActiveDocument().getBody()
    } 
  }
}

Solution

  • It looks that the error message isn't related to the number of tables to be inserted because it occurs before adding the tables.

    Just wait a bit an try again. If the problem persist try your code using a different account if the code runs on the second account it's very possible that you first account exceeded a limit... there are some limits to prevent abuse that aren't published and that might change without any announcement.


    Using the fix suggested for the code from my answer to the previous question and changing the number for iteration limit to 1000 and 2000 works fine

    The following screenshot shows the result for 1000

    Here is the code used for the tests

     function insertSpecification_withSection(){
      startTime = new Date()
      console.log("Starting Function... "); 
      
      // Retuns a Table Template Copied from another Document
      reqTableItem = RequirementTemplate_Copy();
      
      var body = DocumentApp.getActiveDocument().getBody();
      // Creates X number of separated tables from the template
      for (var i = 0; i < 2000; i++){
        table = body.appendTable(reqTableItem.copy());
    
        //    if((i % 100) === 0) {
        //      DocumentApp.getActiveDocument().saveAndClose();
        //    }
        //    
        
      }
      endTime = new Date();
      timeDiff = endTime - startTime;
      console.log("Ending Function..."+ timeDiff + " ms"); 
      
    }
    
    function RequirementTemplate_Copy() {
    
      //---------------------------------------------------------------------------------------------------------------------------------------------------
      var ReqTableID = PropertiesService.getDocumentProperties().getProperty('ReqTableID');
      try{
        var templatedoc = DocumentApp.openById(ReqTableID);
      } catch (error) {
        DocumentApp.getUi().alert("Could not find the document. Confirm it was not deleted and that anyone have read access with the link.");
        //Logger.log("Document not accessible", ReqTableID)
      } 
      var reqTableItem = templatedoc.getChild(1).copy();
      //---------------------------------------------------------------------------------------------------------------------------------------------------
      return reqTableItem
    }
    
    function setReqTableID(){
      PropertiesService.getDocumentProperties().setProperty('ReqTableID', '1NS9nOb3qEBrqkcAQ3H83OhTJ4fxeySOQx7yM4vKSFu0')
    }