简体   繁体   中英

OnEdit(e) Google Sheets to clear adjacent cell

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

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

I am trying to change or Edit 1 cell which should trigger to clear some other ranges. Here what I have:

function onEdit(e) {
var sh = e.range.getSheet();

if(sh.getName() == "sheet1" && e.range.getA1Notation()=='E4')
{
  sh.getRange('e7:e10').clear();
  sh.getRange('e12:e15').clear();
  sh.getRange('e17:e22').clear();
  sh.getRange('e24:e27').clear();
  sh.getRange('e29:e31').clear();
  sh.getRange('e33:e34').clear();
  sh.getRange('e36:e39').clear();
  sh.getRange('e4').clear();
  sh.getRange('b5').clear();
  sh.getRange('d5').clear();
  sh.getRange('g4').clear();
 }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM