【发布时间】:2018-04-18 08:42:18
【问题描述】:
我有一个名为alertnotice(to, message, body) 的函数,它将在用户onClick() 事件上执行。该函数将执行sendEmail(to, message, body) 以根据变量triggerDay 中的计算触发器发送电子邮件,如下所示:
function alertnotice(to, subject, body, dueDate, notifyBeforeDay) {//start of this class
function sendEmail(to, subject, body){
try {
MailApp.sendEmail({
to: to,
subject: subject,
htmlBody: body
});
} catch(e) {
console.error(JSON.stringify(e));
}
}
//check if there is an existing trigger for this process
var existingTrigger = PropertiesService.getScriptProperties().getProperty("sendEmailTrigger");
//set the renewal notice day
var triggerDay = (dueDate - notifyBeforeDay) / (1000*60*60*24);
//if the trigger already exists, inform user about it
if(existingTrigger) {
return "Alert notice had been sent";
} else { // if the trigger does not exists, continue to set the trigger to send alert notice
//runs the script every day at 1am on the time zone specified
var newTrigger = ScriptApp.newTrigger('sendEmail')
.timeBased()
.atTime(triggerDay)
.create();
var triggerId = newTrigger.getUniqueId();
if(triggerId) {
PropertiesService.getScriptProperties().setProperty("autoExportTrigger", triggerId);
return "Alert notice send successfully!";
} else {
return "Failed to send alert notice. Try again please";
}
}
}//end of this class
例如,如果dueDate 是30/07/2018 和notifyBeforeDay = 30,则该函数应在到期日期前30 天发送电子邮件。我试图实现这一点,但不太确定我的算法是否有效。谁能给点建议?
【问题讨论】:
-
这个触发器是为单独的用户分开的还是只有一个触发器?
-
每个用户一个触发器..我的触发器正确吗?
标签: google-apps-script gmail-api google-app-maker