onEdit(e) not working in Add-on

天涯浪子 提交于 2019-12-04 11:23:39

Your onOpen() trigger is running the onEdit() trigger. opOpen() runs in in AuthMode.LIMITED when the Add-on is installed and enabled.

In this documentation, it states:

A mode (LIMITED) that allows access to a limited subset of services. This (LIMITED) mode occurs when an add-on or a script bound to a document executes an onOpen(e) or onEdit(e) simple trigger, except in the case described for NONE.

You are running an onOpen() simple trigger, and it is running in LIMITED mode because it's in an add-on.

So, that part, I'm quite sure of.

I believe what you can do, is create an installable edit trigger, and that runs in FULL mode. So, that's what I would try, get rid of the simple trigger, and install a trigger with ScriptApp.

In the documentation, it states:

They (a simple trigger) cannot access services that require authorization. For example, a simple trigger cannot send an email because the Gmail service requires authorization, but a simple trigger can translate a phrase with the Language service, which is anonymous.

Google Documentation

So, that try/catch included sending an email, which would have stopped the onEdit() simple trigger from working.

Add a try/catch to your code, and send yourself an email if there is an error.

function onEdit(e) {try{
  //Code Here

} catch(err) {
  var errMsg = 'There was an error: ' + err +
      + " \n \n" +
      'from the: onEdit function ' +
      + " \n \n" +
      'The call stack is: ' + err.stack;

  GmailApp.sendEmail('yourEmail@gmail.com', "Subject", errMsg);
};

One possible reason is that you "Test as add-on"

https://developers.google.com/gsuite/add-ons/how-tos/testing-editor-addons#testing_details

Installable triggers aren't supported when testing. Functionality that depends on installable triggers are not testable.

But the funny thing is, it works with a bound script.

It made me confusing for quite a while. So the solution is when you need to test the installable triggers, just stick with a bound document.

If it works, the trigger for the add-on will also work.

For other things, you can do "Test as add-on"

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!