Google 表格,每次单元格值更改时计数的单元格计数器

Google Sheets, cell counter that counts every time a cells values change

我正在尝试在 google 工作表中设置一个计数器,用于计算单元格更改或更新的次数。

我希望单元格“K1”计算单元格“Client!A2”的值发生变化的次数。

这是我第一次使用 Apps-Script,所以我觉得我一定遗漏了一些东西。这是输入到 code.gs 部分的内容。

function onEdit(e) {
   if(e.range.getA2Notation() == "Client!A2") {
    var sCounter = e.source.getRange("K1");
    var counter = sCounter.getValue();

    if(counter === 0) {
      counter = 1;
    } else {
      counter ++;
    }
    sCounter.setValue(counter);  
  }  
}

好像不行, 我应该把其他东西放在括号里吗?还是我完全做错了?

function onEdit(e) {
  const sh = e.range.getSheet();
  const shts = ['Sheet1','Sheet2'];//included sheets
  const idx = shts.indexOf(sh.getName());
  if(~idx && e.value != e.oldValue) {
    let n = Number( PropertiesService.getScriptProperties().getProperty('editcounter'));//counter
    PropertiesService.getScriptProperties().setProperty('editcounter',++n);
    sh.getRange('K1').setValue(n);
    e.source.toast(n);
  }
}