添加大量表时如何解决错误 [英] How to solve error when adding big number of tables

查看:61
本文介绍了添加大量表时如何解决错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个Google脚本,我在其中使用驻留在另一个文档中的模板表来创建文档.

I have this Google Script where I am creating a document using a template table that lives in another document.

新文档中将包含许多小表(如卡片). 该波纹管适用于100个,200个表,并且在不到10秒的时间内完成.但是它不能用于500多个表. 执行"窗口中没有错误消息.

The new document will have a number of small tables (like cards) in it. The code bellow works fine for 100, 200 tables and it finishes in less than 10 seconds. But it fails for more than 500 tables. There is no error message in the Executions window.

我已经尝试过saveAndClose()函数(已注释掉),但是错误仍然存​​在,并且运行时间更长.

I have tried the saveAndClose() function (commented out) but the error continues and it just takes longer to run.

我没办法解决这个问题. 任何帮助或想法将不胜感激.

I ran out of ideas how to fix that. Any help or ideas will be appreciated.


function insertSpecification_withSection(){
  startTime = new Date()
  console.log("Starting Function... "); 
  
  // Retuns a Table Template Copied from another Document
  reqTableItem = RequirementTemplate_Copy();
  
  
  // Creates X number of separated tables from the template
  for (var i = 0; i < 500; i++){
    table = DocumentApp.getActiveDocument().getBody().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
}

推荐答案

不是使用长方法链将表附加到for循环内,而是在for之前声明文档主体的变量并使用它在for中.换句话说,

Instead of using a long method chaining to append the tables inside the for loop, declare a variable for the document body before the for and use it inside the for. In other words,

替换

 // Creates X number of separated tables from the template
  for (var i = 0; i < 500; i++){
    table = DocumentApp.getActiveDocument().getBody().appendTable(reqTableItem.copy());

作者

 var body = DocumentApp.getActiveDocument().getBody();
 // Creates X number of separated tables from the template
  for (var i = 0; i < 500; i++){
    table = body.appendTable(reqTableItem.copy());

以上代码已通过以下模板进行了测试

The above code was tested with the following template

脚本完成无错误.生成的文档有50页.

The script finished without errors. The resulting document has 50 pages.

这篇关于添加大量表时如何解决错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆