Google Script How do I getGivenName() of getActive

2019-02-25 12:23发布

问题:

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 be able to fetch getGivenName() from getActiveUser() for a standard user.

Is there a way?

回答1:

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;
  }
}


回答2:

You can get a user name but first you have to create a domain user using the provisioning api. You can enable the API by logging in to your admin account, and select Domain settings and the User settings tab to select the checkbox enabling the Provisioning API. Read more about it here

You can then use

   user = user.getgivenName()


回答3:

Since the UserManager Service is only available to a Domain Administrator, you could publish a service as the administrator, that serves user's Given Names, and invoke that from the user-run script using the UrlFetchApp.

The UserName Service

Refer to the Content Service Documentation for the background information this is based upon.

The service accepts a parameter, userName, which it uses to perform a lookup as the administrator.

Paste the following code into a script, then deploy the script as a web service. This must be done by a Domain Administrator, as the service access the UserManager Service, but the script must be made accessible by all users in the domain. (Since I'm not an admin in my domain, I cannot access the UserManager, so I've included a domain-user-invokable line for testing, calling the getOwnName() function I described in my first answer.)

Remember to invoke doGet() from the debugger to go through the authorization before accessing the published service.

/**
 * When invoked as a Web Service running as Domain Administrator, 
 * returns the GivenName of the requested user.
 * 
 * @param {String} userName= Should be set to Session.getEffectiveUser().getUsername().
 */
function doGet(request) {
  //return ContentService.createTextOutput(getOwnName()); // for testing by non-admin user

  var userName = request.parameters.userName;
  var givenName = UserManager.getUser(userName).getGivenName();

  return ContentService.createTextOutput(givenName);
}

Invoke service using UrlFetch

Refer to Using External APIs for an explanation of how to make use of the service written in the previous section. I'll show how to access the service from another script, but remember that you can also do this from web pages within your domain.

We will use UrlFetchApp.fetch() to get our service to return the user's first name as a String.

The service was written to accept one parameter, userName, and we append this to the url, in the form userName=<string>.

With the URL built, we fetch(), then retrieve the name from the response. While this example returns just the name, you may choose to change the service to return the complete "Hello User" string.

function testService() {
  var domain = "my-google-domain.com";
  var scriptId = "Script ID of service";
  var url = "https://script.google.com/a/macros/"+domain+"/s/"+scriptId+"/exec?"
     + "userName="+Session.getEffectiveUser().getUsername();
  var response = UrlFetchApp.fetch(url);
  var myName = response.getContentText();
  debugger;  // pause in debugger
}


回答4:

In Apps Script, I was able to get this information using the About REST API: https://developers.google.com/drive/v2/reference/about/get

var aboutData = Drive.About.get();
var userEmail = aboutData["user"]["emailAddress"];
var userDisplayName = aboutData["user"]["displayName"];