Google sheers: On edit event trigger

61 views Asked by At

I have a dropdown (data validation) cell and I need to clear a few cells every time that the user changes that drop down. I am using the script below and nothing happens.

function onEdit(e){
  if (e) {
     var ss = e.source;
     var s = ss.getActiveSheet();
     var editedCell = ss.getActiveCell();
     var tsheet = "Target";
     if(sname === tsheet && editedCell.getColumn() == 3 && editedCell.getrow() == 5){   
         value = "";
         ss.s.getRange(7,3).setValue(value);
         ss.s.getRange(9,3).setValue(value);
         ss.s.getRange(10,3).setValue(value);
         ss.s.getRange(12,3).setValue(value);
         ss.s.getRange(14,3).setValue(value);
         ss.s.getRange(16,3).setValue(value);      
    }
  }
}
2

There are 2 answers

0
Andy On

You have sname === tsheet. I imagine you just want the == operator or if you're just declaring a new variable, just use a regular assignment outside the if. You never use sname, so I don't quite see what it's for. Also, rather than setting the cells to an empty value, you can use clear() instead. If the issue still persists, check that your triggers are properly set up in your script (Resources -> Current Project's Triggers).

3
JPV On

Alternative version:

function onEdit(e){
 var s = e.source.getActiveSheet();
 if(s.getName() !== "Target" || e.range.getA1Notation() !== 'C5') return;   
 [7, 9, 10, 12, 14, 16].map(function (r) {
 s.getRange("C"+r).clear();
     });
 }