Tuesday 12 March 2019

Using OnEdit to add Date to Previous Cell in Google Sheets

Here's a nifty bit of code that will add a Date to the cell of your row when you enter data in the 3rd cell.


function onEdit(e) {
 
    var ss = e.source.getActiveSheet();
    if (ss.getName() !== 'sheet1' || e.range.columnStart !== 2return;
    e.range.offset(0, -1)
        .setValue(e.value ? new Date() : null);
}


Or if you want to have it on a couple of sheets you could use


function onEdit(e) {
 
    var ss = e.source.getActiveSheet();
  if (ss.getName() == 'sheet1' && e.range.columnStart == 2) {
    e.range.offset(0, -1)
        .setValue(e.value ? new Date() : null);
  }
  else if (ss.getName() == 'sheet3' && e.range.columnStart == 2){
    e.range.offset(0, -1)
        .setValue(e.value ? new Date() : null);
  }
  else if (ss.getName() == 'sheet5' && e.range.columnStart == 2){
    e.range.offset(0, -1)
        .setValue(e.value ? new Date() : null);
  }
  else {
   return; 
  }
  
}


No comments: