Google Script How do I getGivenName() of getActiveUser()

前端 未结 5 2144
刺人心
刺人心 2021-01-03 02:42

Each user on the domain initiates a simple script we run for leave entitlements but we want the welcome message to be \"Hi First Name,\" however the script doesn\'t seem to

5条回答
  •  长情又很酷
    2021-01-03 03:03

    As noted in comments, and in Documentation, the UserManager Service is only accessible by Domain Administrators.

    Here's an alternative. Domain Users may have themselves in their own contacts, so how about a best-effort attempt at finding themselves there?

    /**
     * Get current user's name, by accessing their contacts.
     *
     * @returns {String} First name (GivenName) if available,
     *                   else FullName, or login ID (userName)
     *                   if record not found in contacts.
     */
    function getOwnName(){
      var email = Session.getEffectiveUser().getEmail();
      var self = ContactsApp.getContact(email);
    
      // If user has themselves in their contacts, return their name
      if (self) {
        // Prefer given name, if that's available
        var name = self.getGivenName();
        // But we will settle for the full name
        if (!name) name = self.getFullName();
        return name;
      }
      // If they don't have themselves in Contacts, return the bald userName.
      else {
        var userName = Session.getEffectiveUser().getUsername();
        return userName;
      }
    }
    

提交回复
热议问题