Google-sheets – How to add a note (containing date) to a cell in column “X” when it is edited

google sheetsgoogle-apps-script

See this thread: https://productforums.google.com/forum/#!category-topic/docs/how-do-i/_oFmvMZUCqg

In particular this script:

function onEdit(e){
  // Set a comment on the edited cell to indicate when it was changed.
  var range = e.range;
  range.setNote('Last modified: ' + new Date());
}

Also found here: https://developers.google.com/apps-script/guides/triggers/#onedit

I'm looking to make this happen only for cells in a certain column, and not sure how to modify the script accordingly (or if I should take a different approach).

Best Answer

You can test the Column the edited cell is in and then act on the cell if it is in the appropriate column. So for Column C:

function onEdit(e){
  // Set a comment on the edited cell to indicate when it was changed.
  var range = e.range;
  var rangeCol = range.getColumn();
  if(rangeCol === 3){
    range.setNote('Last modified: ' + new Date());
  }
}

.getColumn() returns the column number. Other options can be found at the documentation for the Range class.