简体   繁体   English

如何限制GAS中的可安装触发器?

[英]How to throttle installable trigger in GAS?

We use this code to set up notify() as a spreadsheet onEdit trigger function: 我们使用以下代码将notify()设置为电子表格onEdit触发函数:

var ss = SpreadsheetApp.getActive();
ScriptApp.newTrigger("notify").forSpreadsheet(ss).onEdit().create();

The above trigger notify() will notify the user by mail when a spreadsheet gets updated, but the problem is that it sends an email to the user for each and every edit. 上面的触发器notify()将在电子表格更新时通过邮件通知用户,但是问题是,每次编辑时,它都会向用户发送一封电子邮件。 How do I overcome that, to send a single notification even if there are multiple changes? 我如何克服这一点,即使有多个更改也要发送单个通知?

You can accomplish your goal by remembering that you've notified a user, and suppressing further notifications (for a period of time). 您可以通过记住已通知用户并禁止进一步的通知(一段时间)来实现您的目标。 For example, this function uses the Properties Service to track notifications, and send just once per day: 例如,此功能使用属性服务来跟踪通知,并且每天仅发送一次:

function notify(e) {
  // Get the date we last notified user
  var properties = PropertiesService.getUserProperties();
  var lastNotifyDate = properties.getProperty("lastNotified");

  // Get today's date
  var today = new Date().toDateString();

  // Should we send a notification to user?
  if (!lastNotifyDate || lastNotifyDate !== today) {

    // --- Place notification code here --- //

    // Notify user just once per day; remember we've sent a notification already
    properties.setProperty("lastNotified",today);
  }
}

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM