google apps script - onEdit(e) not working in Add-on -


i've written script works great when used in native spreadsheet. trying publish add-on, , finding onedit(e) not working, when onopen(e) , oninstall(e) work fine.

i've looked on documentation on authorization modes , installing/enabling add-on, think missing (hopefully straightforward) since beginner. should calling functions differently? or placement of onedit? appreciated. thanks!!

function setup() { var ui = spreadsheetapp.getui(); var ss = spreadsheetapp.getactivespreadsheet(); var sheet = ss.getsheetbyname('send auto emails');  try {ss.setactivesheet(ss.getsheetbyname('send auto emails'));}   catch (e) {ss.insertsheet('send auto emails', 0);}  sheet.getrange(1,1).setvalue('recipient email address');     etc...  }  function onedit(e) { var ss = spreadsheetapp.getactivespreadsheet(); var sheet = e.source.getactivesheet(); var range = e.source.getactiverange();      if (range.geta1notation() == "c1" | range.geta1notation() == "d1" |     range.geta1notation() == "e1" && sheet.getname() == "send auto emails") {   browser.msgbox('alert', 'feel free change title here more relevant you.  aware, if use optional item, please make sure referencing in email message appears here, wrapped in < , >. example: <optional item 1>.', browser.buttons.ok); }     if (range.geta1notation() == "j4" && sheet.getname() == "send auto     emails") {   browser.msgbox('alert', 'only add email message body.  "hello, recipient name" , "best, name" automatically added.  if use optional items in message, see example text make sure using them right way.', browser.buttons.ok); }    if (range.geta1notation() == "a2") {     ss.toast("your data in column must not separated blank rows. data after blank row ignored.", "be aware", 90);   }   }   function onopen(e) {   var ui = spreadsheetapp.getui();   ui.createmenu('bulkemail beta')       .additem('1. sheet setup', 'setup')       .additem('2. send emails', 'sendemails')       .addtoui();       onedit(e); }  function oninstall(e) {   onopen(e); } 

edit 1

i've tried creating installable trigger instead of using simple onedit, still no avail.

function createonedit() {   var ss = spreadsheetapp.getactivespreadsheet();    scriptapp.newtrigger('installableonedit')       .forspreadsheet(ss)       .onedit()       .create(); }  function installableonedit(e) {    var ss = spreadsheetapp.getactivespreadsheet();   var sheet = e.source.getactivesheet();   var range = e.source.getactiverange();  try {      if (range.geta1notation() == "c1" | range.geta1notation() == "d1" | range.geta1notation() == "e1" && sheet.getname() == "send auto emails") {   browser.msgbox('alert', 'feel free change title here more relevant you.  aware, if use optional item, please make sure referencing in email message appears here, wrapped in < , >. example: <optional item 1>.', browser.buttons.ok); }         if (range.geta1notation() == "j4" && sheet.getname() == "send auto emails") {   browser.msgbox('alert', 'only add email message body.  "hello, recipient name" , "best, name" automatically added.  if use optional items in message, see example text make sure using them right way.', browser.buttons.ok); }    if (range.geta1notation() == "a2") {     ss.toast("your data in column must not separated blank rows. data after blank row ignored.", "be aware", 90);   }   } catch(err) {   var errmsg = 'there error: ' + err +       + " \n \n" +       'from the: onedit function ' +       + " \n \n" +       'the call stack is: ' + err.stack;  gmailapp.sendemail('elisabeth@groupon.com', "error", errmsg);  } } 

your onopen() trigger running onedit() trigger. opopen() runs in in authmode.limited when add-on installed , enabled.

in this documentation, states:

a mode (limited) allows access limited subset of services. (limited) mode occurs when add-on or script bound document executes onopen(e) or onedit(e) simple trigger, except in case described none.

you running onopen() simple trigger, , running in limited mode because it's in add-on.

so, part, i'm quite sure of.

i believe can do, create installable edit trigger, , runs in full mode. so, that's try, rid of simple trigger, , install trigger scriptapp.

in documentation, states:

they (a simple trigger) cannot access services require authorization. example, simple trigger cannot send email because gmail service requires authorization, simple trigger can translate phrase language service, anonymous.

google documentation

so, try/catch included sending email, have stopped onedit() simple trigger working.

add try/catch code, , send email if there error.

function onedit(e) {try{   //code here  } catch(err) {   var errmsg = 'there error: ' + err +       + " \n \n" +       'from the: onedit function ' +       + " \n \n" +       'the call stack is: ' + err.stack;    gmailapp.sendemail('youremail@gmail.com', "subject", errmsg); }; 

Comments

Popular posts from this blog

get url and add instance to a model with prefilled foreign key :django admin -

css - Make div keyboard-scrollable in jQuery Mobile? -

ruby on rails - Seeing duplicate requests handled with Unicorn -