Google脚本:如何调用一个函数以在另一个函数完成后运行 [英] Google Scripts: How to call a function to run after another function is completed

查看:119
本文介绍了Google脚本:如何调用一个函数以在另一个函数完成后运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Google脚本,其中包含4个不同的功能,这些功能需要一个接一个地运行,但是一个功能可以在上一个功能完成/完成后立即运行.

I have a google script with 4 different functions that need to run one after another, but a function can run just after the previous one has finished/completed.

每个功能花费的时间有所不同,但平均每个功能大约需要15-20分钟.

The time that takes each function depends, but on average each function takes about 15-20 minutes.

每个函数完成了一个包含大量数据的电子表格,因此我想运行第一个函数,等到第一个函数结束,再运行第二个函数,等等.在触发器中每次(例如)加载信息4小时.

Each function completes an spreadsheet with a lot of data, so I want to run the first function, wait until the first one ends, run the second one, etc. inside a trigger to load the information every (for example) 4 hours.

谢谢!

推荐答案

在一般情况下,要在函数B之后运行函数A,一个人编写了一个控制器例程,该例程首先调用一个例程,然后调用另一个例程:

In the general case, to run function A after function B, one writes a controller routine that first calls one, then calls the other:

function a() { /* stuff */ }
function b() { /* other stuff */ }
function doStuff() {
  a();
  b();
}

对于您的情况,如果您有一个执行时间限制要处理,否则将终止它们执行您的一个功能并阻止其余功能的运行,您需要编写一个调度例程,该例程使用Apps脚本 ScriptApp.您可以通过多种方式来执行此操作,从安排第一个具有设置的间隔触发器的时间开始,并使用每个功能告诉Google 何时运行下一个功能,而不是运行 >下一个功能,或使用脚本属性指示要运行的下一个功能,以手动运行一个功能并让最后一个功能设置第一个功能的调用.

For your case, where you have an execution time limit to deal with that would otherwise kill they execution of one of your functions and prevent the rest from running, you need to write a scheduling routine that makes use of Apps Scripts ScriptApp class. You have a number of ways to do this, from scheduling the first with a set interval trigger and using each function to tell Google when to run the next function, rather than to run the next function, or using a script property to indicate the next function which be run, to manually running one and having the last one set up a call for the first.

链接示例:

function a() {
  ...
  // Google will run b() within +/-15 minutes of the indicated time
  // Schedule it for 16 minutes from now, so we know a() has
  // completed and shut down before b runs.
  var next = ScriptApp.newTrigger("b").timeBased();
  next.after(16 * 60 * 1000).create();
}
function b(e) {
  /* Add code to delete the one-time trigger used to call this function
  (The trigger uid is in the event object e) */
  ...

属性方法:

function doStuff() {
  var props = PropertiesService().getScriptProperties();
  var next;
  switch (props.getProperty("runNext")) {
    case "a":
      a();
      next = "b";
      break;
    ...
  }
  props.setProperty("runNext", next);
}
function a() {...}
...

如果您实现了一个将函数链接在一起的解决方案(相对于使用属性指示要运行的属性的解决方案),则需要确保删除以前的触发器,以防止触发器数量不断增长.

If you implement a solution that chains functions (vs. one that uses a property to indicate which to run), you will want to make sure you delete previous triggers to prevent the trigger count from growing endlessly.

这篇关于Google脚本:如何调用一个函数以在另一个函数完成后运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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