0

I'm having trouble making my OnEdit function work properly. I have a trigger checkbox embedded into my spreadsheet as a Named Range, so that my team can "check" the trigger box in order to run the script, without having to dive into scripts at all. The Named Range button I created in my spreadsheet is called "MasterClear".

However, when I attempt to run the OnEdit script, I am getting an error message in line 3. The functions work by themselves when I test them. But, the OnEdit trigger alone is not performing. I borrowed this line of code from a forum I found online, so I'm not able to decode or resolve it myself. Can anyone help?

SCRIPT //

  function onEdit(e) {
  var MasterClear = SpreadsheetApp.getActiveSpreadsheet().getRangeByName("MasterClear").getA1Notation(); 
  var actv = e.range.getA1Notation();
  if (actv == MasterClear ) {
    clear1();
    clear2();
 
}
}

function clear1() {
  var sheet = SpreadsheetApp.getActive().getSheetByName('Team 3');
  sheet.getRange('T11:T300').clearContent() ;
 
}

function clear2() {

  var sheet = SpreadsheetApp.getActive().getSheetByName('Team 3');
  sheet.getRange('O11:O300').clearContent() ;
  
}
Rubén
  • 34,714
  • 9
  • 70
  • 166
  • 1
    I use `e.source.toast()` a lot and I regularly check view/executions when things are working. In particularly difficult situations I've run the entire function from another function call, where I have supplied the event object myself. But most of the time I simply debug in place by editing a test sheet and capture information either in a toast or on a spreadshet. – Cooper Dec 11 '20 at 23:21
  • 1
    Does this answer your question? [How can I test a trigger function in GAS?](https://stackoverflow.com/questions/16089041/how-can-i-test-a-trigger-function-in-gas) – Iamblichus Dec 14 '20 at 10:34
  • You are trying to run the `onEdit` function from the script editor, so `e` is not populated. Take a look at [this answer](https://stackoverflow.com/a/63851123). – Iamblichus Dec 14 '20 at 10:35

1 Answers1

0

This works for me:

I changed the ranges and and sheet names for my ease of viewing and the name of the named range. I use the toasts for letting me know what's working and what's not and I was pushing the event object into Z1 for debugging information. I also reset the checkbox back to false each time.

function onEdit(e) {
  //e.source.toast('Entry');
  //const sh=e.range.getSheet();
  //sh.getRange('Z1').setValue(JSON.stringify(e));
  if(e.range.getA1Notation()==e.source.getRangeByName('MyCheckBox').getA1Notation() && e.value=="TRUE") {
    e.range.setValue("FALSE");
    clear1(e);
    clear2(e);
  }
}

function clear1(e) {
  //e.source.toast('clear1');
  e.source.getSheetByName('Sheet1').getRange('2:2').clearContent();
}

function clear2(e) {
  //e.source.toast('clear2');
  e.source.getSheetByName('Sheet1').getRange('3:3').clearContent();
}

You could just put a button on the sheet and not use onEdit() at all. In many ways that is a lot easier and less burden on the server. Not that most people worry about that.

Cooper
  • 59,616
  • 6
  • 23
  • 54
  • Thanks for this. I am actually not familiar with how a toast works. Can you describe what this is for and how it works? – eeesimmons Dec 14 '20 at 02:32
  • I'm trying to implement this into my sheet now though, and not sure how to make it work. I'm not sure that I am savvy enough to get the toast working. But, with the code you suggested, I am getting an error message that says: Cannot read property 'range' of undefined (line 5). Any advice? What I tried to do was change the [MyCheckbox] to be my Named Range instead, which is called [MasterClear]. Then, for both clear1 and clear2 functions, I add my sheet names and cell ranges. Those are the only changes I made in order to adapt this for my spreadsheet. It is not working. – eeesimmons Dec 14 '20 at 02:39