OnEdit(e) Google Sheets to clear adjacent cell

Viewed 90

I am trying to get the below code to run, and it won't trigger. The goal is to clear B2, when B1 is changed

function onEdit(e) {
  const sh = e.range.getSheet();
  if(sh.getName() == "Quotes V2" && e.range(1,2) )
  {
    e.range(2,2).clear();
  }
}
1 Answers

Explanation:

  • onEdit trigger works only when the user changes the value of a cell. If the value is changed by a script or formula, then onEdit won't work for you.

  • Familiarize yourself with the event object. This expression is wrong e.range(2,2). Because e.range is not a function but a range object.

Solution:

function onEdit(e){
  const sh = e.range.getSheet();
  if(sh.getName() == "Quotes V2" && e.range.getA1Notation()=='B1' )
  {
    sh.getRange('B2').clear();
  }
}
Related