我有一个谷歌表格。每次提交时,答案都会进入谷歌电子表格。我正在尝试编写一个每次提交表单时触发的应用程序脚本,并在表单数据右侧的列中添加一个“编辑”链接。链接本身很容易生成,谷歌有一个方法叫做 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 ')
没有“事件表单”,因为此脚本在工作表中运行,而不是在表单中运行。
关于javascript - 在谷歌应用程序中获取最新表单提交的 responseID,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28548802/