【问题标题】:Trigger on calculated date on Google AppMaker在 Google AppMaker 上的计算日期触发
【发布时间】: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/2018notifyBeforeDay = 30,则该函数应在到期日期前30 天发送电子邮件。我试图实现这一点,但不太确定我的算法是否有效。谁能给点建议?

【问题讨论】:

  • 这个触发器是为单独的用户分开的还是只有一个触发器?
  • 每个用户一个触发器..我的触发器正确吗?

标签: google-apps-script gmail-api google-app-maker


【解决方案1】:

这个实现对我来说看起来很脆弱。我宁愿使用单个触发器来避免任何可能的重复电子邮件并确保至少有一个。像这样的:

// I am server script, trigger me on schedule (for instance nightly)
function sendAlerts() {
  var query = app.models.Customers.newQuery();

  // query only for non-notified customers with upcoming due date
  query.filters.NorificationAttempts._lessThan = ATTEMPTS_THRESHOLD;
  query.filters.Notified._equals = false;
  query.filters.DueDate._lessThan = <dueDate>;

  var customers = query.run();

  customers.forEach(function(customer) {
    var success = sendNotification(customer);

    if (success) {
      customer.Notified = true;
    } else {
      customer.NorificationAttempts++;
    }

    // performance trade off in favor of consistency
    app.saveRecords([customer]);
  });
}

此类脚本的触发器可以由应用的管理员安装,您可以在People Skills模板中找到类似的实现。

【讨论】:

  • 如果我的情况是 sendEmail() 函数对吗?
  • 有点是的......它只是一个你需要提供给触发器的函数,在你的原始代码 sn-p 中你将传递给触发器 sendEmail 函数。
  • 要填什么?
  • 这样的:var today = new Date(); var expiresOn = new Date(); tomorrow.setDate(today.getDate() + notificationThreshold); query.filters.DueDate._lessThan = expiresOn;
  • 试过了,但得到了这个错误:InternalError: Cannot find method at(number).在 alertnotice (SendEmailTrigger:97) 在 SendEmailTrigger:125 在 sendAlerts (SendEmailTrigger:124) 在 onProjectCreate_ (Datasources:64) 在 models.Project.onBeforeCreateEvent:1
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-24
  • 1970-01-01
相关资源
最近更新 更多