google-apps-script - 用于在从表单插入的行上插入时间戳的 Google 表格脚本?如何使用 onChange 检测插入的行?

标签 google-apps-script google-sheets

我正在尝试编写一个脚本,该脚本将在从表单(不是谷歌表单)插入到 Google 工作表的行(第 23 列)上插入时间戳;它是其他供应商将数据发送到工作表和不传递时间戳)。

我一直在尝试根据示例编写一些脚本,但我似乎无法让它工作。到目前为止,我的脚本如下:

function setUpTrigger() {
  ScriptApp.newTrigger('timestamp')
  .forSpreadsheet('spreadsheet_id_goes_here')
  .onChange()
  .create();
}

function timestamp(e){

  if (e.changeType == 'INSERT_ROW'){
    SpreadsheetApp.getActiveSpreadsheet().getActiveSheet().getRange(e.range.getRow(), 23).setValue(new Date());
  } 
}

非常感谢任何帮助

最佳答案

问题:

onChange 事件对象 e 不包含键 range。它不直接提供有关 range 的信息。

解决方案:

onChange 的大多数情况下,特别是在您的情况下,更改类型是 INSERT_ROWactiveRange 表示插入的当前行。

function timestamp(e){
  if (e.changeType == 'INSERT_ROW'){
    const sh = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
    sh.getRange(sh.getActiveRange().getRow(), 23).setValue(new Date());
  } 
}

相关回答:

关于google-apps-script - 用于在从表单插入的行上插入时间戳的 Google 表格脚本?如何使用 onChange 检测插入的行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63839264/

相关文章:

javascript - Google Apps 脚本属性服务 Obj 文字中的意外标记

javascript - 在 Google 电子表格中声明全局数组

javascript - 无法将 FormData POST 到 Google 表格,希望限制受邀编辑电子表格的人员的访问权限

google-sheets - 如何计算列中最后七个值的平均值?

python - 插入行 python gdata.spreadsheets.client

google-apps-script - 如何使用应用程序脚本删除特定单元格?

google-apps-script - 谷歌表格 : getting a sheet by its index

asynchronous - Google Apps 脚本网络应用程序未按预期异步运行

google-sheets - 查询产品总和

google-apps-script - 将一个范围从一个电子表格复制到另一个电子表格