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

Time:02-18

I'm trying to get a counter going in google sheets that will count how many times a cell has changed or updated.

I want cell "K1" to count how many times cell "Client!A2" values have changed.

This is my first time using Apps-Script so I feel like I must be missing something here. This is what has been input into the code.gs section.

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);  
  }  
}

It doesn't seem to be working, Am I supposed to be putting anything else in the brackets? Or am I just doing it wrong altogether?

CodePudding user response:

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);
  }
}
  • Related