javascript - 在谷歌应用程序中获取最新表单提交的 responseID

标签 javascript google-apps-script google-forms

我有一个谷歌表格。每次提交时,答案都会进入谷歌电子表格。我正在尝试编写一个每次提交表单时触发的应用程序脚本,并在表单数据右侧的列中添加一个“编辑”链接。链接本身很容易生成,谷歌有一个方法叫做 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/

相关文章:

google-apps-script - 在脚本中使用电子表格标准函数?

javascript - 下载为 Microsoft Excel 时,Google Script 中的自定义函数不起作用

google-apps-script - 提交 Google 表单后如何发送电子邮件?

google-apps-script - 如何将 "Edit Response"链接添加到 Google Forms 电子邮件?

javascript - Electron + Vue API 请求落到应用程序 ://

javascript - HighCharts 自定义 SVG 标记符号

javascript - 内存泄漏和 jQuery 插件

javascript - 这是多余的吗?

javascript - 如何在执行脚本一段时间后暂停 javascript 然后继续?

forms - 在提交事件中使用 Google App 脚本获取 Google 表单中特定问题/项目的值