Google App Script 超时错误经常发生在具有许多编辑器的工作表中

问题描述 投票:0回答:1

我有一个Google Sheet,数据集很大,用于多个用户(小于10人)同时登录并编辑的场景,需要实时同步。但是,最近通过应用程序脚本使用自动化流程调用此电子表格时,我经常遇到超时错误。这些错误导致自动化无法顺利运行,即使对于 setValues、setFormula、clearcontent 等基本数据操作也是如此。

我已向 Google Workspace 客户支持寻求帮助,他们的回复表明超时错误可能与多个用户同时编辑文档的情况有关。在这种情况下,我该如何改善这种情况呢?

这是应用程序脚本代码的示例。我认为大多数错误发生在代码执行到“const main = SpreadsheetApp.openById(“xxxxxxxxxxxxxxxxxx”);”时

const ss = SpreadsheetApp.getActive();
SpreadsheetApp.flush();
 
  const originalSheet = ss.getSheetByName('aaa');
  const modifySheet = ss.getSheetByName('bbb');
  const main = SpreadsheetApp.openById("xxxxxxxxxxxxxxxxxx");
  const mainSheet = main.getSheetByName("ABC");
  const mainSheetLastRow = glr(mainSheet,"A1:C");
  mainSheet.getRange(mainSheetLastRow + 2,1,moveRowNum,76).setValues(modifySheet.getRange(3,1,moveRowNum,76).getValues()); 
  
  mainSheet.getRange(mainSheetLastRow + 2,47 ,moveRowNum, 1).setFormula("=VLOOKUP($L:$L,'kkk'!$F$2:$N$3500,9,false)"); 
  mainSheet.getRange(mainSheetLastRow + 2,65 ,moveRowNum, 1).setFormula("=VLOOKUP($L:$L,'kkk'!$F$2:$J$3500,5,false)"); 

  originalSheet.getRange(3,1,moveRowNum,76).clearContent();

我已联系 Google Workspace 客户支持人员,并建议我在此社区中寻求帮助!

google-apps-script timeout runtime-error
1个回答
0
投票

我猜您正在使用 onEdit 触发器运行。如果是这样,它最好是一个可安装的触发器,指向一个未命名为 onEdit 的处理函数,因为 onEdit 的简单触发器无法通过 id 打开文件。另外,您不应该在 google apps 脚本中使用诸如“A1:C”之类的不确定范围,因为您会在数组底部得到许多空值。您应该学会最大限度地利用事件对象,并停止浪费时间为已有的数据调用函数。最后,使用 SpreadsheetApp.flush 只会让事情变得更糟。 onEdit 本身就足够慢,无需对每个操作进行刷新。

© www.soinside.com 2019 - 2024. All rights reserved.