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

imt*_*irl 0 triggers onchange google-sheets google-apps-script

这个问题让我被禁止提问...我做错了什么?!我真的不明白。

我正用头撞键盘。

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

类型错误:无法在 onChange(checkBoxes:5:20) 处读取未定义**的属性“getNumColumns”

目标是单击一个复选框,获取日期戳,但我还需要检查列标题(第 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% */
Run Code Online (Sandbox Code Playgroud)

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

我尝试了多种组合来解决此问题,但每次都会显示“无法读取未定义的属性‘getNumColumns’”

我已经尝试过以下方法:

e.range.columnStart
Run Code Online (Sandbox Code Playgroud)

Coo*_*per 6

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
}
Run Code Online (Sandbox Code Playgroud)

有时还有其他的,比如授权级别和用户信息

  • 您无法运行此函数,它只是事件对象中值的解释,如果没有触发器提供的事件对象,您无法真正运行此类函数 (2认同)