Home > Software design >  How to get Cell address in google script on change event
How to get Cell address in google script on change event

Time:08-01

I want to run a function when I change a specific cell in my google sheets. onEdit(e) function runs whenever I change any cell in my sheet but I want to run a function when I change a specific cell e.g. ws.getRange("b1"). Is there any method like e.range.getAddress in google sheets onEdit event?

CodePudding user response:

You can find more details about events here. They provided the following example, it's comparable to your use-case:

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

CodePudding user response:

Using the event object of an onEdit trigger

function onEdit(e) {
  const sh = e.range.getSheet();
  Logger.log(JSON.stringify(e));//allows you to view the event object
  if(sh.getName() == "Your sheet name" && e.range.columnStart == "your column number" && e.range.rowStart == "Your row number" && e.value == " Your value") {
    //your operation.  
  }
}

If you wish to see the event object of an onChange event then write the function:

function onMyChange(e) {
  Logger.log(JSON.stringify(e));
}

Create an onEdit trigger for "onMyChange" and make a change. The got to executions and view the event object. There will be items in the event object that are not documented. I use them all of the time and I've never had problems.

CodePudding user response:

From the question

Is there any method like e.range.getAddress in google sheets onEdit event?

Yes, there is: Range.getA1Notation()

function onEdit(e){
   const address = e.range.getA1Notation();
   console.log(address);
}
  • Related