Google Apps Script: Get current user email from a spreadsheet Add-On

Retrieve the current user email using App Script in your Spreadsheet Add-On is a little bit tricky. You can easily use the Session object to get it but in some cases, this object will return undefined.

Current user email using Session object

var userEmail = Session.getActiveUser().getEmail();

So, you need to find a workaround. The workaround could be to use the PropertyService object.

Current user email using PropertiesService object

var userEmail = PropertiesService.getUserProperties().getProperty('userEmail');

Why not, that’s another way to get it but not 100% too. If you want to secure it and combine another way to get it, you need to work with the sheet protection.

Current user email using spreadsheet protection

var protection = SpreadsheetApp.getActive().getRange('A1').protect();
protection.removeEditors(protection.getEditors());
var editors = protection.getEditors();
if (editors.length === 2) {
    var owner = SpreadsheetApp.getActive().getOwner();
    editors.splice(editors.indexOf(owner), 1);
}
userEmail = editors[0];
protection.remove();
PropertiesService.getUserProperties().setProperty('userEmail', userEmail);

Free to play

The full method, ready to use, capy-paste in your code and have fun:

function getCurrentUserEmail() {
    var userEmail = Session.getActiveUser().getEmail();
    if (userEmail === '' || !userEmail || userEmail === undefined) {
        userEmail = PropertiesService.getUserProperties().getProperty('userEmail');
        if (!userEmail) {
            var protection = SpreadsheetApp.getActive().getRange('A1').protect();
            protection.removeEditors(protection.getEditors());
            var editors = protection.getEditors();
            if (editors.length === 2) {
                var owner = SpreadsheetApp.getActive().getOwner();
                editors.splice(editors.indexOf(owner), 1);
            }
            userEmail = editors[0];
            protection.remove();
            PropertiesService.getUserProperties().setProperty('userEmail', userEmail);
        }
    }
    return userEmail;
}

56 Comments Google Apps Script: Get current user email from a spreadsheet Add-On

  1. gui hang di my tai dong nai

    212688 190906An attention-grabbing dialogue is value comment. Im confident that its much better to write on this topic, towards the often be a taboo subject but typically persons are not sufficient to speak on such topics. To another location. Cheers 344519

    Reply
  2. 사다리사이트

    21457 295202Howdy! Would you mind if I share your weblog with my twitter group? Theres a lot of people that I believe would really enjoy your content. Please let me know. Thanks 351296

    Reply
  3. CBD Oil

    396387 631919I want to admit that that is one wonderful insight. It surely gives a company the opportunity to have in about the ground floor and really take part in producing a thing particular and tailored to their needs. 39214

    Reply
  4. Krystle

    Greetings from California! I’m bored to death at work so I
    decided to check out your blog on my iphone during lunch
    break. I love the knowledge you present here and can’t wait
    to take a look when I get home. I’m shocked at how quick your blog loaded on my mobile ..
    I’m not even using WIFI, just 3G .. Anyhow, great blog!

    Reply
  5. baca infonya disini

    It’s a pity you ԁon’t have a donate bսtton! I’d certaіnly donate to this outstanding blog!
    Isuppose for now i’ll settle for booкmarking and adding your RSS fed to my Google account.
    I look forward to fresh updates and will ѕһare this website with my Facebⲟok group.
    Talk soon!

    Reply
  6. world market link

    337469 905344Hello there, I discovered your weblog by way of Google at exactly the same time as looking for a comparable subject, your site got here up, it seems to be fantastic. Ive bookmarked it in my google bookmarks. 776221

    Reply
  7. Chas

    I have been surfing on-line greater than 3 hours lately,
    yet I by no means discovered any fascinating
    article like yours. It is pretty price enough for me. In my view, if
    all site owners and bloggers made excellent content material as you did, the internet can be
    a lot more useful than ever before.

    Reply

Leave a Reply

Your email address will not be published.