基于时间的触发器无法按预期工作

时间:2014-04-04 02:43:30

标签: google-apps-script google-form

我们正在为公司测试新的午餐订单系统。

基于时间的触发器没有按预期工作,我们的目标是按时开始和停止接受响应,同时向公司中的每个人发送电子邮件并提醒他们订购午餐。

/**
 * Specify the form to operate on
 */
var formId = '-';
var form = FormApp.openById(formId);

/**
 * Set this up as a time-based trigger to open your form at
 * a specific date & time.
 */
function startAcceptingResponses() {
  form.setAcceptingResponses(true);
}

/**
 * Set this up as a time-based trigger to close your form at
 * a specific date & time.
 */
function stopAcceptingResponses() {
  form.setAcceptingResponses(false);
}

function sendEmails() {
  var emailAddress = "thomas@abc.com";
  var message = "Please choose your lunch from this link: https://docs.google.com/<secret>/viewform";     
  var subject = "lunch order submission";
  MailApp.sendEmail(emailAddress, subject, message);
  }


ScriptApp.newTrigger("startAcceptingResponses")
   .timeBased()
   .onWeekDay(ScriptApp.WeekDay.FRIDAY)
   .atHour(9)
   .nearMinute(25)
   .create();


 ScriptApp.newTrigger("stopAcceptingResponses")
   .timeBased()
   .onWeekDay(ScriptApp.WeekDay.WEDNESDAY)
   .atHour(18)
   .nearMinute(00)
   .create();

 ScriptApp.newTrigger("sendEmails")
   .timeBased()
   .onWeekDay(ScriptApp.WeekDay.MONDAY)
   .atHour(18)
   .nearMinute(55)
   .create();

2 个答案:

答案 0 :(得分:0)

我在函数.onWeekDay(...)中遇到了一些问题。 我现在所做的只是将其删除并使用.everyDays(1)代替, 在函数本身我测试当天:

var date = new Date();
var day = date.getDay();
if(day != ScriptApp.WeekDay.SUNDAY) { // I don't work on Sunday
  //...
}

答案 1 :(得分:0)

这是一篇旧帖子,但对于任何想要使用上述代码的人来说都是:

  

如果您使用atHour()或nearMinute()

,则需要频率

来自:https://developers.google.com/apps-script/reference/script/clock-trigger-builder#atHour(Integer)

所以写下你的函数:

DateTime