Gmail 自动回复脚本 - 如何设置条件时间并停止多次回复?

Gmail auto reply script - how to set conditional time and stop multiple replies?

我已经修改了一个 Gmail 自动回复脚本来向在我的非工作日给我发消息的人发送电子邮件。我希望脚本从 16:30 开始在星期五发送回复,但我不确定我的代码是否正确。

我的脚本在上周五测试时似乎没有发送任何回复,但在周六、周日和周一工作正常。

function autoReply() {
var interval = 5;
  var date = new Date();
  var day = date.getDay();
  var hour = date.getHours();
  var minute = date.getMinutes();
  if ([1,6,0].indexOf(day) > -1 || (day == 1 && hour < 8) || (day == 5 && hour == 16 && minute >= 30)) {
    var timeFrom = Math.floor(date.valueOf()/1000) - 60 * interval;
    var threads = GmailApp.search('is:inbox after:' + timeFrom);
    for (var i = 0; i < threads.length; i++) {
      if (threads[i].isUnread()){
        threads[i].reply("Hi," + "\n\n Thanks for your email. My working days are Tuesday to Friday, so I won't pick up your message until Tuesday morning.");
      threads[i].markRead();
      threads[i].star();
      }
    }
  }
}

此外,是否有任何方法可以调整我的脚本,使其不会将自动回复邮件发送给已经收到邮件的发件人(与 Gmail 的标准自动回复功能一样)。我已使用 'conditional replies' 等术语在 Stack Overflow 中搜索此解决方案,但无济于事。 (感谢我可能完全使用了错误的术语!)

如有任何帮助,我们将不胜感激!

您需要根据条件 (day == 5 && hour >= 17)

补充您的 if 语句

要验证电子邮件是否已发送给某些发件人,您可以使用 PropertiesService。这允许您将发件人保存在缓存中,并比较每封新电子邮件的发件人是否已包含在属性中:

function autoReply() {
var interval = 5;
  var date = new Date();
  var day = date.getDay();
  var hour = date.getHours();
  var minute = date.getMinutes();
  if ([1,6,0].indexOf(day) > -1 || (day == 1 && hour < 8) || (day == 5 && hour == 16 && minute >= 30)|| (day == 5 && hour >= 17)){
    var timeFrom = Math.floor(date.valueOf()/1000) - 60 * interval;
    var threads = GmailApp.search('is:inbox after:' + timeFrom);
    for (var i = 0; i < threads.length; i++) {
      if (threads[i].isUnread()){
         var sender=threads[i].getMessages()[0].getFrom();
        if(PropertiesService.getScriptProperties().getKeys().length==0){
           PropertiesService.getScriptProperties().setProperty('from', '');
         }
        var scriptProperties = PropertiesService.getScriptProperties().getProperty('fromArray');
        if(scriptProperties.indexOf(sender)==-1){
          threads[i].reply("Hi," + "\n\n Thanks for your email. My working days are Tuesday to Friday, so I won't pick up your message until Tuesday morning.")
          threads[i].markRead();
          threads[i].star();
          scriptProperties=scriptProperties+sender; 
          PropertiesService.getScriptProperties().setProperty('from',  scriptProperties);
          }
      }
    }
  }
}