Why is Session.getActiveUser().getEmail() returning empty for non-owner accounts in Google Sheets?

When triggered by users other than the owner, Session.getActiveUser().getEmail() remains blank. How can I obtain the correct email?

function verifyCurrentUser() {
  var appInterface = SpreadsheetApp.getUi();
  var currentEmail = Session.getActiveUser().getEmail();
  var spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
  spreadsheet.toast(currentEmail);
  if(currentEmail === "[email protected]" || currentEmail === "[email protected]"){
    return true;
  } else {
    appInterface.alert("Access denied");
    return false;
  }
}

My experience with this issue led me to reexamine the deployment strategy used by the app. I initially encountered the same problem where the Session.getActiveUser().getEmail() returned empty for non-owner accounts. After some research and testing, I discovered that deploying the script as a web app with proper OAuth settings significantly improved reliability. I also experimented with deploying the script as a bound script versus standalone, and the context in which it runs can affect the output. In my case, ensuring that the script was correctly authorized and adjusting the deployment context resolved the issue.

In my experience, this behavior is due to Google’s security restrictions. I hit the same issue when working on a shared document and realized that the method is limited to only showing the owner’s email. The problem arises because getActiveUser() returns the email only when the script has been authorized in a specific way, often tied to the domain or the owner’s privileges. I found that when I switched to using getEffectiveUser() in some cases and carefully set up the OAuth consent screen, I got more reliable results. Adjusting the deployment settings to use web apps with proper configuration also helped to ensure consistency across different user accounts.

im not a 100% sure but i fixed it by deploying as a web app so it runs as owner. also, switching to getEffectiveUser() sometimes helps. check your oauth and permissions settings too - google often hides email for non owner runs.

Based on my experience, the issue you’re encountering when using Session.getActiveUser().getEmail() is often related to the deployment method and the script’s authorization context. I encountered similar problems on a project where the script was container-bound. The remedy involved adjusting the deployment settings by converting it to a web app and ensuring the appropriate scopes were requested. Although switching to getEffectiveUser() seemed promising, it didn’t fully resolve the issue in my case. Careful review of your OAuth consent and permissions setup was crucial in obtaining a more reliable behavior from the API.

hey, i had similer trouble. i ended up deployin it as a web app and double checked my oauth settings. sometimes switching to getEffectiveUser() helps too. google security tends to block emails for non owners so make sure your permissions are all set.