Google 表格应用程序脚本:当 onChange 函数通过触发器为 运行 时,e.range 为空

Google Sheets App Script: e.range is null when onChange function is run via a trigger

编辑:这个问题让我被禁止提问...我做错了什么?!实在看不懂

编辑:解决方案 这需要一个 onEdit() 函数。谢谢@cooper。

我正在用头撞键盘。请帮忙

这看起来应该很简单...但我仍然收到错误消息:

TypeError: 无法读取未定义的 属性 'getNumColumns' 在 onChange(checkBoxes:5:20)

目标是单击一个复选框,获取日期戳,但我还需要检查列 header(第 4 行)以确定接下来 运行 的功能。 . 但是,我不能走那么远,因为我需要知道触发函数的单元格的列。

  function onChange(e) {
    const range = e.range;
    Logger.log(range.getNumColumns());
  };

/**Trigger:
     Head:= Deployment
     Event:= From spreadsheet - On change
     Function:= onChange
     Error rate:= 100% */

从技术上讲我可以使用 activecell 但我担心用户不可靠,如果他们选中 col:1 中的复选框然后单击 col:2 会怎样 activecell return col:1 或 2?

我已经尝试了多种组合来解决这个问题,但每次都显示“无法读取未定义的 属性 'getNumColumns'”

我试过以下方法:

e.range.columnStart
function onEdit(e) {
  e.range is the location of the edit it could be more than one cell if it's something like a paste.
  e.range.getSheet() is the sheet 
  e.range.columnStart is the column of the edited cell
  e.range.rowStart is the row of the edited cell
  e.range.columnEnd is the last column in e.range
  e.range.rowEnd is the last row in e.range
  e.source is the active spreadsheet
  e.oldValue is the value before the change
  e.value is the value after the edit
  Logger.log(JSON.stringify(e)); //will provide a description of the entire event object
}

有时还有权限级别和用户信息等