Callum
Callum

Reputation: 11

Google script counter for edits

I am trying to count any edits made in range A:I & set the value in the sheet, however my script only works for column edits currently, not row.

Here's the script I'm using:

function onEdit(e) {
    var ss =SpreadsheetApp.getActiveSpreadsheet()
    var s=ss.getActiveSheet()
    var editColumn=e.range.getColumn()
    var editRow = e.range.getRow()
    if(editColumn == 4 && editRow >=2) {
        var sCounter = s.getRange(editRow,editColumn+1,1,1);
        var counter = sCounter.getValue();
        if(counter === 0) {
            counter = 1;
        } else {
            counter ++;
        }
        sCounter.setValue(counter);  
    }
}

Upvotes: 1

Views: 155

Answers (1)

Cooper
Cooper

Reputation: 64062

Try this:

function onEdit(e) { 
  var sh=e.range.getSheet();
  //e.source.toast('Flag1');
  if(sh.getName()=="Sheet1" && e.range.columnStart<10 && e.range.rowStart>1) {
    //e.source.toast('Flag2');
    if(!PropertiesService.getDocumentProperties().getProperty('EditCounter')) {
      PropertiesService.getDocumentProperties().setProperty('EditCounter', 1);
      //e.source.toast('Flag3');
    }else{
      PropertiesService.getDocumentProperties().setProperty('EditCounter', Number(PropertiesService.getDocumentProperties().getProperty('EditCounter'))+1);
      e.source.toast(PropertiesService.getDocumentProperties().getProperty('EditCounter'));
    }
  }
}

You will probably want to remove the last toast that provides you with the Edit Counter.

function resetEditCounter() {
  var ss=SpreadsheetApp.getActive();
  PropertiesService.getDocumentProperties().setProperty("EditCounter", 0);
  ss.toast(PropertiesService.getDocumentProperties().getProperty('EditCounter'));
}

You can tie the below function to a menuitem to get the current count.

function getEditCounter() {
  var ss=SpreadsheetApp.getActive();
  var html=Utilities.formatString('Edit Counter: %s',PropertiesService.getDocumentProperties().getProperty('EditCounter'));
  var userInterface=HtmlService.createHtmlOutput(html);
  SpreadsheetApp.getUi().showModelessDialog(userInterface, 'Current Edit Counter');
}

Upvotes: 1

Related Questions