Google-sheets – Timestamp code for Google Sheets

google sheetsgoogle-apps-scriptgoogle-sheets-timestamp

I am using this script in order to put a time stamp in column B when someone edits in a row. However, I need it to place the time stamp if someone edits a row in column A. I don't need it to place a stamp every time someone makes a change on the rest of the sheet. Can someone tell me what I need to change on this script in order for the time stamp to only be placed in columb B if the cell to the left of it in column A is edited?

function onEdit() {
  var s = SpreadsheetApp.getActiveSheet();
  var r = s.getActiveCell();
  if( r.getColumn() != 2 ) { //checks the column
    var row = r.getRow();
    var time = new Date();
    time = Utilities.formatDate(time, "GMT-05:00", "MM/dd/yy, h:mm a");
    SpreadsheetApp.getActiveSheet().getRange('B' + row.toString()).setValue(time);
  };
 };

Best Answer

The "if" conditional should check whether the edited column has number 1. So it's ...getColumn() == 1 instead of ...getColumn() != 2.

In addition, I simplified the script which wasn't particularly well written. Using the event object, and its range property, eliminates the need for most of that code.

function onEdit(e) {
  if (e.range.getColumn() == 1) {                // check if the edit was in column A 
    e.range.offset(0, 1).setValue(new Date());   // put timestamp one cell to the right
  }
}