Reputation: 503
I want to check the user who is clicking that specific button. When I click on while signed in as my account it runs fine. but if anyone else runs it, it is blank. How can I get the logged user email id?
This is my code:
function check_User(){
var ui = SpreadsheetApp.getUi();
var usr = Session.getActiveUser().getEmail();
ss.toast(usr);
if( usr == "[email protected]" || usr =="[email protected]"){
return true;
}
else{
ui.alert("Not authorized");
return false;
}
}
Upvotes: 2
Views: 5316
Reputation: 3094
See getActiveUser() method description:
If security policies do not allow access to the user's identity,
User.getEmail()
returns a blank string. The circumstances in which the email address is available vary: for example, 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, or a web app deployed to "execute as me" (that is, authorized by the developer instead of the user). However, these restrictions generally do not apply if the developer and the user belong to the same Google Apps for Business domain.
What execute permission is your app deployed with? If it is "as you (owner)", then getActiveUser().getEmail()
will only be populated if you (script owner) and the users belong to the same Google Apps domain.
Judging from your code, you are not using Google Apps domain account, but a regular consumer @gmail.com account to publish your script. In this case getActiveUser().getEmail()
will only be populated if your script is published with execute as user accessing the script permission.
Upvotes: 2