首页 文章

Google Apps脚本定义时间驱动触发器不工作的时间范围

提问于
浏览
1

我正在使用我在这里看到的代码中的函数:http://mashe.hawksey.info/2012/09/google-app-script-scheduling-timed-triggers/带有我添加的子函数,如指示所示 .

function scheduledCollection2(){
  var schedule = [];
  // dates/times in mm/dd/yyyy hh:mm - timezone matches settings in File > Project properties
  schedule.push({start:"10/25/2014 09:00", end:"12/25/2014 10:00"});
  checkCollect2(schedule);
}

function checkCollect2(schedule){
  var now = new Date();
  for (i in schedule){
    var start = new Date(schedule[i].start);
    var end = new Date(schedule[i].end);
    if (now > start && now < end){
      function sendEmails() {
        var emailAddress = "abc@gmail.com";
        var message = "Hello, this is a test email.";
        var subject = "Sending Recurring Emails";
        MailApp.sendEmail(emailAddress, subject, message);
      }
    }
  }
}

sendEmails功能单独运行良好 . 但是,当我因某种原因将其添加到代码的其余部分时,它会停止工作 . 我的目标是让它与时间驱动的触发器一起工作 .

关于它为什么不起作用的任何建议,以及我如何让它工作?

1 回答

  • 0

    简单地以不同的方式构建脚本:(而不是定义函数只执行代码)

    function checkCollect2(schedule){
      var now = new Date();
      for (i in schedule){
        var start = new Date(schedule[i].start);
        var end = new Date(schedule[i].end);
        if (now > start && now < end){
            var emailAddress = "abc@gmail.com";
            var message = "Hello, this is a test email.";
            var subject = "Sending Recurring Emails";
            MailApp.sendEmail(emailAddress, subject, message);
        }
      }
    }
    

相关问题