Google Apps脚本:“找不到脚本功能”错误

时间:2018-10-01 19:56:32

标签: google-apps-script google-form

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

我目前正在使用Google Apps脚本编写一个简单的程序,如果用户忘记在某个日期之前提交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!");
    };
}

我遇到的问题是,每当我运行该程序时,我都会收到一条错误消息,指出针对功能提醒电子邮件的“找不到所选功能”。我环顾四周并关注解决该问题的先前文章,例如重命名功能并使用全新的其他Google From重新启动,但没有任何效果。我还对该脚本具有完全所有权和授权,因此权限不应该成为问题。

如果您对解决此问题有任何想法,请告诉我。任何反馈都欢迎!如果我的问题中有任何不清楚的部分,请告诉我。感谢您抽出宝贵的时间阅读这篇冗长的文章。

1 个答案:

答案 0 :(得分:0)

失败的原因是因为您只需要将函数名称传递给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!");
  };
}

我能够按应有的方式清理您的代码,但是不确定futureDate函数中的reminderEmail