简体   繁体   中英

Event object for installable trigger

So, I'm trying to write a code that will send an email when a particular value appears in a cell. For this I'm using an installable OnEdit trigger as simple ones can't access mailApp.sendEmail. The installable trigger is for the following function:

function myOnEdit(e){
  var ss = SpreadsheetApp.getActiveSheet();
  var email_finished = "abc@email.com"
  var r = e.range;
  var heading = ss.getrange(1,range.getcolumn()).value

  if (heading == "FINISHED" && r.getvlaues() == "√") {
    MailApp.sendEmail(email_finished, "SOP Review", "SOP has been finished");
  }
}

I'm getting an error that "e" is undefined. I'm wondering how I can reference the event object for the installable onEdit() trigger.

I'm an absolute Javascript noob, so please do enlighten me as much as you can my coding gurus!! Thanks in advance.

EDIT: As Serge said there were a bunch of typos. A side-effect of VBA coding! But I corrected the typos and changed how I was doing it:

function myonEdit(){
  var ss = SpreadsheetApp.getActiveSheet();
  var e = ss.getActiveCell();
  var heading = ss.getRange(1, e.getColumn()).getValues();
  var rvalue = e.getValue(); 
  var email_finished = "abc@xyz.com";

  var ui = SpreadsheetApp.getUi()
  ui.alert("Are you sure?", ui.ButtonSet.OK)

  if (heading == "FINISHED" && rvalue == "√") {
    MailApp.sendEmail(email_finished, "SOP Review", "SOP has been finished");
  }
}

The I added this on a manual trigger from Edit>>Current project's triggers . It works fine when debugging, sends email and all. However, when I edit the sheet, the code isn't triggered! I'm not sure what I'm doing wrong this time.

Yeah (e) only has a value when it's triggered inside the sheet. You can't just test it in the script editor. I had to take time to figure this out too.

Both the other answer and Sandy's comment are right, e is indeed undefined if the function is not called from an "edit" event...

But there is of course a clever workaround presented by my friend Mogsdad in this excellent post : How can I test a trigger function in GAS?


EDIT

There are too many typos in your code and you approach it the wrong way.

Below is a simplified version to show the way it works.You only need to define the e parameters you really need

function test_myOnEdit() {
  var e = {};
  e.range = SpreadsheetApp.getActiveSheet().getActiveRange();
  myOnEdit(e)
}

function myOnEdit(e){
  var email_finished = "abc@xyz.com";
  var range = e.range;
  var heading = range.getValue();

  Logger.log("heading = "+heading);
}

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