我使用Admin Directory API有以下(大部分)工作脚本。但是,我不想拉动整个域名 - 我只想提取特定部门的信息。
function listAllUsers() {
var ss = SpreadsheetApp.getActive();
var pageToken, page, count = 0;
var listArray = [];
listArray.push(['full name', 'first name', 'last name', 'email', 'department', 'ID'])
do {
page = AdminDirectory.Users.list({
domain: 'example.co.uk',
orderBy: 'givenName',
maxResults: 100,
pageToken: pageToken
});
var users = page.users;
if (users) {
for (var i = 0; i < users.length; i++) {
var user = users[i];
listArray.push([user.name.fullName, user.name.givenName, user.name.familyName, user.primaryEmail, user.organizations, user.id,]);
}
}
pageToken = page.nextPageToken;
break; // This means you only get one page
} while (pageToken);
try {
var outputSheet = ss.getSheetByName('allMembers');
outputSheet.getDataRange();
} catch(err) {
var outputSheet = ss.insertSheet('allMembers', 2);
}
outputSheet.getDataRange().clear();
outputSheet.getRange(1, 1, listArray.length, listArray[0].length).setValues(listArray);
outputSheet.getRange(1, 6, outputSheet.getLastRow(), 4).setHorizontalAlignment("center");
outputSheet.getRange(1, 1, outputSheet.getLastRow(), 1).setHorizontalAlignment("center");
var width = [150, 150, 180, 250, 250, 200];
formatSheet(outputSheet, width);
}
我尝试使用user.organization []。域过滤到域,但只是收到了错误消息。我将参数user.organizations
更改为user.organizations[].department
,如文档in the Admin SDK reference所示。
这最初抛出了一个SyntaxError,当更改为user.organizations[0].department
时,它抛出了错误消息:
TypeError:无法从未定义的
读取属性“0”
我完全省略了括号并使用了user.organizations.department
,但得到了:
TypeError:无法从undefined
读取属性“department”
另外,如果可能的话,我想分别列出部门和标题。目前,它以这种格式导出信息:
{customType=work, name=, location=002, title=Technical Support Manager, department=Technical Support, primary=true}
当前显示:
我想要的输出格式:
答案 0 :(得分:1)
您收到这些错误是因为您域中的某些用户没有部门/标题用户属性。
添加2个变量和try.. catch
块应该允许代码按预期工作。
以下是更新后的代码:
function listAllUsers() {
var ss = SpreadsheetApp.getActive();
var pageToken,
page,
count = 0;
var listArray = [];
listArray.push(['full name', 'first name', 'last name', 'email', 'department', 'title', 'ID'])
do {
page = AdminDirectory.Users.list({
domain : 'example.com',
orderBy : 'givenName',
pageToken : pageToken
});
var users = page.users;
if (users) {
for (var i = 0; i < users.length; i++) {
var user = users[i];
var department,
title; // Addded two new variables
try { // Try to get the users department if there is an error push the error to the array
department = user.organizations[0].department;
} catch (e) {
department = e
}
try {// Try to get the users title if there is an error push the error to the array
title = user.organizations[0].title;
} catch (e) {
title = e
}
listArray.push([user.name.fullName, user.name.givenName, user.name.familyName, user.primaryEmail, department, title, user.id, ]);
}
}
pageToken = page.nextPageToken;
break; // This means you only get one page
} while (pageToken);
try {
var outputSheet = ss.getSheetByName('allMembers');
outputSheet.getDataRange();
} catch (err) {
var outputSheet = ss.insertSheet('allMembers', 2);
}
outputSheet.getDataRange().clear();
outputSheet.getRange(1, 1, listArray.length, listArray[0].length).setValues(listArray);
outputSheet.getRange(1, 6, outputSheet.getLastRow(), 4).setHorizontalAlignment("center");
outputSheet.getRange(1, 1, outputSheet.getLastRow(), 1).setHorizontalAlignment("center");
var width = [150, 150, 180, 250, 250, 200];
formatSheet(outputSheet, width);
}
<强>更新强>
要仅列出某个部门的用户,您只需在if
附近添加push
if(department == '--NAME OF DEPARTMENT--'){
listArray.push([user.name.fullName, user.name.givenName, user.name.familyName, user.primaryEmail, department, title, user.id,]);
}
答案 1 :(得分:0)
使用管理员SDK和查询,您可以根据OrgName,ManagerId,Ref Link search user等属性enter link description here进行查询。 在您的请求中传递查询
var queryStr="orgDepartment=HR"
var page = AdminDirectory.Users.list({
domain : 'example.com',
orderBy : 'givenName',
pageToken : pageToken,
query:queryStr
});