如何在插入大量数据时修复 "Service Documents failed while accessing document"?

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

这是从

派生的后续问题

使用下面的代码,我在 500 table 秒时收到以下消息。但是它适用于 200 例如。 Exception: Service Documents failed while accessing document with id

错误发生在第 22 行,内部 de if body = DocumentApp.getActiveDocument().getBody();

您也有 table 模板 ID 可以尝试,但这里有一张图片

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()
    } 
  }
}

看起来错误消息与要插入的表的数量无关,因为它发生在添加表之前。

稍等一下再试。如果问题仍然存在,请使用不同的帐户尝试您的代码,如果代码在第二个帐户上运行,则您的第一个帐户很可能超过了限制......有一些限制可以防止未发布的滥用,并且可能会在没有任何限制的情况下发生变化公告.


使用针对 中的代码建议的修复并将迭代限制的数量更改为 1000 和 2000 可以正常工作

以下屏幕截图显示了 1000 的结果

这是用于测试的代码

 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')
}