我有一个谷歌表单,有以下两个字段:

  • 电子邮件地址:- 一个文本框
  • 工具:- 一个单选按钮
    • 工具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 待运行-> emailFormSubmission, 活动 -> from Form , onFormSubmit.

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

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

更新

根据 @wchiquito 的评论,我将代码更改为以下以使其工作。

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());
}
有帮助吗?

解决方案

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

  • 价值观​​
  • 范围
  • 命名值​​

你可以这样做:

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

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

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

更新

首先,请原谅我的困惑,我向您展示了 Spreadsheet form submit event 当你真正使用一个 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());
  }
}

不过,根据作为表单响应发送的数据设置的确认消息,似乎不是很明确,可以设置该消息,但对于主动响应不会显示,如果不用于下一个。

其他提示

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

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(). 。但 getRange() 在一个 sheet 从索引 1 开始(我想是为了更容易阅读代码)。我经常发现由于数据传递的方式而出现差一错误。使用电子表格时请记住这一点:)

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

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

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top