Google脚本如何获取getActiveUser()的getGivenName()

use*_*953 4 google-apps-script

域中的每个用户都会启动一个简单的脚本来运行休假权利,但我们希望欢迎消息为“ Hi First Name”,但是该脚本似乎无法从标准的getActiveUser()获取getGivenName()。用户。

有办法吗?

Mog*_*dad 6

如注释中和文档中所述,只有域管理员才能访问UserManager服务。

这是另一种选择。域用户可能拥有自己的联系人,那么如何尽最大努力在该联系人中找到自己?

/**
 * 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;
  }
}
Run Code Online (Sandbox Code Playgroud)

  • 事实证明,ContactsApp 速度慢得离谱。除非必要,否则不要使用这个。 (2认同)

小智 6

在 Apps 脚本中,我可以使用 About REST API 获取此信息: https: //developers.google.com/drive/v2/reference/about/get

var aboutData = DriveApp.About.get();
var userEmail = aboutData["user"]["emailAddress"];
var userDisplayName = aboutData["user"]["displayName"];
Run Code Online (Sandbox Code Playgroud)


Rac*_*len 2

您可以获取用户名,但首先必须使用配置 api 创建域用户。您可以通过登录管理员帐户来启用 API,然后选择域设置和用户设置选项卡以选中启用配置 API 的复选框。在这里阅读更多相关信息

然后您可以使用

   user = user.getgivenName()
Run Code Online (Sandbox Code Playgroud)