Google Scripts如何实现函数按顺序依次执行(前序完成后启动)
按顺序执行长耗时Google Script函数并设置定时触发
首先得明确:你的每个函数要跑15-20分钟,四个加起来远超Google Apps Script的单执行时间限制(消费者版最多6分钟,商业/教育版最多30分钟),所以没法直接在一个函数里依次调用。得用状态跟踪+时间驱动触发器的方式,让每个函数执行完自动触发下一个,同时设置根触发器每4小时启动整个流程。
步骤1:添加状态跟踪机制
用Script Properties存储当前执行阶段,确保每个函数只在正确的时机触发:
// 获取当前执行阶段 function getCurrentStage() { const props = PropertiesService.getScriptProperties(); return props.getProperty("executionStage") || "idle"; } // 设置当前执行阶段 function setCurrentStage(stage) { const props = PropertiesService.getScriptProperties(); props.setProperty("executionStage", stage); } // 重置执行状态(流程结束或出错时调用) function resetExecutionStage() { setCurrentStage("idle"); }
步骤2:改造每个函数,添加触发逻辑
每个函数执行完后,自动触发下一个;同时加校验防止误触发,出错时重置状态避免流程卡住:
function function1() { try { // 这里放你的函数1业务逻辑(处理电子表格数据) console.log("开始执行函数1"); // ... 你的代码 ... console.log("函数1执行完成"); // 更新状态并触发函数2 setCurrentStage("stage2"); ScriptApp.newTrigger("function2") .timeBased() .after(1000) // 延迟1秒确保状态更新完成 .create(); } catch (e) { console.error("函数1执行失败:", e); resetExecutionStage(); throw e; } } function function2() { // 校验是否处于正确阶段,防止误触发 if (getCurrentStage() !== "stage2") { console.log("函数2触发时机不对,跳过执行"); return; } try { // 你的函数2业务逻辑 console.log("开始执行函数2"); // ... 你的代码 ... console.log("函数2执行完成"); // 更新状态并触发函数3 setCurrentStage("stage3"); ScriptApp.newTrigger("function3") .timeBased() .after(1000) .create(); } catch (e) { console.error("函数2执行失败:", e); resetExecutionStage(); throw e; } finally { // 删除当前触发器,避免重复触发 deleteTriggersForFunction("function2"); } } function function3() { if (getCurrentStage() !== "stage3") { console.log("函数3触发时机不对,跳过执行"); return; } try { // 你的函数3业务逻辑 console.log("开始执行函数3"); // ... 你的代码 ... console.log("函数3执行完成"); // 更新状态并触发函数4 setCurrentStage("stage4"); ScriptApp.newTrigger("function4") .timeBased() .after(1000) .create(); } catch (e) { console.error("函数3执行失败:", e); resetExecutionStage(); throw e; } finally { deleteTriggersForFunction("function3"); } } function function4() { if (getCurrentStage() !== "stage4") { console.log("函数4触发时机不对,跳过执行"); return; } try { // 你的函数4业务逻辑 console.log("开始执行函数4"); // ... 你的代码 ... console.log("函数4执行完成"); } catch (e) { console.error("函数4执行失败:", e); throw e; } finally { // 流程全部完成,重置状态 resetExecutionStage(); deleteTriggersForFunction("function4"); } } // 辅助函数:删除指定函数的所有触发器 function deleteTriggersForFunction(functionName) { const triggers = ScriptApp.getProjectTriggers(); for (const trigger of triggers) { if (trigger.getHandlerFunction() === functionName) { ScriptApp.deleteTrigger(trigger); } } }
步骤3:创建启动流程的主函数
这个函数负责初始化状态,只有当没有正在执行的流程时才启动:
function startExecutionFlow() { const currentStage = getCurrentStage(); if (currentStage !== "idle") { console.log("已有流程在执行,跳过本次启动"); return; } // 设置初始状态并触发函数1 setCurrentStage("stage1"); ScriptApp.newTrigger("function1") .timeBased() .after(1000) .create(); }
步骤4:设置每4小时的定时触发器
- 打开Google Script编辑器,点击左侧的「触发器」图标(时钟形状)
- 点击「添加触发器」,按以下配置设置:
- 选择要运行的函数:
startExecutionFlow - 事件源:「时间驱动」
- 触发器类型:「小时计时器」
- 间隔:「每4小时」
- 选择要运行的函数:
- 点击「保存」,按提示完成权限授权
关键注意事项
- 执行时间限制:如果你是消费者版账户,单个函数最多跑6分钟,肯定满足不了你的需求——这种情况下得把每个函数拆成小批次处理,用触发器分批执行;商业/教育版最多30分钟,刚好覆盖你的单函数时长。
- 错误告警:可以在catch块里加邮件通知(比如
MailApp.sendEmail()),方便及时知道流程出错。 - 手动干预:如果需要终止正在执行的流程,记得调用
resetExecutionStage()并手动删除相关触发器。
内容的提问来源于stack exchange,提问作者Patricia Trejo
相关产品推荐
相关产品推荐

