【问题标题】:How to trigger the onEdit event of a referenced cell?如何触发引用单元格的 onEdit 事件?
【发布时间】:2012-12-07 12:42:28
【问题描述】:
我正在编写 Google 电子表格的 Google 应用脚本,但遇到了一些问题。我尝试通过其值被其他单元格引用的单元格触发“onEdit”函数。但是,它似乎效果不佳。比如A单元格的值被B单元格引用(A的值是“='B'”),如果我改变了B单元格的值,A单元格也会随之改变,但是无法触发onEdit函数一个细胞。我在想是否是因为电子表格自动进行的更改不是会触发 onEdit 函数的事件。
有人知道解决此问题的其他解决方案吗?
提前致谢!
【问题讨论】:
标签:
google-apps-script
triggers
google-sheets
【解决方案1】:
onEdit 将在单元格更新时运行(如您所料)。使用上面的逻辑,是否可以在onEdit 事件中设置检查以查看更改了哪个单元格,如果是B,您能否做出A 也更改的逻辑假设?
function onEdit(event)
{
// 'event' contains information about the edit that took place
// Here, we read the 'source' and get the active range (our changed cell)
// We then pull out the string form of the notation to get the cell that changed
var changedCell= event.source.getActiveRange().getA1Notation();
Browser.msgBox(changedCell)
if (changedCell == 'B1') {
// If the changed cell is the one that affects A, do something
Browser.msgBox('You changed B1, and this will affect A');
}
}
【解决方案2】:
正如您所提到的,onEdit 将针对单元格 B 而不是单元格 A 触发。您可以使用它来读取单元格 A,或者您可以每分钟触发一个函数来读取单元格 A。