0

My understanding is, the only way to authorize a script is to execute it and then it'll do all of the authorizations it needs. How do we do this for functions that are used in conjunction with an even trigger like on form submit?

Say I have a function like this:

function onFormSubmit(e){ ... }

If this function triggers on form submit then e will have the event details. But if I just execute it normally then e will be undefined.

And if my function needs e to do other things, like send an e-mail using MailApp then the script will have to be authorized to send e-mail using MailApp.

So how to get the script authorized for everything that needs authorization?

IMTheNachoMan
  • 5,343
  • 5
  • 40
  • 89

1 Answers1

2

Add an innocuous function like

/**
 *
 * The purpose of this function is to trigger the script authorization dialog without
 * throwing any errors.
 */
function doNothing(){}

Then run it. Even if it do nothing it will trigger the script authorization dialog.


From https://developers.google.com/apps-script/guides/services/authorization

Granting access rights

Apps Script determines the authorization scopes (like access your Google Sheets files or Gmail) automatically, based on a scan of the code. Code that is commented out can still generate an authorization request. If a script needs authorization, you'll see one of the authorization dialogs shown here when it is run.


Rubén
  • 34,714
  • 9
  • 70
  • 166
  • Some things are done at run time so it wont' know what authorization it needs. For example, the `e` event object will have a `FormResponse` which will require authorization for certain functions. – IMTheNachoMan May 14 '19 at 17:18
  • Actually it does as all the required authorization scopes are loaded every time a function is ran. Just try it, is super easy and there isn't any risk. – Rubén May 14 '19 at 17:36
  • I added a quote. – Rubén May 14 '19 at 18:01