如何在提交事件处理程序中获取表单值? [英] How to get form values in the submit event handler?

查看:233
本文介绍了如何在提交事件处理程序中获取表单值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试着从一个非常简单的Google表单开始,这个表单只包含几个问题(一个只有2个选项和一个短文本的多选)。创建它之后,我打开脚本编辑器并键入

function onSubmit(e){
Logger.log(onSubmit(%s),JSON.stringify(e));
}

并配置 onSubmit 作为表单提交的处理程序,使用编辑菜单中的当前项目触发器触发。



填写表单并立即提交确实会导致处理程序被调用,但我只在日志中看到:

  [17-04-15 18:56:23:584 CEST] onSubmit({response:{},source:{},authMode:{},triggerUid:1870249629})

即响应字段为空。我也尝试过使用 FormApp.getActiveForm()。getResponses(),但它也返回一个包含多个空对象的数组(OTOH, FormApp。 getActiveForm().getTitle()确实返回了我给出的表单的标题)。



我怀疑我需要给脚本一些额外的权限访问表单数据,但我不知道如何去做,即使这是真正的问题。



有谁知道为什么我没有得到表单价值观,我该怎么做才能得到它们?提前致谢!

解决方案

有两种检索提交值的模式。对于这两种模式,从表单提交中检索值的函数都必须作为触发器进行安装。可安装触发器的详细信息是 https://developers.google.com/apps- script / guides / triggers / installable


1。脚本在电子表格上打开。



在这种情况下,通过安装触发器,您可以通过脚本检索提交的值。事件对象的详细信息是 https://developers.google。 com / apps-script / guides / triggers / events#form-submit



脚本:

  function onSubmit(e){
Logger.log(%s,JSON.stringify(e));
}

结果:

  {
values:[
date and time,
test
],
namedValues:{
fromtestform:[
test
],
timeStamp:[
date and time$
range:{
columnStart:1,
rowStart:2,
rowEnd:2,
columnEnd:2
},
source:{},
authMode:{},
triggerUid:#####
}



2。脚本在表单上打开。



在这种情况下,可以通过以下脚本检索提交的值。详细信息是 https://developers.google.com/apps-script / reference / forms / form-response



脚本

  function onSubmit(e){
Logger.log(authMode =%s,source.getId()=%s,e.authMode,e.source.getId ());
var items = e.response.getItemResponses();
for(i in items){
Logger.log(getItem()。getTitle()=%s,getResponse()=%s,items [i] .getItem()。getTitle ),items [i] .getResponse());




结果: p>

  authMode = FULL,source.getId()= ###表单ID ### 
getItem()。getTitle )= ## item's title ##,getResponse()= test

如果我误解了你的问题,对不起。

I'm trying to get started with a very simple Google Form containing just a couple of questions (a multiple choice with just 2 options and a short text). After creating it, I opened the script editor and typed in

function onSubmit(e) {
  Logger.log("onSubmit(%s)", JSON.stringify(e));
}

and configured onSubmit as the handler for "form submit" trigger using the "Current project's triggers" from the "Edit" menu.

Filling in the form and submitting it now does result in the handler being called, but I only see this in the log:

[17-04-15 18:56:23:584 CEST] onSubmit({"response":{},"source":{},"authMode":{},"triggerUid":1870249629})

i.e. the response field is empty. I've also tried using FormApp.getActiveForm().getResponses(), but it returns an array of several empty objects too (OTOH, FormApp.getActiveForm().getTitle() does return the title I gave the form).

I suspect I need to give the script some extra permissions to access the form data, but I have no idea how to do it, nor even if this is really the problem.

Does anybody know why am I not getting the form values and what should I do to get them? Thanks in advance!

解决方案

There are 2 patterns for retrieving submitted values. For both patterns, the function for retrieving the values from form submission has to be installed as a trigger. The detail information of Installable Triggers is https://developers.google.com/apps-script/guides/triggers/installable.

1. Script is opened on spreadsheet.

In this case, by installing a trigger, you can retrieve the submitted values by your script. The detail information of Event Objects is https://developers.google.com/apps-script/guides/triggers/events#form-submit.

Script :

function onSubmit(e){
  Logger.log("%s", JSON.stringify(e));
}

Result :

{
  "values": [
    "date and time",
    "test"
  ],
  "namedValues": {
    "fromtestform": [
      "test"
    ],
    "timeStamp": [
      "date and time"
    ]
  },
  "range": {
    "columnStart": 1,
    "rowStart": 2,
    "rowEnd": 2,
    "columnEnd": 2
  },
  "source": {},
  "authMode": {},
  "triggerUid": #####
}

2. Script is opened on form.

In this case, the submitted values can be retrieved by following script. The detail information is https://developers.google.com/apps-script/reference/forms/form-response.

Script :

function onSubmit(e){
  Logger.log("authMode=%s, source.getId()=%s", e.authMode, e.source.getId());
  var items = e.response.getItemResponses();
  for (i in items){
    Logger.log("getItem().getTitle()=%s, getResponse()=%s", items[i].getItem().getTitle(), items[i].getResponse());
  }
}

Result :

authMode=FULL, source.getId()=### form ID ###
getItem().getTitle()=## item's title ##, getResponse()=test

If I misunderstand your question, I'm sorry.

这篇关于如何在提交事件处理程序中获取表单值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆