![](/img/trans.png)
[英]How to add a google script trigger that runs every hour near minute 15?
[英]How do I create a timeBased Add-on trigger that runs every hour?
我以前的帖子的扩展在这里 ,我只是想通这是更具体的,应该是它自己的岗位。
插件对触发器有限制。 其中一个就是每小时只能运行一次。 我不知道该怎么做。
运行以下脚本将生成错误:作为附件运行时, “试图执行不允许的操作” 。 因此,如果以下内容不是一个小时一次脚本的正确附加方法,那是什么,或者我是否发现了错误?
ScriptApp.newTrigger('updateDay').timeBased().everyHours(1).create();
我尝试按照Spencer的建议添加授权检查,并在此处的文档中进行了概述。 它通过身份验证,但仍会产生相同的错误。
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的状态。
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.
}
我认为您正在特定时间的特定日期使用触发器,因此在您的示例中,您需要指定日期(例如星期一),而atHour(1)将使触发器在每个星期一的1运行。
为了指定触发频率,您需要编写:ScriptApp.newTrigger('myFunction')。timeBased()。 everyHours(1) .create();
我得出的结论是,这是一个错误或.timebased()触发器不作为附件受支持(我认为是)。
请为该问题加注星标,以帮助使其再次正常运行。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.