如何创建每小时运行一次的timeBased Add-on触发器?

时间:2015-01-26 16:59:38

标签: google-apps-script

我以前的帖子here的扩展,我只是认为这更具体,应该是它自己的帖子。

加载项对触发器有限制。其中之一是它每小时只能运行一次。我无法弄清楚如何做到这一点。

运行以下脚本将产生错误:“尝试执行不允许的操作”作为加载项运行时。因此,如果以下内容不是每小时一次脚本的正确附加方法,那么我发现了什么,或者我找到了什么错误?

ScriptApp.newTrigger('updateDay').timeBased().everyHours(1).create();

我尝试按Spencer的建议添加授权检查,并在文档here中进行了概述。它通过了身份验证,但仍会产生相同的错误。

function installTrigger(e) {
  var addonTitle = 'Lab Scheduler';
  var props = PropertiesService.getDocumentProperties();
  var authInfo = ScriptApp.getAuthorizationInfo(ScriptApp.AuthMode.FULL);
  if (authInfo.getAuthorizationStatus() ==
      ScriptApp.AuthorizationStatus.REQUIRED) {
    var lastAuthEmailDate = props.getProperty('lastAuthEmailDate');
    var today = new Date().toDateString();
    if (lastAuthEmailDate != today) {
      if (MailApp.getRemainingDailyQuota() > 0) {
        var html = HtmlService.createTemplateFromFile('AuthorizationEmail');
        html.url = authInfo.getAuthorizationUrl();
        html.addonTitle = addonTitle;
        var message = html.evaluate();
        MailApp.sendEmail(Session.getEffectiveUser().getEmail(),
                          'Authorization Required',
                          message.getContent(), {
                            name: addonTitle,
                            htmlBody: message.getContent()
                          }
                         );
      }
      props.setProperty('lastAuthEmailDate', today);
    }
  } else {
    // Authorization has been granted, so continue to respond to the trigger.
    try{ 
      var as = SpreadsheetApp.getActiveSpreadsheet();
      var userTriggers = ScriptApp.getUserTriggers(as);
      var userTriggerL = userTriggers.length;
      if (userTriggers.length == 0){
        ScriptApp.newTrigger('updateDay').timeBased().everyHours(1).create();
      } 
    } catch(err){  
      catchToString_(err);  
    } // End try catch
  }
}

3 个答案:

答案 0 :(得分:1)

您正在运行的问题是创建或运行触发器时加载项的授权范围。已安装的触发器在AuthMode.FULL中运行。您需要先测试当前的授权级别,然后才能运行触发器。您可以使用ScriptApp.getAutorizationInfo(authMode)来获取正在运行加载项的authMode的状态。

https://developers.google.com/apps-script/reference/script/script-app#getAuthorizationInfo(AuthMode)

以下是Apps脚本文档中的一些代码示例: https://github.com/googlesamples/apps-script-form-notifications-addon/blob/master/Code.gs

var authInfo = ScriptApp.getAuthorizationInfo(ScriptApp.AuthMode.FULL);

  // Check if the actions of the trigger require authorizations that have not
  // been supplied yet -- if so, warn the active user via email (if possible).
  // This check is required when using triggers with add-ons to maintain
  // functional triggers.
  if (authInfo.getAuthorizationStatus() ==
  ScriptApp.AuthorizationStatus.REQUIRED) {
    // Re-authorization is required. In this case, the user needs to be alerted
    // that they need to reauthorize; the normal trigger action is not
    // conducted, since it authorization needs to be provided first. Send at
    // most one 'Authorization Required' email a day, to avoid spamming users
    // of the add-on.
sendReauthorizationRequest();
  } else {
    // All required authorizations has been granted, so continue to respond to
    // the trigger event.
  }

答案 1 :(得分:0)

我认为您在特定时间使用触发器特定日期,所以在您的示例中,您需要指定日期(例如星期一)和atHour(1)将触发器在每个星期一运行1。

要指定触发的频率,您需要编写: ScriptApp.newTrigger(' myFunction的&#39)。基于时间的()的 everyHours(1) .create();

答案 2 :(得分:0)

我得出的结论是,这是一个bug或.timebased()触发器不支持作为附件,(我认为它们是这样)。

请为此问题加注星标以帮助其重新开始工作。

https://code.google.com/p/google-apps-script-issues/issues/detail?id=4524&q=.timeBased()%20add-on%20trigger&colspec=Stars%20Opened%20ID%20Type%20Status%20Summary%20Component%20Owner