Session.getActiveUser.getEmail() blank? Google sheets

旧城冷巷雨未停 提交于 2021-02-10 14:23:22

问题


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 == "abc@gmail.com" || usr =="myemail@gmail.com"){ 

    return true;
  }
  else{
    ui.alert("Not authorized");
    return false;
   }
  }

回答1:


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.



来源:https://stackoverflow.com/questions/28474704/session-getactiveuser-getemail-blank-google-sheets

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