Google Apps 脚本:“找不到脚本函数”错误

Posted

技术标签:

【中文标题】Google Apps 脚本:“找不到脚本函数”错误【英文标题】:Google Apps Script: "Script function not found" error 【发布时间】:2018-10-01 19:56:31 【问题描述】:

我一直在为我的问题寻找解决方案,但一直没有找到,因此我在 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】:

它失败的原因是你只需要将函数名传递给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

【讨论】:

谢谢!这非常有帮助,我希望我的声誉更高,以便显示我的支持! futureDate 用于确保用户已提交 Google 表单。例如,如果用户表示他们的会话将在 10 月 1 日举行,则它是 futureDate。然后下次他们提交表单时,会话就会发生,所以现在是pastDate。这意味着应该有 2 个 10/1 实例,也就是 futureDate,如果用户按时提交了表单,就会出现。 [待续] [cont.] 程序检查 Google 表单电子表格中的提交,并确保在提醒电子邮件功能中有 2 个 futureDate 实例。如果没有,那就是当它发送电子邮件提醒用户提交他们的 Google 表单时。我希望这能澄清一点,如果您发现任何令人困惑的地方,请告诉我。 1) 如果您觉得我的回答有助于回答您提出的直接问题,可以将其标记为已接受。 2) 您应该通过搜索表单在reminderEmail 中添加计算futureDate 的逻辑。

以上是关于Google Apps 脚本:“找不到脚本函数”错误的主要内容,如果未能解决你的问题,请参考以下文章

找不到脚本函数 doget,但我没有在我的代码上使用 doGet

是否可以使用 Apps 脚本运行 Google 表格插件?

Google Apps 脚本:Google 表格分组

如何通过 Apps 脚本在 Google 表格中“清除格式”

使用 Google BigQuery / Apps 脚本为插入 Google 表格的数据添加时间戳

如何使用 Google Apps 脚本执行 API + Java 创建 Google 表单?