1

I'm building an add on that needs to check how many units a customer has left. This code works for me as the developer however, it doesn't work when users actually install the application.

// When someone opens the sheet for the first time
function onOpen(e) {
    var ui = SpreadsheetApp.getUi()
        .createAddonMenu()
        .addItem('SMS', 'sms')
        .addItem('EMAIL', 'email')
        .addItem('SMS and EMAIL', 'emailAndSMS')
        .addToUi();
}

// The function for the SMS Side Bar
// It is here I run the code for getting the users email address. 
// Nothing is logged to the console at all.

function sms() {
    var html = HtmlService.createHtmlOutputFromFile('sms').setTitle('Heartbeat SMS');
    SpreadsheetApp.getUi().showSidebar(html);
    var userEmail = Session.getActiveUser().getEmail();
    Logger.log(userEmail); // Nothing gets logged to the console.
}

I expected to see users Email addresses being logged, however I get an empty string back

1 Answers1

0

According to the documentation on getActiveUser():

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

So having your app deployed will be one of these scenarios when they cannot use them. the documentation do goes on to say:

However, these restrictions generally do not apply if the developer runs the script themselves or belongs to the same G Suite domain as the user.

What this means is that if another user ran your script from, for example, the GAS editor, it would work. for your intention to work, they would need to somehow submit their address, be it in a form, a document or part of the add-on.

AMolina
  • 1,295
  • 1
  • 4
  • 16