在谷歌应用程序中获取最新表单提交的responseID
getting responseID of latest form submission in google apps
我有一个谷歌表格。每次提交时,答案都会进入谷歌电子表格。我正在尝试编写一个应用程序脚本,该脚本在每次提交表单时都会触发,并在表单数据右侧的列中添加一个"编辑"链接。链接本身很容易生成,谷歌有一个名为getEditResponseURL()的方法。(https://developers.google.com/apps-script/reference/forms/form-response)
但每次我运行它时,我都会收到错误"TypeError:无法调用null的方法"getResponses"。"
这是我的代码:
function addeditlink(e) {
// Get the active sheet
var sheet = SpreadsheetApp.getActiveSheet();
// Get the active row
var row = sheet.getActiveCell().getRowIndex();
//get the form
var form = FormApp.getActiveForm();
//get latest form response
var responses = form.getResponses();
var lastResponse = responses[responses.length - 1];
//get edit URL
var editurl = lastResponse.getEditResponseUrl();
//build link
var editlink = "<a href='"" + editurl + "'">Edit</a>";
//place edit link in column R (index 18)
if (sheet.getRange(row, 18).getValue() == "") {
sheet.getRange(row, 18).setValue(editlink);
}
}
有什么帮助吗?谢谢
解决方案是删除:
var form = FormApp.getActiveForm(); //this is WRONG
并替换为:
var form = FormApp.openByID(' ID here ')
没有"活动表单",因为此脚本是在工作表中运行的,而不是在表单中运行的。
我认为您只能从附加到表单的脚本中调用FormApp.getActiveForm(),而您的脚本包含在GSheet中。我找不到一种方法来轻松获取使用此工作表作为目的地的表格,所以我所做的是获取所有表格,然后查看每个表格的目的地id,并检查它是否与此电子表格相同。一旦你有了Form对象,你就可以得到响应了。感觉有点长篇大论很想知道是否有人知道更快的方法。
FormApp抛出的一些例外情况也需要处理。
这是我使用的功能:
/**
* Find the first form that is linked to a specific spreadsheet
*
* @param {string} spreadsheet id
* @return {object} Form or null
*/
function getFormByDestinationId_(spreadsheetId) {
var formFiles = DriveApp.getFilesByType('application/vnd.google-apps.form');
var form;
var formFile;
var formId;
var destinationId;
while (formFiles.hasNext()) {
formFile = formFiles.next();
formId = formFile.getId();
// Throws an error if ID invalid
try {
form = FormApp.openById(formId);
} catch (error) {
if (error.name === "Exception") {
// Just ignore it
} else {
throw error;
}
}
// Form.getDestinationId() throws an error if there is no destination id
try {
destinationId = form.getDestinationId();
} catch (error) {
if (error.name === "Exception") {
// Just ignore it
} else {
throw error;
}
}
if (destinationId !== spreadsheetId) {
continue;
}
return form;
}
return null;
} // getFormByDestinationId_()
唯一使用:getResponses()
方法的行是:
var responses = form.getResponses();
您的错误:
无法调用空的方法"getResponses"
表示form
为空。如果form
为空,则此行:
//get the form
var form = FormApp.getActiveForm();
不起作用。那么,为什么它不起作用呢?代码没有任何问题,所以这一定是另一个问题。如果存在活动表单,则该代码将返回form
类型。这意味着没有绑定到脚本的表单。getActiveForm()
返回脚本容器绑定到的窗体。
您的脚本未"容器绑定"到表单。您的脚本已绑定到电子表格。
文件说明:
若要与脚本未绑定到容器的表单交互,请改用openById(id)或openByUrl(url)。
您可以从表单的编辑页打开脚本编辑器,将脚本绑定到表单。但是,如果您想将脚本绑定在电子表格上,则无需执行此操作。
var form = FormApp.getActiveForm();
行在电子表格脚本中不起作用。
将事件对象e
与可安装触发器一起使用的问题是,看起来无法获取响应URL。
谷歌表单事件
这意味着您需要在绑定到电子表格的脚本中使用openById(id)
或openByUrl(url)
,或者将所有脚本移动到表单中。
以下是如何从电子表格中的脚本中获取编辑url:
// Open a form by ID.
var form = FormApp.openById('1234567890abcdefghijklmnopqrstuvwxyz');
现在的问题是,您只能通过"FormResponse"类获得编辑响应URL:getEditResponseUrl()
。所以你需要表格回复。
var formResponses = form.getResponses();
但这就是所有的回应,你需要最后一个。
var lastResponseIndex = formResponses.length - 1;
var lastResponse = formResponses[lastResponseIndex];
var editURL = lastResponse.getEditResponseUrl();
或:
function getEditURLofLastResponse() {
// Open a form by ID.
var form = FormApp.openById('Your Form ID');
var formResponses = form.getResponses();
//get last respnse
var lastResponseIndex = formResponses.length - 1;
var lastResponse = formResponses[lastResponseIndex];
var editURL = lastResponse.getEditResponseUrl();
Logger.log(editURL);
}
只是一个观察:
您使用的是e
参数:function addeditlink(e) {
。但我看不出它在你的代码中被使用。这让我怀疑您是否在使用"可安装"触发器,而不是"简单"触发器。
可以获得刚刚与e.values
或e.namedValues
一起提交的值。但是您无法使用事件对象获取编辑URL。
- HTML表单提交时未执行外部函数
- JS表单提交"无法使用Chrome数据保护程序加载此页面.尝试重新加载页面.调试信息:POST CISmtuK
- 将图像上传ajax与表单提交ajax相结合
- Javascript无法处理表单提交
- 如何在我的情况下禁用表单提交
- 加载后的页面与ajax表单提交不起作用
- angularjs-控制器在表单提交时未调用
- 禁用带有字符白名单的表单提交
- 如何使用Google Analytics跟踪表单提交
- 使用javascript将表单提交到iframe目标中
- e.preventDefault在表单提交时被忽略
- AngularJS JQuery Ajax表单提交等效
- JavaScript表单提交帮助
- PHP表单提交和重定向
- 当所有输入文本字段都为空时,禁止表单提交,但当jquery中的任何字段不为空时允许提交
- setTimeOut AFTER jQuery表单提交
- 从函数中选择要触发表单提交的正确元素
- JavaScript表单提交没有't fire asp服务器端点击功能的提交按钮
- 表单提交问题,如何在我的URL末尾获得ID的值
- 在谷歌应用程序中获取最新表单提交的responseID