添加大量表时如何解决错误
How to solve error when adding big number of tables
我有这个 Google 脚本,我在其中使用位于另一个文档中的模板 table 创建文档。
新文档中将包含许多小 table(如卡片)。
下面的代码在 100、200 table 秒内运行良好,并在不到 10 秒的时间内完成。但是失败超过 500 tables。
执行 window.
中没有错误消息
我尝试了 saveAndClose() 函数(已注释掉),但错误仍然存在,只是需要更长的时间才能 运行。
我运行不知道如何解决这个问题。
任何帮助或想法将不胜感激。
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
内使用它。也就是说,
替换
// 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());
以上代码已使用以下模板进行测试
脚本已完成且没有错误。生成的文档有 50 页。
就像另一个答案中提到的,最好将文档保存在循环外,需要时再调用。文档正文也是如此。
let currentDoc = DocumentApp.getActiveDocument();
let bodyDoc = currentDoc.getBody();
for (var i = 0; i < 500; i++){
table = bodyDoc.appendTable(reqTableItem.copy());
// if((i % 100) === 0) {
// currentDoc.saveAndClose();
// }
//
}
但是,既然你提到你收到了 Exception: Service Documents failed while accessing document with id
错误,你可能想看看问题跟踪器上的这个 issue 因为您遇到的问题可能是 Apps 脚本的错误。
如果是这种情况,我建议你给这个问题加注星标,最后添加一条评论说你受到了它的影响。
参考
我有这个 Google 脚本,我在其中使用位于另一个文档中的模板 table 创建文档。
新文档中将包含许多小 table(如卡片)。 下面的代码在 100、200 table 秒内运行良好,并在不到 10 秒的时间内完成。但是失败超过 500 tables。 执行 window.
中没有错误消息我尝试了 saveAndClose() 函数(已注释掉),但错误仍然存在,只是需要更长的时间才能 运行。
我运行不知道如何解决这个问题。 任何帮助或想法将不胜感激。
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
内使用它。也就是说,
替换
// 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());
以上代码已使用以下模板进行测试
脚本已完成且没有错误。生成的文档有 50 页。
就像另一个答案中提到的,最好将文档保存在循环外,需要时再调用。文档正文也是如此。
let currentDoc = DocumentApp.getActiveDocument();
let bodyDoc = currentDoc.getBody();
for (var i = 0; i < 500; i++){
table = bodyDoc.appendTable(reqTableItem.copy());
// if((i % 100) === 0) {
// currentDoc.saveAndClose();
// }
//
}
但是,既然你提到你收到了 Exception: Service Documents failed while accessing document with id
错误,你可能想看看问题跟踪器上的这个 issue 因为您遇到的问题可能是 Apps 脚本的错误。
如果是这种情况,我建议你给这个问题加注星标,最后添加一条评论说你受到了它的影响。