angelokh
angelokh

Reputation: 9428

Google Sheet Add-on get user email

I have an Google Sheet add-on published in the store. But I can't get the user email back.

On Code.js, onStartup() when the app(sidebar) is loaded on the screen.

function onStartup() {
  return Session.getActiveUser().getEmail();
}

On my client side javascript, this is how I invoke onStartup().

google.script.run
        .withSuccessHandler(
          function(ret, scope) {
            console.log(">>> user email is: " + ret);
          })
        .withFailureHandler(
          function(msg, scope) {
          })
        .withUserObject()
        .onStartup();

EDIT:

Try getEffectiveUser() if you're working on an Add-on.

Upvotes: 4

Views: 3503

Answers (2)

user9937931
user9937931

Reputation:

From: https://developers.google.com/apps-script/reference/base/session#getactiveuser

...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)

.getEffectiveUser() is indeed the better option especially where custom functions are involved.

Upvotes: 0

Bjorn Behrendt
Bjorn Behrendt

Reputation: 1264

This worked in my test, it did initially give me some issues for an unknown reason:

Example: https://docs.google.com/spreadsheets/d/1zevasz5XjumO92qOwYIvsqoUPkzyB9EBD27pWH2I5e0/copy

code.gs

function onOpen() {
  var ui = SpreadsheetApp.getUi()
  var menu = ui.createAddonMenu().addItem('Launch Sidebar', 'sidebar').addToUi();
}

function sidebar(){
   var ui = SpreadsheetApp.getUi();
  var html = HtmlService.createHtmlOutputFromFile("Sidebar.html")
  .setSandboxMode(HtmlService.SandboxMode.IFRAME)
  .setTitle("Sidebar Listbox");
  ui.showSidebar(html);
}

function getListItems(){
  var activeUser = "Active User";
  var effectiveUser = "Effective User";
  try{
    activeUser = Session.getActiveUser().getEmail();
  } catch(err1){
    activeUser = err1;
  }
  try{
    effectiveUser = Session.getEffectiveUser().getEmail();
  } catch(err2){
    effectiveUser = err2; 
  }
  var returnArray = [activeUser,effectiveUser]; 
  return returnArray;
}

Sidebar.html

  <link rel="stylesheet" href="//code.jquery.com/ui/1.11.2/themes/smoothness/jquery-ui.css">
  <link rel="stylesheet" href="/resources/demos/style.css">

<p>
Effective User: <span id="effectiveUser"></span>
</p>
<p>
Active User: <span id="activeUser"></span>
</p>


  <script src="//code.jquery.com/jquery-1.10.2.js"></script>
  <script src="//code.jquery.com/ui/1.11.2/jquery-ui.js"></script>

  <script>
  $(function() {    //This function runs automatically when the sidebar is opended  
        google.script.run.withSuccessHandler(loadListItems).withFailureHandler(fail).getListItems();
  });


    function loadListItems(returnedData){
    $( "#effectiveUser" ).html(returnedData[0]);
    $( "#activeUser" ).html(returnedData[1]);   
  }
     function fail(){
        //update the field with the id of yourChoice
      $( "#effectiveUser").html("<strong> Opps, something went wrong.... </strong>");
  }
  </script>

Upvotes: 4

Related Questions