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

发布于 2025-01-18 07:20:16 字数 762 浏览 3 评论 0原文

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

我正用头撞键盘。

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

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

目标是单击复选框,获取日期戳,但我还需要检查列标题(第 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返回列:1,还是2?

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

我都尝试了以下方法:

e.range.columnStart

This question got me banned from asking questions... what did I do wrong?! I really don't understand.

I am bashing my head against my keyboard.

This seems like it should be simple... but I continue to get the error:

TypeError: Cannot read property 'getNumColumns' of undefined**
at onChange(checkBoxes:5:20)

The goal would be to click a check box, get a date stamp, but I need to also check the column header (Row 4) to determine what function to run next...
However, I can not get that far because I need to know the column of the cell that triggered the function.

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

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

Technically speaking I can use activecell but I am concerned that users are unreliable, what happens if they check a check box in col:1 then click to col:2, will activecell return col:1, or 2?

I have tried a verity of combinations to solve this, but each time it says "Cannot read property 'getNumColumns' of undefined"

I have tried the following:

e.range.columnStart

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

铜锣湾横着走 2025-01-25 07:20:16
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
}

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

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
}

There are others like authorization level and user information sometimes

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文