在域中的每个用户开始我们为休假权利运行一个简单的脚本,但我们要欢迎消息是“嗨名”,然而剧本似乎并没有能够为标准取从getActiveUser getGivenName()()用户。
有没有办法?
在域中的每个用户开始我们为休假权利运行一个简单的脚本,但我们要欢迎消息是“嗨名”,然而剧本似乎并没有能够为标准取从getActiveUser getGivenName()()用户。
有没有办法?
正如在评论中指出,在文档中,服务的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;
}
}
在Google Apps脚本,我是能够得到使用关于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"];
你可以得到一个用户名,但首先你要创建一个使用配置API的域用户。 您可以通过登录到您的管理员帐户启用API,并选择域设置和用户设置选项卡,选中该复选框启用配置API。 了解更多关于它在这里
然后,您可以使用
user = user.getgivenName()
由于的UserManager服务只提供给一个域管理员,你可以发布服务作为管理员,用作用户的教名,并调用从使用用户运行脚本UrlFetchApp
。
请参阅内容服务文档因为这是基于背景信息。
该服务接受的参数, userName
,它用来执行查找作为管理员 。
下面的代码粘贴到一个脚本,然后部署脚本作为Web服务。 这必须由域管理员来完成,为服务访问的UserManager服务,但该脚本必须由域中的所有用户都可以访问。 (因为我不是在我的域管理员,我不能访问的UserManager,所以我已经包括用于测试的域用户可调用线,调用getOwnName()
我在描述函数的第一个答案 。)
记住要调用doGet()
从调试器访问发布服务之前要经过授权。
/**
* 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);
}
请参阅使用外部的API对于如何利用编写的一节中的服务的说明。 我将展示如何从另一个脚本访问该服务,但要记住,你也可以从你的域内的网页做到这一点。
我们将使用UrlFetchApp.fetch()
来获得我们的服务来回报用户的名字作为一个字符串。
该服务被写接受一个参数, userName
,和我们这个附加到URL,在形式userName=<string>
。
通过URL建立,我们fetch()
然后检索响应名称。 虽然这个例子只返回名称,你可以选择改变,以返回完整的“Hello用户”串服务。
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
}