如何创建一个每小时运行一次的基于时间的附加触发器? [英] How do I create a timeBased Add-on trigger that runs every hour?

查看:54
本文介绍了如何创建一个每小时运行一次的基于时间的附加触发器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我以前的帖子的扩展此处,我只是觉得这更具体,应该是自己的帖子.

Extension of my previous post here, I just figured this was more specific and should be it's own post.

附加组件对触发器有限制.其中一个就是每小时只能运行一次.我不知道该怎么做.

Add-ons have restrictions on triggers. One such is that it can only run once an hour. I cannot figure out how to make that work.

运行以下脚本将产生错误:试图执行不允许的操作" (作为附件运行).因此,如果下面的方法不是一个小时一次的脚本的正确附加方法,那是什么,或者我是否发现了错误?

Running the below script will produce the error: "attempted to perform an action that is not allowed" when it is run as an add-on. So if the below is not the proper add-on method for a once an hour script, what is, or did I find a bug?

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

我尝试按照Spencer的建议添加授权检查,并在文档

I tried adding the authorization check as Spencer suggested, and outlined in the documentation here. It passes the authentication, but still produces the same error.

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
  }
}

推荐答案

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

The issue you are running against is the scope of authorization your Add-on is running in when the trigger gets created or ran. Installed Triggers are run in AuthMode.FULL. You need to test for the current level of authorization before you can run the trigger. You use the ScriptApp.getAutorizationInfo(authMode) to get the status of the authMode the add-on is running in.

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

Here is an example bit of code from the Apps Script documentation: 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.
  }

这篇关于如何创建一个每小时运行一次的基于时间的附加触发器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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