You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

为何进入onEdit函数时oldValue始终为undefined?

解决Google Apps Script中onEdit事件oldValue始终为undefined的问题

问题根源是你代码里的属性名拼写错误:你写的是event.oldvalue,但Google Apps Script事件对象中正确的属性名是oldValue(注意字母V是大写)。由于JavaScript对大小写敏感,小写的v会导致无法读取到旧值,因此返回undefined。

修正后的代码如下:

function onEdit(event) {
  // 修正拼写:使用大写V的oldValue
  const valueBeforeEdit = event.oldValue;
  const valueAfterEdit = event.value;
  
  outputDebuggingMsg("valueAfterEdit = " + valueAfterEdit +
                  "\n valueBeforeEdit = " + valueBeforeEdit);
}

另外确认两个前提条件(符合官方文档要求):

  • 编辑操作针对单个单元格(批量编辑、粘贴多单元格内容时不会触发oldValue)
  • 编辑前单元格确实存在有效值(空单元格编辑后不会生成oldValue)

内容的提问来源于stack exchange,提问作者Chris Sharp

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.15 17:02:33