6

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'?

Alan Wells
  • 30,746
  • 15
  • 104
  • 152
Jelle Poelmans
  • 61
  • 1
  • 1
  • 2

3 Answers3

10

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());

};
Alan Wells
  • 30,746
  • 15
  • 104
  • 152
0

You may also try to extract the column index. Function getA1Notation() returns cell position which can be used to parse column.

function onEdit(e){
  // Set a comment on the edited cell to indicate when it was changed.
  var range = e.range;
  var title = range.getA1Notation();
  var data = {
   'bookName': 'Some book',
   'whoAdded': 'Nick',
   'whenAdded': new Date()
 };
 var options = {
   'method' : 'post',
   'contentType': 'application/json',
   // Convert the JavaScript object to a JSON string.
   'payload' : JSON.stringify(data)
 };
  if(title.charAt(0) === 'B'){
      var result = UrlFetchApp.fetch('https://xxx.xxx', options);
  }else{
    range.setNote('Failed to upload request :(');
  }
}
NickitaX
  • 352
  • 2
  • 14
-3

If you want the script to work for edits in column C, try

function onEdit(e) {
if(e.range.columnStart === 3) {
e.range.setNote('Laatst veranderd: ' + new Date());
  }
}
JPV
  • 26,499
  • 4
  • 33
  • 48