简体   繁体   English

如何创建一个每小时运行一次的基于时间的附加触发器?

[英]How do I create a timeBased Add-on trigger that runs every hour?

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();

I tried adding the authorization check as Spencer suggested, and outlined in the documentation here . 我尝试按照Spencer的建议添加授权检查,并在此处的文档中进行了概述。 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
  }
}

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. 已安装的触发器在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. 您可以使用ScriptApp.getAutorizationInfo(authMode)来获取加载项正在运行的authMode的状态。

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

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 这是来自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.
  }

I think you are using the trigger for specific days at specific hour, so in your example you would need to specify the day (eg Mondays) and atHour(1) would make the trigger to run every monday at 1. 我认为您正在特定时间的特定日期使用触发器,因此在您的示例中,您需要指定日期(例如星期一),而atHour(1)将使触发器在每个星期一的1运行。

For specifying how often it should be triggered you would need to write: ScriptApp.newTrigger('myFunction').timeBased(). 为了指定触发频率,您需要编写:ScriptApp.newTrigger('myFunction')。timeBased()。 everyHours(1) .create(); everyHours(1) .create();

I have come to the conclusion that this is a bug or .timebased() triggers are not supported as an add-on, (which I thought they were). 我得出的结论是,这是一个错误或.timebased()触发器不作为附件受支持(我认为是)。

Please star this issue to help get this working again. 请为该问题加注星标,以帮助使其再次正常运行。

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

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

相关问题 如何添加在第15分钟附近每小时运行一次的Google脚本触发器? - How to add a google script trigger that runs every hour near minute 15? 程序化创建基于时间的电子表格触发器 - Programmatic create timebased trigger for spreadsheet 如何在gmail插件中添加按钮以触发我需要的功能? - How to add a button in a gmail add-on to trigger the function that I need? 例外:附加触发器的重复间隔必须至少为一小时 - Exception: The recurrence interval for an Add-on trigger must be at least one hour 附加触发器未运行,如何解决 - Add-on Trigger not running, how can I troubleshoot 在 AppScript 中,对于 Workspace Published Add-On Sheet Extensions,我如何切换到 Head 版本并停止部署每个错误修复? - In AppScript, for Workspace Published Add-On Sheet Extensions, how do I switch to Head version and stop Deploying every bug fix? 如何编写条件Gmail附加触发器? - How to write a conditional Gmail add-on trigger? 如何为谷歌日历创建插件? 是否可以? - How can I create add-on for google calendar? Is it possible? 我可以修改 Google Analytics 插件以在插件运行后运行 function 吗? - Can I modify an Google Analytics add-on to run a function after the add-on runs? 如何计算附加组件用户的每个自定义函数使用情况 - How can I count every custom function usage for a user of an add-on
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM