Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use onEdit only in one column

I'm currently using this script:

function onEdit(e)

  // Set a comment on the edited cell to indicate when it was changed.
  var range = e.range;
  range.setNote('Laatst veranderd: ' + new Date());

What do I need to add so this will only work in column 'C'?

like image 806
Jelle Poelmans Avatar asked Jan 12 '16 16:01

Jelle Poelmans


1 Answers

Restrict the code from running in a Google Sheet if a certain column is edited. This uses Apps Script onEdit() reserved function name, which is triggered to run on the Edit Event.

Get the column number of the range:

function onEdit(e) {//"e" receives the event object
  var range = e.range;//The range of cells edited

  var columnOfCellEdited = range.getColumn();//Get column number
  //Logger.log(columnOfCellEdited)

  if (columnOfCellEdited === 3) {// Column 3 is Column C
    //Set a comment on the edited cell to indicate when it was changed.
    range.setNote('Laatst veranderd: ' + new Date());
  };
};

Another version:

function onEdit(e) {//"e" receives the event object
  var range = e.range;//The range of cells edited

  var columnOfCellEdited = range.getColumn();//Get column number
  //Logger.log(columnOfCellEdited)


  if (columnOfCellEdited !== 3) {return;}// Halt the code if the column 
    //edited is not column C
    //Set a comment on the edited cell to indicate when it was changed.

  range.setNote('Laatst veranderd: ' + new Date());

};
like image 147
Alan Wells Avatar answered Sep 27 '22 23:09

Alan Wells