【问题标题】:Accessing user entered data upon submit in google forms访问用户在谷歌表单中提交时输入的数据
【发布时间】:2013-11-18 00:18:07
【问题描述】:

我有一个包含以下两个字段的 google-form:

  • 电子邮件地址:- 一个文本框
  • 工具:- 单选按钮
    • 工具 1
    • 工具 2
    • 工具 3

用户将输入他的电子邮件地址并选择一个工具并单击提交。我希望出现以下消息:

感谢您的回复。 输入的电子邮件地址已向您发送一封电子邮件,以下载所选工具。

我在脚本编辑器中有以下代码

    function emailFormSubmission() {
        var form = FormApp.getActiveForm();//the current form
        var dest_id = form.getDestinationId(); //the destination spreadsheet where form responses are stored
        var ss = SpreadsheetApp.openById(dest_id);//open that spreadsheet
        var theFormSheet = ss.getSheets()[0]; //read the first sheet in that spreadsheet
        var row = theFormSheet.getLastRow(); //get the last row
        var emailid = theFormSheet.getRange(row,2,1,1).getValue();//get column 2 corresponding to the email id. column 1 is timestamp. so, skip that.
        var tool = theFormSheet.getRange(row,3,1,1).getValue();//get column 3 corresponding to the selected tool.
        form.setConfirmationMessage('Thanks for responding. An email has been sent to you '+ emailid + ' to download' + tool);
    }

我还将 triggers 设置为 Run -> emailFormSubmission, Events -> from Form , onFormSubmit。

发生的情况是:假设第一个用户 ('A') 输入他的信息并单击提交。他输入的信息得到正确显示。当第二个用户 ('B') 输入他的信息并单击提交时,将显示 A 的信息。当第三个用户('C')输入他的信息并单击提交时,将显示 B 的信息。我发现问题出在“getlastrow()”上,因为电子表格在 emailFormSubmission 处理后更新。

上面的代码有什么问题?我该如何解决这个问题?

更新

基于@wchiquito 的cmets,我将代码更改为以下以使其工作。

function emailFormSubmission(e) {
    var form = FormApp.getActiveForm();
    //Check this link on how to access form response:
    //https://developers.google.com/apps-script/understanding_events?hl=en

    var responses = e.response;//e is of type formresponse.
    var emailid = responses.getItemResponses()[0].getResponse();
    var tool = responses.getItemResponses()[1].getResponse();
    Logger.log(emailid);
    Logger.log(tool);
    form.setConfirmationMessage('Thanks for responding. An email has been sent to  '+ emailid + '   with instructions to download ' + tool +'. If you do not find our email in your inbox, please check your spam folder');  
    Logger.log(form.getConfirmationMessage());
}

【问题讨论】:

  • 我试过了,它仍然为下一次提交设置确认消息,而不是当前的。所以如果 A 提交了表单,然后 B 提交它,B 得到 A 的响应。
  • emailFormSubmission,这个功能用完了吗?我想现在是onFormSubmit()

标签: google-apps-script google-apps google-forms


【解决方案1】:

请记住,事件On form submit (Understanding Events) 接收具有以下结构的参数:

  • 价值观​​​
  • 范围
  • namedValues​​​

你可以这样做:

function emailFormSubmission(e) {
    ...
    var row = e.range.getRow();
    ...
}

试试下面的代码观察参数e的结构:

function emailFormSubmission(e) {
    ...
    Logger.log(e);
    ...
}

更新

首先,请原谅我的困惑,当您真正使用Form submit event 时,我向您展示了Spreadsheet form submit event 的结构。

果然,Form submit event 的结构如下:

  • 回复

返回FormResponse 类型的对象。

因此,定义事件:On submit form (Form submit event),您可以执行如下操作:

function emailFormSubmission(e) {
  var itemResponses = e.response.getItemResponses();
  for (var i = 0, len = itemResponses.length; i < len; ++i) {
    Logger.log('Response #%s to the question "%s" was "%s"',
               (i + 1).toString(),
               itemResponses[i].getItem().getTitle(),
               itemResponses[i].getResponse());
  }
}

但是,根据表单的响应发送的数据设置的确认消息,似乎不是很清楚,您可以设置消息,但不会显示为主动响应,如果不是下一个。

【讨论】:

  • 感谢您的回复,但这没有帮助。当我记录它时,我得到e 是undefined。还有其他想法吗?
  • @Bhushan:从表单发送响应时,在参数e 中获取undefined?如果参数e 得到undefined,请不要直接从代码编辑器运行该函数。
  • 感谢您的提示。我是初学者,所以不知道必须从实时表单而不是编辑器测试脚本。您的代码 sn-p 有所帮助。 e 是FormResponse 类型,通过e.response 访问FormResponse,如在表单提交事件中所述:developers.google.com/apps-script/understanding_events?hl=en。现在,我可以打印所有正确的信息,但是 form.setConfirmationMessage() 没有按预期工作。提交时,我只收到一条默认消息,上面写着“您的回复已被记录”,而不是我的自定义消息。有什么想法吗?
  • 您的确认信息应更改为类似:form.setConfirmationMessage('Thanks for responding. An email has been sent to you '+ e.emailid + ' to download' + e.tool);。但是,您正在记录这些元素(您说它们打印正确),您应该使用相同的方法来访问这些变量。如果在您调用form.setConfirmationMessage() 时出现error(可能不再定义emailid 和tool),那么它将使用默认消息代替 - 您会看到这种情况发生,因此可能会发生错误。
  • @ChrisCirefice - 感谢您的来信。问题不在于代码。我必须对脚本进行身份验证才能允许它运行。身份验证不时过期。因此,重新验证并运行它使其工作。
【解决方案2】:

我的第一个猜测是这里的这两行:

var emailid = theFormSheet.getRange(row,2,1,1).getValue();//get column 2 corresponding to the email id. column 1 is timestamp. so, skip that.
var tool = theFormSheet.getRange(row,3,1,1).getValue();//get column 3 corresponding to the selected tool.

当您在工作表上调用 getLastRow() 时 - 您将获得最后一行。当然,但考虑到事件的顺序以及这些值的处理方式,您需要一个+1,才能获得最近的提交。目前,当您的代码运行以更新表单确认消息时,您落后一排。

所以只需将您的代码更改为以下内容:

var emailid = theFormSheet.getRange(row+1,2,1,1).getValue();
var tool = theFormSheet.getRange(row+1,3,1,1).getValue();

在我看来,电子表格是最令人困惑的 Google 服务。当您在电子表格中获取值时,它们会以[] 或[][] 的形式返回,具体取决于您调用getValues() 时的范围。但是 sheet 上的 getRange() 从索引 1 开始(我想是为了更容易阅读代码)。由于数据的传递方式,我经常发现我有一个错误的错误。使用电子表格时请记住这一点:)

【讨论】:

  • 感谢您的回复,但这没有帮助。当我访问 row+1 时,变量 emailid 和 tool 都是空字符串。还有其他想法吗?
  • 我很高兴@wchiquito 的回答能够帮助您。就我个人而言,我不依赖脚本中的表单提交中的events - 我需要在随机时间多次处理数据,所以我有其他参数可以遵循。我想我的答案是针对那些代码在提交表单之后运行的人,并且通过获取特定范围来处理响应电子表格的原始数据。很抱歉,我在那里陷入了自己的小世界——我想我只是假设你和我的做法一样,因为我没有在你的问题中看到 event 变量:P
【解决方案3】:

简短的回答:您想要的无法通过 Google 表单完成。

说明: form.setConfirmationMessage() 为存储在服务器上的表单设置确认消息,而不是当前活动表单。同样适用于例如 form.setTitle()。活动表格不会被修改。人们会期望确认消息有不同的行为,但可惜事实并非如此。

【讨论】:

    【解决方案4】:

    是的,您可以使用附加组件“Formfacade”来做到这一点。 以 1 种形式免费使用。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-08-17
      • 2016-08-10
      • 2023-04-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-11-05
      • 1970-01-01
      相关资源
      最近更新 更多