Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/google-apps-script/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 为“表单提交”触发器指定表单_Javascript_Google Apps Script_Google Sheets_Triggers_Google Forms - Fatal编程技术网

Javascript 为“表单提交”触发器指定表单

Javascript 为“表单提交”触发器指定表单,javascript,google-apps-script,google-sheets,triggers,google-forms,Javascript,Google Apps Script,Google Sheets,Triggers,Google Forms,我有两个google表单,它们将数据发送到一个电子表格中的两个选项卡 我已经设置了一个包含两个函数的脚本。我想在提交第一份表格时运行第一个函数。以及在提交第二个表单时运行的第二个函数 问题是:在设置触发器onformsubmit时,它不允许您指定哪个表单 因此,每当完成第一个表单时,它都会运行这两个函数 问题:如何限制函数仅在提交特定表单时运行?我刚刚查看了一些文档,发现: 它说: 创建并返回绑定到给定表单的FormTriggerBuilder 此外: 该代码似乎与特定的表单相关联。正如我在评论

我有两个google表单,它们将数据发送到一个电子表格中的两个选项卡

我已经设置了一个包含两个函数的脚本。我想在提交第一份表格时运行第一个函数。以及在提交第二个表单时运行的第二个函数

问题是:在设置触发器onformsubmit时,它不允许您指定哪个表单

因此,每当完成第一个表单时,它都会运行这两个函数


问题:如何限制函数仅在提交特定表单时运行?

我刚刚查看了一些文档,发现:

它说:

创建并返回绑定到给定表单的FormTriggerBuilder

此外:


该代码似乎与特定的表单相关联。

正如我在评论中所建议的,您可以通过分析响应对象的内容来确定表单提交的来源

下面是一个基本的例子来说明:它将向您发送一封电子邮件,告诉您已提交了哪个表单,但您当然可以使用相同的条件测试来选择要运行的操作

function formSubmitOriginTest(e){
//  Logger.log(JSON.stringify(e));
// example value : {"namedValues":{"Untitled Question 2":["test"],"Timestamp":["8/17/2014 11:22:47"]},"values":["8/17/2014 11:22:47","test"],"source":{},"range":{"rowStart":3,"rowEnd":3,"columnEnd":2,"columnStart":1},"authMode":{}}
  if(e.namedValues["Untitled Question 2"]!=undefined){// only form B has one question with this exact title, that's enough to identify it.
    MailApp.sendEmail(Session.getActiveUser().getEmail(),'form B has been submitted','');// optionally include the answers in the email body if you want
  }else{
    MailApp.sendEmail(Session.getActiveUser().getEmail(),'form A has been submitted','');// optionally include the answers in the email body if you want
  }
}

另一种方法是通过range属性查看响应的事件对象中的工作表名称。以下是我如何确定触发提交的表单的示例:

// set the sheet name that stores the form responses you care about here:
function getSourceSheetName() { return 'Form Responses 1'; }

function submit(eventObj) {
    var range = eventObj.range;

    var eventSourceSheetName = range.getSheet().getName();
    var givenSourceSheetName = getSourceSheetName();

    // if the source sheet (from form response sheet) is not the same as the specified form response sheet, quit (return) so extra forms don't trigger the code
    if (eventSourceSheetName.toUpperCase() !== givenSourceSheetName.toUpperCase() ) {
        Logger.log('A different form just submitted data - quit code');
        return;
    }

    // you now know which form submitted the response, so do whatever you want!
    // ... your code goes here
}
基本上,此代码通过range.getSheet.getName获取表单响应来自的工作表的名称。接下来,它检查该名称是否是我们在函数getSourceSheetName中指定的要查找的指定名称

我认为也可以通过以下方式获取活动图纸名称:

eventObj.source.getActiveSheet().getName();
以防您不想使用范围


我希望这有帮助

您是否尝试使用记录器查看eventInfo的确切内容,并查看是否可以在其中找到一些特定于表单的信息?我会很乐意做的,但现在我没有电脑。。。手机不太适合做这样的测试:-在每个功能中使用Logger.loge,并通过发送表单响应进行测试。如果错误消息正确,则是服务器错误,而不是代码问题。但是,以前也发生过这样的情况,错误消息并不完全正确。如果你认为这是谷歌的一个bug或问题,你可以在应用程序脚本问题跟踪器上报告:如果有什么问题的话,错误消息是毫无帮助的。很可能是服务器错误持续了2天,但我会感到惊讶。如果我再次遇到它,我会将它添加到跟踪器中!
eventObj.source.getActiveSheet().getName();