【发布时间】: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