Bhu*_*han 8 google-apps google-apps-script google-forms
我有一个google表单,其中包含以下两个字段:
用户将输入他的电子邮件地址并选择工具并单击"提交".我希望看到以下消息:
谢谢你的回复.已发送电子邮件至您输入的电子邮件地址以下载所选工具.
我在脚本编辑器中有以下代码
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);
}
Run Code Online (Sandbox Code Playgroud)
我还设置了triggers
Run - > emailFormSubmission
,Events - > from Form
, onFormSubmit
.
会发生什么:假设第一个用户('A')输入他的信息并点击提交.他输入的信息正确显示.当第二个用户('B')输入其信息并单击提交时,将显示A的信息.当第三个用户('C')输入他的信息并点击提交时,则显示B的信息.我发现问题是"getlastrow()",因为电子表格在处理emailFormSubmission 后更新.
上述代码有什么问题?我该如何解决?
UPDATE
根据@ 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());
}
Run Code Online (Sandbox Code Playgroud)
wch*_*ito 11
请记住,事件On form submit
(了解事件)接收具有以下结构的参数:
你可以这样做:
function emailFormSubmission(e) {
...
var row = e.range.getRow();
...
}
Run Code Online (Sandbox Code Playgroud)
请尝试以下代码来观察参数的结构e
:
function emailFormSubmission(e) {
...
Logger.log(e);
...
}
Run Code Online (Sandbox Code Playgroud)
UPDATE
首先,原谅我的困惑,我向你展示了Spreadsheet form submit event
当你真正使用时的结构Form submit event
.
果然,a 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());
}
}
Run Code Online (Sandbox Code Playgroud)
但是,根据作为表单响应发送的数据设置的确认消息,似乎不太清楚,您可以设置消息,但不会显示活动响应,如果不是下一个.
归档时间: |
|
查看次数: |
7101 次 |
最近记录: |