问题描述
我希望仅当I1
中的值更改时才清除单元格J2:K2
.
I want cell J2:K2
to be cleared ONLY when value in I1
changes.
到目前为止,我有以下脚本:
I have the following script so far :
function onEdit(e) {
if (e.range.getA1Notation() === 'i1') {
SpreadsheetApp.getActiveSpreadsheet().getSheetByName('sheet1').getRange('j2:k2').clear()
}
}
我认为onEdit
仅在更改I1
时才会启动,但是当我尝试在J2
或K2
中写入值时,范围J2:K2
会不断被清除.有人可以帮我吗?
I thought onEdit
would only fires up when I1
is changed however range J2:K2
keeps getting cleared when I try to write values in either J2
or K2
. Can someone please help me?
推荐答案
简单修复.这对我有用:
Simple fix. This works for me:
function onEdit(e) {
if (e.range.getA1Notation() === 'I1') {
SpreadsheetApp.getActiveSpreadsheet().getSheetByName('sheet1').getRange('j2:k2').clear();
}
}
问题是您的if
语句条件未得到满足.这是因为.getA1Notation()
返回范围的字符串说明这需要准确.用I1
替换i1
可以解决此问题.
The problem was your if
statement condition was not getting met. This is because .getA1Notation()
returns a string description of the range which needs to be exact. Replacing i1
with I1
fixes the problem.
希望这会有所帮助!
这篇关于当某个单元格的值发生变化时,Google工作表onEdit的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!