-1

I am trying to create a timestamp for our Google Sheets script. The main goal is to create a script, which would make a timestamp in every "odd" column if something is added to any "even" column.

Right now, I have found this:

function onEdit() {
 var s = SpreadsheetApp.getActiveSheet();
 if( s.getName() == "Sheet1" ) {
   var r = s.getActiveCell();
   if( r.getColumn() == 13 ) { 
     var nextCell = r.offset(0, 1);
     if( nextCell.getValue() === '' ) 
       var time = new Date();
       time = Utilities.formatDate(time, "GMT", "HH:mm:ss");
       nextCell.setValue(time);
   };
 };
}

It works perfectly, but unfortunately, only for the 13th column. How to make this code work for every even column?

Community
  • 1
  • 1

1 Answers1

0

You can use the modulo operator (as explained here) to check if the edited column is 'even' or 'odd'. Asuming you want the code to work on 'Sheet1', see if this code works for you

function onEdit(e) {
if(e.source.getActiveSheet().getName() !== 'Sheet1' || 
e.range.columnStart % 2 > 0) return;
var off = e.range.offset(0, 1);
if(!off.getValue()){ 
off.setValue(Utilities.formatDate(new Date(), "GMT", 
"HH:mm:ss"))
}
}
JPV
  • 26,499
  • 4
  • 33
  • 48