【问题标题】:Specifying form for 'on form submit' trigger为“提交表​​单”触发器指定表单
【发布时间】:2014-08-10 11:10:13
【问题描述】:

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

我设置了一个包含两个函数的脚本。我希望在提交第一个表单时运行第一个函数。并且,在提交第二个表单时运行的第二个函数。

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

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

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

【问题讨论】:

  • 您是否尝试过使用记录器查看 eventInfo 的确切内容,看看是否可以在其中找到一些特定于表单的信息?我很乐意这样做,但现在我没有电脑......手机不太适合进行此类测试:-) 在每个功能中使用 Logger.log(e) 并通过发送表单响应进行测试。

标签: javascript google-apps-script google-sheets triggers google-forms


【解决方案1】:

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

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

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
  }
}

【讨论】:

    【解决方案2】:

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

    // 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();
    

    如果您不想使用“范围”。

    我希望这会有所帮助!

    【讨论】:

      【解决方案3】:

      我只是在查看一些文档并发现了这个:

      Documentation forForm

      它说:

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

      还有:

      var form = FormApp.openById('1234567890abcdefghijklmnopqrstuvwxyz');
       ScriptApp.newTrigger('myFunction')
           .forForm(form)
           .onFormSubmit()
           .create();
      

      onFormSubmit

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

      【讨论】:

      • 如果错误信息是正确的,那么它是一个服务器错误,而不是代码的问题。但是,之前发生过错误消息并不完全正确。如果您认为这是 Google 的错误或问题,可以在 Apps 脚本问题跟踪器上报告:Link - Issue List
      • 如果有的话,错误消息非常无用。很可能这是一个持续 2 天的服务器错误,但我会感到惊讶。如果我再次遇到它,我会将它添加到跟踪器中!
      猜你喜欢
      • 2013-06-25
      • 1970-01-01
      • 1970-01-01
      • 2015-07-14
      • 2015-03-03
      • 1970-01-01
      • 2022-11-02
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多