Google-sheets – Add timestamp to two different columns in Google Spreadsheets

google sheetsgoogle-apps-scriptgoogle-sheets-timestamp

Need help in adding another function using the below script.
I would like to have a timestamp "start" and "end" on two different columns, whenever the other two separate columns are edited. Based on the below script, I could only have one time stamp, from one column which is the column 2 and time stamp on column 10.

function onEdit() 
{
 var s = SpreadsheetApp.getActiveSheet();
 if( s.getName() == "Sheet1" ) { //checks that we're on the correct sheet
   var x = s.getActiveCell();
   if( x.getColumn() == 2 ) { //checks the column
     var nextCell = x.offset(0, +8);
     if( nextCell.getValue() === '' ) //is empty?
       var time = new Date();
     time = Utilities.formatDate(time, "GMT", "hh:mm");
       nextCell.setValue(time);
   };
 };
}

Best Answer

See if this helps ?

function onEdit(e) {
var s = e.source.getActiveSheet(),
    watchCols = [2, 5],
    offsetCol = [8, 10],
    ind = watchCols.indexOf(e.range.columnStart)
    if (s.getName() !== "Sheet1" || ind === -1) return;
e.range.offset(0, offsetCol[ind])
    .setValue(!e.value ? null : Utilities.formatDate(new Date(), "GMT", "hh:mm"))
}