Google Apps 脚本:"Script function not found" 错误

Google Apps Script: "Script function not found" error

我一直在为我的问题寻找解决方案,但一直找不到,因此我在 Stack Overflow 中写下了我的第一个问题。

我目前正在 Google Apps Script 上编写一个简单的程序,如果用户忘记在特定日期之前提交 Google 表单,它会向用户发送电子邮件提醒。现在我有 3 个不同的函数:onFormSubmitscheduleTriggerreminderEmail。以下是我到目前为止写出的代码:

/**
This function searches the Google Form for the date when the previous
session was held & the date when the next session will be held. 
This function also calculates the date when to remind the user if they
forget to submit the Google Form. This reminder date is calculated to be 2 
days after the date when the next session is held. 
This function calls the next function: scheduleTrigger.
*/
function onFormSubmit(e) {
    var form = FormApp.getActiveForm();
    var frm = FormApp.getActiveForm().getItems();
    var futureDate = new Date(e.response.getResponseForItem(frm[3]).getResponse());
    var pastDate = new Date(e.response.getResponseForItem(frm[0]).getResponse());

    var reminderDate = new Date(futureDate.setDate(futureDate.getDate() + 2));
    futureDate.setDate(futureDate.getDate() - 2);

    scheduleTrigger(reminderDate, futureDate, pastDate, form);
}

/**
This function schedules the reminder email trigger at a specific date. The
specific date is the reminder date that was calculated in the previous function.    
This function calls the next function: reminderEmail. 
*/
function scheduleTrigger(reminderDate, futureDate, pastDate, form) {
    ScriptApp.newTrigger('reminderEmail(reminderDate, futureDate, pastDate, form)').timeBased().atDate(reminderDate.getFullYear(), reminderDate.getMonth(), reminderDate.getDate()).inTimezone('America/New_York').create();
}

/**
This function checks the submissions if the form has been submitted. 
If there is no submission, then it sends out an email reminder. 
*/
function reminderEmail(reminderDate, futureDate, pastDate, form) {
    var count = 0;
    var formResponses = form.getResponses();
    for (var i = 0; i < formResponses.length; i++) {
        var formResponse = formResponses[i];
        var itemResponses = formResponse.getItemResponses();
        for (var j = 0; j < itemResponses.length; j++) {
            var itemResponse = itemResponses[j];
            if (itemResponse == futureDate) {
                count++; 
            }
        }
    }

    if (count != 2) {
        MailApp.sendEmail("test@gmail.com",
                          "Submit Form Reminder",
                          "Hey! You didn't fill out the form yet!");
    };
}

我 运行 遇到的问题是,每当我 运行 程序时,我都会收到一条错误消息,指出函数 reminderEmail "The selected function cannot be found"。我环顾四周并遵循了之前为解决此问题所做的 posts,例如重命名函数并使用全新的不同 Google From 重新启动,但没有任何效果。我也拥有该脚本的完全所有权和授权,因此权限应该不是问题。

如果您对如何解决此问题有任何想法,请告诉我。欢迎任何和所有反馈!如果我的问题有任何不清楚的地方,请告诉我。感谢您花时间阅读这篇冗长的 post。

失败的原因是您只需要将函数名称传递给 ScriptApp.newTrigger。你不能用它发送参数,这是你必须以其他方式处理的事情。

Documentation

/**
This function searches the Google Form for the date when the previous session was held & the date when the next session will be held. 
This function also calculates the date when to remind the user if they forget to submit the Google Form. This reminder date is calculated to be 2 days after the date when the next session is held. 
This function calls the next function: scheduleTrigger.
*/
function onFormSubmit(e) {
  var form = FormApp.getActiveForm();
  var frm = FormApp.getActiveForm().getItems();
  var futureDate = new 
Date(e.response.getResponseForItem(frm[3]).getResponse());
  var pastDate = new 
Date(e.response.getResponseForItem(frm[0]).getResponse());

  var reminderDate = new Date(futureDate.setDate(futureDate.getDate() + 2));
  futureDate.setDate(futureDate.getDate() - 2);

  scheduleTrigger(reminderDate);
}

/**
This function schedules the reminder email trigger at a specific date. The specific date is the reminder date that was calculated in the previous function.    
This function calls the next function: reminderEmail. 
*/
function scheduleTrigger(reminderDate) {
  ScriptApp.newTrigger('reminderEmail').timeBased().atDate(reminderDate.getFullYear(), reminderDate.getMonth(), reminderDate.getDate()).inTimezone('America/New_York').create();
}

/**
This function checks the submissions if the form has been submitted. If there is no submission, then it sends out an email reminder. 
*/
function reminderEmail() {
  var form = FormApp.getActiveForm();
  var count = 0;
  var formResponses = form.getResponses();
  for (var i = 0; i < formResponses.length; i++) {
   var formResponse = formResponses[i];
   var itemResponses = formResponse.getItemResponses();
    for (var j = 0; j < itemResponses.length; j++) {
      var itemResponse = itemResponses[j];
      if (itemResponse == futureDate) { // Unsure what this part is for, perhaps you should find another way to calculate this.
        count++; 
      }
    }
  }

  if (count != 2) {
    MailApp.sendEmail("test@gmail.com",
                      "Submit Form Reminder",
                      "Hey! You didn't fill out the form yet!");
  };
}

我能够将您的代码清理成它应该工作的方式,但不确定 reminderEmail 函数中的 futureDate