Google 表单提交触发器和电子表格填充之间的竞争条件?

Race condition between Google Form submission trigger and spreadsheet population?

我有一个 Google 表格,用于记录对电子表格的回复。我编写了一个 Apps 脚本,该脚本在提交表单时触发,它根据响应填充电子表格中的其他列。

我假设电子表格的最后一行包含正在记录的回复:

function onFormSubmit(event) {
  const form = FormApp.getActiveForm();
  const spreadsheet = SpreadsheetApp.openById(form.getDestinationId());
  const sheet = spreadsheet.getSheetByName("Parts List");
  const row = sheet.getLastRow();
  ...

这在测试中效果很好,但今天我看到一个提交发生的实例,脚本 运行 在 previous 提交的行上,好像在电子表格中记录新响应之前执行的触发器。

有没有办法保证运行我的脚本只在电子表格更新后执行?

作为解决方法,我可以自己记录对电子表格的回复,而不是使用表单的那个功能。

来自问题

Is there a way to guarantee that my script executes only after the spreadsheet is updated?

对电子表格使用表单提交可安装触发器,而不是对表单使用触发器。电子表格应该是链接到表单的电子表格。

I assume that the last row of the spreadsheet is the one that contains the response that is being recorded:

这可能并不总是正确的。如果您需要获取响应行,请使用 event.range.getRow()(其中 event 是来自电子表格可安装触发器的表单提交事件)