google-apps-script - Google Apps 脚本 - 如何在 onFormSubmit (Google Forms 事件)中获取目标电子表格中的相应记录?

标签 google-apps-script google-forms google-apps-script-addon

当受访者发送响应时我捕获此事件

https://developers.google.com/apps-script/guides/triggers/events#form-submit_4

在此函数内,我想将一些信息添加到目标电子表格中的相应行(Google 自动创建此行)

为了做到这一点,首先我需要找到这一行

目前我能找到的最好方法是使用 sheet.getLastRow()

https://developers.google.com/apps-script/reference/spreadsheet/sheet#getlastrow

但是这个方法存在并发问题:Serge insas在这里说的:https://stackoverflow.com/a/17751544/2667212

There are a few risks to use that simple "strategy" as there might be concurrency issues when 2 or more people send a form simultaneously

有没有正确的方法来实现这一目标?

最佳答案

解决方案:

最简单的方法是禁用 Google 提供的自动电子表格链接并使用自定义链接。

片段:

function formSubmitInForm(e){
 const formResponse = e.response;
 const id = formResponse.getId();
 const timestamp = formResponse.getTimestamp();
 const sheet = SpreadsheetApp.openById(/*SPREADSHEET ID*/)
    .getSheetByName(/*FORM RESPONSE SHEET*/);
 const responses = formResponse.getItemResponses().map(function(itemRes){
    return itemRes.getResponse();
 })
 sheet.appendRow([id,timestamp].concat(responses))
}

引用:

关于google-apps-script - Google Apps 脚本 - 如何在 onFormSubmit (Google Forms 事件)中获取目标电子表格中的相应记录?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57736961/

相关文章:

javascript - 如何使用谷歌应用程序脚本设置谷歌表单中项目的父级?

javascript - 设置与 Google 表单中的标签不同的值

javascript - 另一个插件的访问方法

google-apps-script - 您无权调用 showSidebar/showModalDialog

google-apps-script - AuthMode 对多个登录用户感到困惑

javascript - Google Apps 脚本触发成功

google-apps-script - 使用 Apps 脚本动态编辑实时 Google 表单中的多项选择选项

使用 MailApp.sendEmail 时电子邮件被拒绝

javascript - 使用 Google 脚本将共享文件移至我的云端硬盘?

forms - 使用 Google 表单更新现有的 Google 表格