谷歌gmail脚本时间自动回复

时间:2016-08-15 12:32:35

标签: google-apps-script gmail

早上好,

我希望有人在我的正常办公时间之外发送电子邮件给我一个自动回复,M-F 8a-4p EST。

我已经想出如何在Sat&太阳,但无法弄清楚如何将它与时间结合起来。以下是我现在正在使用的内容。

感谢您的帮助!

function autoReply() {
  var interval = 5;          //  if the script runs every 5 minutes; change otherwise
  var daysOff = [6,0];   // 1=Mo, 2=Tu, 3=We, 4=Th, 5=Fr, 6=Sa, 0=Su
  var message = "Thank you for your email! I am currently away from the office for the weekend spending time with my family. I will be back in the office Monday morning at 8:00 am EST.  My office hours are Monday - Friday 8:00 am - 4:00 pm EDT. I monitor email periodically over weekends for emergencies.  I look forward to assisting you when I am back in the office.  Thank you!"
  var date = new Date();
  var day = date.getDay();
  if (daysOff.indexOf(day) > -1) {
    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++) {
      threads[i].reply(message);
    }
  }
}

2 个答案:

答案 0 :(得分:0)

您必须以这种方式使用getHours()方法

var d = new Date();
var n = d.getHours();
if (n<8 || n>16){ 
      // "out of office";
}
else{
      //regular
}

答案 1 :(得分:0)

我已将问题的初始代码与 Eugene's 的修改版本结合起来:

    function autoReply() {
      var interval = 5;          //  if the script runs every 5 minutes; change otherwise
      var daysOff = [6,0];   // 1=Mo, 2=Tu, 3=We, 4=Th, 5=Fr, 6=Sa, 0=Su
      var workingHours = [9,17]; // 0-24
      var message = "Thank you for your email! \n\
      I am currently OOO and will be back in regular office hours (though I monitor email periodically for emergencies). \n\Looking forward to assisting you when I am back in the office. Thank you for your understanding!"
      var date = new Date();
      var day = date.getDay();
      var hour = date.getHours();
      if (daysOff.indexOf(day) > -1 || n<workingHours[0] || n>workingHours[1]) {
        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++)
          threads[i].reply(message);
      }
    }
相关问题