HELP! User restrictions using onEdit(e)

232 views
Skip to first unread message

Andrew Apell

unread,
Feb 5, 2022, 2:27:29 PM2/5/22
to Google Apps Script Community
Hi guys!
I'm trying to limit access to certain functions in my publicly available Google Sheets add-on.
I read this in the documentation concerning getEmail():

"the user's email address is not available in any context that allows a script to run without that user's authorization, like a simple onOpen(e) or onEdit(e) trigger, a custom function in Google Sheets"

Which, to me, means that I should not be able to see the user email when using a custom function or simple triggers.
HOWEVER, when I run a simple test like this:

if (Session.getActiveUser().getEmail() != spec...@example.com) {
// Do something;
}

It works without throwing any error.

My question is: Is this behaviour to be expected or will an error be thrown under certain circumstances I have not yet considered?

Zack Reynolds

unread,
Feb 5, 2022, 2:32:30 PM2/5/22
to google-apps-sc...@googlegroups.com
So from my experience, if a user has previously authorized the script on that project, you will be allowed to access their email in functions like onOpen or custom functions, but if a user has not authorized the script, then yes, you won't be allowed to access their user info in any non-authorized way.

--
You received this message because you are subscribed to the Google Groups "Google Apps Script Community" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-apps-script-c...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/google-apps-script-community/cf869658-8887-4ebe-9709-075aa4fb5c86n%40googlegroups.com.

Andrew Apell

unread,
Feb 5, 2022, 2:39:04 PM2/5/22
to Google Apps Script Community
So, in this case what error would show up for the person who didn't authorize the add-on?

Zack Reynolds

unread,
Feb 5, 2022, 2:50:42 PM2/5/22
to google-apps-sc...@googlegroups.com
So that would depend on what it was. If it was a custom function, it would error out, other things might just not run with an error in the script executions, or you could add functionality into your scripts to handle the exception. You could do a try/catch inside of an onOpen to see if that method is accessible and prompt them to authorize the script if not (with say a menu item).

Andrew Apell

unread,
Feb 5, 2022, 2:55:23 PM2/5/22
to Google Apps Script Community
OK. My tests just now seem to agree with your comment.
When I test for email under oneEdit, the error goes away. Let me bite the bullet and see if this works.

Reply all
Reply to author
Forward
0 new messages