【问题标题】:create an email digest using google scripts使用谷歌脚本创建电子邮件摘要
【发布时间】:2021-04-07 01:49:05
【问题描述】:

我编写了一个脚本,该脚本会在满足特定条件时自动踢出一封电子邮件。这很好用,每次条件合适时都会发送一封电子邮件;虽然会产生很多电子邮件...

有几个代码块寻找不同的参数,比如这个:

//checking for 2 months from now
    if (
      expireDateMonth === twoMonthsMonth &&
      expireDateDay === twoMonthsDay &&
      expireDateYear === twoMonthsYear
    ) {
      var subject =
        'A license is expiring in 2 months: ' +
        row[x] + ' ' +
        expireDateFormat;
      MailApp.sendEmail('permissions@makethisworkplease.now', subject, message);
      Logger.log('2 months from now');
    }

他们每个人都会在触发时发送一封电子邮件。我想做的是制作任何弹出的触发器,一次在一封电子邮件摘要中发出。感谢您对此事的任何想法。由于工作原因,我无法使用外部服务来完成此任务。

【问题讨论】:

  • 什么触发你的脚本运行?如果它是一个简单的触发器,那么它需要用户的输入。它不会触发公式或其他脚本的操作。除了轮询之外没有其他解决方法
  • 您可以在 PropertiesService 或另一个文件中收集消息,然后使用基于时间的触发器每天一次转储它们。但我不能确定,因为你提供了minimal reproducible example
  • @Cooper 脚本读取一张表格,当它到达某个日期时,它会触发电子邮件通知。日期各不相同,我不知道如何让所有这些都在一封电子邮件中报告;特别是因为脚本以重复的时间间隔运行。我可以做些什么来澄清我的问题,我是个暴徒?
  • 如果是这种情况,您需要让脚本读取整个工作表并将电子邮件内容累积在一个变量中,然后在整个工作表完成后将该变量内容作为消息传递给 MailApp.sendEmail 函数阅读。
  • 谢谢。这正是我的想法。我会尝试这个建议以及 PropertiesService 解决方案。

标签: google-apps-script google-sheets


【解决方案1】:

假设您在为触发脚本设置的时间间隔内陷入僵局(假设它们是基于时间的),那么下面的想法对您来说应该是一个可行的选择。

将数据存储在PropertiesService(根据 Cooper 的评论)是解决问题的好方法。然后在一天中的最后一个时间间隔,您需要将所有这些数据整理到一封电子邮件中。

代码

function sendBulkEmail() {
  var scriptProperties = PropertiesService.getScriptProperties();
  var user = 'user@domain.com';
  // Condition and data here are just placeholders
  // Ran the script twice with different data
  var condition = true; 
  // First was John Doe, and next is John Reese
  var data1 = 'John Reese'; 
  var data2 = new Date();

  if (condition) {
    var message = data1 + ' ' + data2 + '\n';
    // if TWOMONTHS_MESSAGE already contains a data, it will be appended with the 
    // new data from the recent run
    message = scriptProperties.getProperty('TWOMONTHS_MESSAGE') + message;
    scriptProperties.setProperty('TWOMONTHS_MESSAGE', message); 
  }
  
  // If TWOMONTHS_MESSAGE has data and current run is triggered during 1 am 
  // Assuming your trigger is per n-hours and one trigger is timed at 1 am daily
  // Used 1am since it is my current time, but 11pm should be better for live data
  if(scriptProperties.getProperty('TWOMONTHS_MESSAGE') && new Date().getHours() == 1) {
    subject = 'A license is expiring in 2 months: ';
    // get all accumulated message for a certain condition and then dump in one email
    // if you have other conditions, you can append them all and have conditions 
    // to modify subject and message based on data availability
    message = scriptProperties.getProperty('TWOMONTHS_MESSAGE');
    MailApp.sendEmail(user, subject, message);

    // reset TWOMONTHS_MESSAGE value to blank after sending the data so it
    // won't be included in the email to be sent the next day.   
    scriptProperties.setProperty('TWOMONTHS_MESSAGE', ''); 
  }
}

样本输出:

参考:

【讨论】:

  • 嗨@Pers,感谢您检查我的回答。如果我们回答了您的问题,请点击接受按钮。通过这样做,社区中可能与您有同样担忧的其他人将知道他们的问题可以得到解决。如果您无法使用接受按钮,请随时告诉我。 stackoverflow.com/help/accepted-answer
  • 你好,没有显示接受按钮。但答案是可以接受的
  • 嗨@Pers,这将是答案左侧的复选按钮。
猜你喜欢
  • 2013-04-04
  • 2017-07-31
  • 1970-01-01
  • 1970-01-01
  • 2016-09-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-04-22
相关资源
最近更新 更多