Is there a way to check OAuth permissions from the onOpen() simple trigger in apps script?
My app creates and shares spreadsheets with my users, and when they open the spreadsheet, I need to be able to check if they've authorized the required scopes yet. Simple triggers can't natively run functions that require authorized scopes. But installable triggers can. I've created a function that will prompt the user for permissions and then install the needed triggers with those scopes. I have a custom menu item that runs this function just fine, but the simple onOpen() trigger can't run that function because it requests OAuth scopes permissions. Many of our users wont realize they need to run that function from the menu for the spreadsheet to work.
My initial idea was to have a hidden cell somewhere in the spreadsheet with a true/false value that starts as "false". When the user opens the spreadsheet, the onOpen() simple trigger:
- checks that cell's value
- if it's false then pops up a ui alert for the user.
The alert tells them to use a custom menu that onOpen() added which will run a function that will:
- prompt the user for permissions
- add onOpen and onEdit installable triggers for that user so the rest of my app works for that user when they interact with the spreadsheet.
- change the hidden cell's value to "true" so that the simple onOpen() trigger doesn't keep prompting the user.
This works great! But only for the first user who accesses the spreadsheet. Once they authorize the permissions, that hidden cell turns to "true" and subsequent users wont be prompted to run the menu function.
My second idea was instead of having that hidden cell as a true/false value, make it a list of the user's gmail accounts that have given permissions (accessed via Session.getActiveUser().getEmail()
). But in order to get the current user's email, I need a OAuth scope! So the simple onOpen() trigger cannot even check if the current user is a new user or not...
Is there another way to check if the active user has authorized the OAuth scopes needed from the simple onOpen() trigger?
I don't want to just rely on my users to know that they have to run that special menu function in order for things to work.