gpt4 book ai didi

google-apps-script - 使用 Google Apps 脚本从 Admin Directory API 按部门获取用户

转载 作者:行者123 更新时间:2023-12-04 16:03:45 25 4
gpt4 key购买 nike

我有以下(大部分)使用管理目录 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[].domain 过滤域,但只收到错误消息。我将参数 user.organizations 更改为 user.organizations[].department,如记录 in the Admin SDK reference .

这最初抛出一个 SyntaxError,当更改为 user.organizations[0].department 时抛出错误消息:

TypeError: Cannot read property "0" from undefined

我完全省略了括号并使用了 user.organizations.department,但是得到了:

TypeError: Cannot read property "department" from undefined

此外,如果可能的话,我想分开列出部门和职位。目前,它以这种格式导出信息:

{customType=work, name=, location=002, title=Technical Support Manager, department=Technical Support, primary=true}

当前显示:
Here is a screenshot of how the information is displayed in a Google Sheet

我想要的输出格式:
Here is a screenshot of how I would like it to appear

最佳答案

您收到这些错误是因为您域中的某些用户没有 department/title 用户属性。

添加 2 个变量和 try..catch block 应该允许代码按预期工作。

这是更新后的代码:

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

周围添加一个 if
 if(department == '--NAME OF DEPARTMENT--'){
listArray.push([user.name.fullName, user.name.givenName, user.name.familyName, user.primaryEmail, department, title, user.id,]);
}

关于google-apps-script - 使用 Google Apps 脚本从 Admin Directory API 按部门获取用户,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49691457/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com