使用Google Apps脚本通过Admin Directory API按部门划分用户 [英] Get users by Department from Admin Directory API using Google Apps Script
问题描述
我有以下(主要是)使用Admin Directory API的工作脚本.但是,除了提取整个域之外,我还想提取特定部门的信息.
I have the following (mostly) working script using the Admin Directory API. However, rather than pulling the entire domain - I would like to just pull the information for a specific department.
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 [].domain过滤到域,但收到错误消息.我已将文件 user.organizations
更改为 user.organizations [].department
,如
I have tried to filter to the domain by using user.organization[].domain but just got error messages. I changed the parameter user.organizations
to user.organizations[].department
as documented in the Admin SDK reference.
这最初抛出了SyntaxError,当更改为 user.organizations [0] .department
时,它抛出了错误消息:
This initially threw out a SyntaxError, and when changed to user.organizations[0].department
it threw out the error message:
TypeError:无法从未定义中读取属性"0"
TypeError: Cannot read property "0" from undefined
我完全省略了括号,并使用了 user.organizations.department
,但是得到了:
I omitted the brackets altogether and used user.organizations.department
, but got:
TypeError:无法从未定义中读取属性部门"
TypeError: Cannot read property "department" from undefined
此外,如果可能的话,我想分开列出部门和职务.目前,它以以下格式导出信息:
Also, if possible I would like to list the Department and Title separably. Currently, it exports the information in this format:
{customType = work,name =,location = 002,title = Technical Support Manager,department = Technical Support,primary = true}
当前显示:
我想要的输出格式:
My desired output format:
推荐答案
您会遇到这些错误,因为您域中的某些用户不具有部门/职务"用户属性.
You are getting those errors because some of the users in your domain don't have the department/ title user property.
添加2个变量和 try .. catch
块应可使代码按预期工作.
Adding 2 variable and try.. catch
blocks should allow the code to work as you expect.
这是更新的代码:
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);
}
更新
仅列出特定部门的用户,您只需在 push
To only list the users in a certain department you would just need to add a if
around the push
if(department == '--NAME OF DEPARTMENT--'){
listArray.push([user.name.fullName, user.name.givenName, user.name.familyName, user.primaryEmail, department, title, user.id,]);
}
这篇关于使用Google Apps脚本通过Admin Directory API按部门划分用户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!