简体   繁体   English

在 Google Apps 脚本中重试 try catch

[英]Retry try catch in Google Apps Script

When processing results of Google forms with Google Apps Script accessing the form by …当使用 Google Apps 脚本处理 Google forms 的结果时,通过...

    let formID = FormApp.getActiveForm().getId();

… sometimes fails with an exception like "Form data could not be retrieved." ... 有时会失败,出现“无法检索表单数据”之类的异常。 Manually started just a minute later it works properly.一分钟后手动启动,它工作正常。

To handle those errors the best way I want to catch the exception and retry the method one minute later.为了以最好的方式处理这些错误,我想捕获异常并在一分钟后重试该方法。 I came up with this:我想出了这个:

    function foo() {
      const maxTries = 3;
      let formID;
      let tries = 1;
      while(true) {
        try {
          formID = FormApp.getActiveForm().getId();
          break;
        } catch (e) {
          console.log("Retrieving form data failed (" + tries + ")");
          if (tries >= maxTries) {
            console.log("Retrieving form data not possible"); // and/or …
            throw(e);
            return;
          } else {
            tries++;
          };
        };
      };
      // Do things with form stuff
    };

How can I insert a 60 second pause between the tries?如何在两次尝试之间插入 60 秒的暂停? And I'm not sure anyway, if there isn't a better way to overcome those errors (at all or within Google Apps Script).而且我不确定是否有更好的方法来克服这些错误(完全或在 Google Apps 脚本中)。

In this case why don't you try using Utilities.sleep .在这种情况下,您为什么不尝试使用Utilities.sleep This method will pretty much puts the script to sleep for a set amount of time.这种方法几乎可以让脚本休眠一段设定的时间。 Please note the maximum amount is of 5 minutes as the maximum execution time for a script is of 6 minutes.请注意最大数量为 5 分钟,因为脚本的最长执行时间为 6 分钟。 You can check runtime limits in the documentation .您可以在文档中检查运行时限制。

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

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