Search code examples
google-apps-scriptgoogle-apps

How can I get a user's name from their email?


I'm trying to get a user's name from their email and we are all on the same business domain. I saw a post here detailing how to do it if you want to pull from the contacts list. The problem is when I try it myself, it knows that there is contact for me, but it returns all of the values as null. If I use another contact email, then it pulls the info just fine.

The link says that there should be another way to do it but you need admin privileges. I can get that, but all of the links to the usermanager documentation are broken. Also, searching usermanager doesn't come up with anything on Google's developer site.

var email = Session.getActiveUser().getEmail();
Browser.msgBox(email)

var self = ContactsApp.getContact(email);
var name = self.getFullName();

Solution

  • With admin rights you can use the directory api. Do not forget to enhable the admin api in your appscript (Ressources > Advanced Google services) and also in the Developers Console.

    function getUserName(email){
    var result = AdminDirectory.Users.get(email, {fields:'name'});
    var fullname = result.name.fullName;
    Logger.log(fullname);
    return fullname;
    }