Doug Fir
Doug Fir

Reputation: 21292

Can I add a Trigger based on change of a particular cell?

I have a script that is currently set to run onEdit. Ideally, I'd like the script to run ONLY when someone makes a change to cell B26 of the sheet in question.

Is this possible?

Upvotes: 3

Views: 7638

Answers (1)

Alan Wells
Alan Wells

Reputation: 31310

This is code taken directly from the documentation:

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

It already has e.range in it. All you need to do is add an if statement, and get the cell address of the range:

function onEdit(e){
  var cellAddress,cellAddressToTestFor;

  cellAddressToTestFor = 'B26';

  // Get cell edited - If it's B6 then do something
  cellAddress = e.range.getA1Notation();
  Logger.log('cellAddress: ' + cellAddress);

  if (cellAddress === cellAddressToTestFor) {
    //To Do - Code here if cell edited is correct
    Logger.log('the check worked!');
  };
}

Add that code. Edit a cell, then VIEW the LOGS.

Upvotes: 4

Related Questions