简体   繁体   English

超过最大执行时间谷歌表格脚本解决方法

[英]Exceeded maximum execution time google sheets script work around

I have a script that runs a function for a set of data that is drawn from a master sheet.我有一个脚本运行 function 用于从主表中提取的一组数据。 Each time the function runs, it draws and processes a data set based on a date designated in a cell in a sheet (cell A3).每次 function 运行时,它都会根据工作表中单元格(单元格 A3)中指定的日期绘制和处理数据集。 The date in this cell is updated via a 'for' loop for a date range that I specified.此单元格中的日期通过我指定的日期范围的“for”循环更新。 the script can execute 5 complete runs (aka each run execute the function for a day worth of data) of these loops before I run into "Exceeded maximum execution time" some time during the 6th run.该脚本可以执行 5 次完整的运行(也就是每次运行都执行 function 以获得一天的数据),然后我在第 6 次运行期间的某个时间遇到“超过最大执行时间”。 All of my data are already saved to the spreadsheet in a continual updating fashion after every run so what I have to do after five cycles is to manually restart the run for another 5 days starting at the 6th day by modifying my script.每次运行后,我的所有数据都已经以持续更新的方式保存到电子表格中,所以在五个周期后我必须做的是通过修改我的脚本从第 6 天开始手动重新启动运行另外 5 天。 I am reading about time driven trigger where someone is pausing the script every 5 minutes to get around the 6 minutes execution time limit but it does not suit my needs because I would like a break after every 5 cycles, (not based on time).我正在阅读关于时间驱动的触发器,其中有人每 5 分钟暂停一次脚本以绕过 6 分钟的执行时间限制,但它不适合我的需要,因为我希望每 5 个周期后休息一下(不是基于时间)。 What I need is to write a script where after running 5 cycles, the script can break and then resume running again for another 5 cycles.我需要编写一个脚本,在运行 5 个周期后,脚本可以中断,然后再继续运行 5 个周期。 Here is what my code looks like:这是我的代码的样子:

 function runMultipleDates() { var ss = SpreadsheetApp.getActiveSpreadsheet(); var start = new Date("2018-11-05"); var end = new Date("2018-11-09"); var step = 1; for (var date1 = start; date1 <= end; date1.setDate(date1.getDate() + step)) { var date2 = new Date(date1.getTime()); date2.setDate(date2.getDate() + 1); ss.getSheetByName('Time Range').getRange("A3").setValue(date2); runEverything(); }; }

As you can see, right now I am adjusting the start and end date manually for 5 days, let it run and then restart the process again by adjusting my script start date to be 2018-11-09 and end date to be 2018-11-13 for the next run.如您所见,现在我正在手动调整开始和结束日期 5 天,让它运行,然后通过将我的脚本开始日期调整为 2018-11-09 并将结束日期调整为 2018-11 再次重新启动该过程-13 为下一次运行。 This function, on its own, is to able to run for far more than 5 days cycles without the execution time restriction.这个 function 本身可以运行超过 5 天的周期而不受执行时间的限制。 So the question is whether there is away to pause the script and resume running after 5 cycles of the above script.所以问题是在上述脚本的5个周期后是否可以暂停脚本并恢复运行。 Any help is greatly appreciated.任何帮助是极大的赞赏。

Thanks for the ideas sent my way.感谢您向我发送的想法。 I have found an answer for this problem with the following codes adapted slightly from benlcollins on gitHub.我已经找到了这个问题的答案,下面的代码稍微改编自gitHub上的 benlcollins。 As @Cooper suggested, I ran my function every 5.5 minutes which was just a little more than what it took for me to complete the main function runMultipleDates() (this function processes 5 days at a time on its own); As @Cooper suggested, I ran my function every 5.5 minutes which was just a little more than what it took for me to complete the main function runMultipleDates() (this function processes 5 days at a time on its own); I am repeating this 20 times which means it will cover 20x5=100 days which is good enough for me.我重复这 20 次,这意味着它将覆盖 20x5=100 天,这对我来说已经足够了。 I am posting this in case it helps someone.我发布这个以防它帮助某人。

 // ----------------------------------------------------------------------------- // add menu // ----------------------------------------------------------------------------- function onOpen() { var ui = SpreadsheetApp.getUi(); ui.createMenu("Auto Trigger").addItem("Run","runAuto").addToUi(); } function runAuto() { // resets the loop counter if it's not 0 refreshUserProps(); // create trigger to run program automatically createTrigger(); } function refreshUserProps() { var userProperties = PropertiesService.getUserProperties(); userProperties.setProperty('loopCounter', 0); } function createTrigger() { ScriptApp.newTrigger('runGlobal').timeBased().everyMinutes(5.5).create(); } // ----------------------------------------------------------------------------- // function to delete triggers // ----------------------------------------------------------------------------- function deleteTrigger() { // Loop over all triggers and delete them var allTriggers = ScriptApp.getProjectTriggers(); for (var i = 0; i < allTriggers.length; i++) { ScriptApp.deleteTrigger(allTriggers[i]); } } // ----------------------------------------------------------------------------- // function to run called by trigger once per each iteration of loop // ----------------------------------------------------------------------------- function runGlobal() { var ss = SpreadsheetApp.getActiveSpreadsheet(); var sheet = ss.getSheetByName('Data'); // get the current loop counter var userProperties = PropertiesService.getUserProperties(); var loopCounter = Number(userProperties.getProperty('loopCounter')); var limit = 20; // create trigger to run program automatically // if loop counter < limit number, run the repeatable action if (loopCounter < limit) { // do stuff runMultipleDates(); // increment the properties service counter for the loop loopCounter +=1; userProperties.setProperty('loopCounter', loopCounter); // see what the counter value is at the end of the loop Logger.log(loopCounter); } // if the loop counter is no longer smaller than the limit number else { Browser.msgBox("Run Completed"); // Log message to confirm loop is finished deleteTrigger(); } }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM