-
Notifications
You must be signed in to change notification settings - Fork 2
/
getUsersList.gs
132 lines (115 loc) · 4.22 KB
/
getUsersList.gs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
function getUsersList() {
const users = [];
const userEmail = Session.getActiveUser().getEmail();
const domain = userEmail.split("@").pop();
const options = {
domain: domain,
customer: "my_customer",
maxResults: 100,
projection: "FULL",
viewType: "admin_view",
orderBy: "email",
};
const spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
const sheets = spreadsheet.getSheets();
const lastSheetIndex = sheets.length;
// Check if "Users" sheet exists, delete it if it does
let usersSheet = spreadsheet.getSheetByName("Users");
if (usersSheet) {
spreadsheet.deleteSheet(usersSheet);
}
// Create a new sheet at the last index
usersSheet = spreadsheet.insertSheet("Users", lastSheetIndex);
var headers = ["Name", "Email", "Super Admin", "Delegated Admin", "Suspended",
"Archived", "Last Login Time", "Enrolled in 2SV", "Enforced in 2SV",
"Org Path"];
var headerRange = usersSheet.getRange("A1:J1");
headerRange.setValues([headers]);
headerRange.setFontColor("#ffffff");
headerRange.setFontSize(10);
headerRange.setFontFamily("Montserrat");
headerRange.setBackground("#fc3165");
headerRange.setFontWeight("bold");
// Delete cells K to Z
usersSheet.deleteColumns(11, 16); // K to Z
let response;
do {
response = AdminDirectory.Users.list(options); //Types of info pulled from API https://developers.google.com/admin-sdk/directory/reference/rest/v1/users
users.push(
...response.users.map((user) => [
user.name.fullName,
user.primaryEmail,
user.isAdmin,
user.isDelegatedAdmin,
user.suspended,
user.archived,
user.lastLoginTime,
user.isEnrolledIn2Sv,
user.isEnforcedIn2Sv,
user.orgUnitPath,
]),
);
// For domains with many users, the results are paged
if (response.nextPageToken) {
options.pageToken = response.nextPageToken;
}
} while (response.nextPageToken);
// Insert data in a spreadsheet
usersSheet.setFrozenRows(1); // Sets headers in sheet and freezes row
usersSheet.getRange(2, 1, users.length, users[0].length).setValues(users); // Adds in user info starting from row 1
// Apply conditional formatting
var rangeC = usersSheet.getRange("C2:C1000");
var ruleC = SpreadsheetApp.newConditionalFormatRule()
.whenTextContains("TRUE")
.setBackground("#ffb6c1")
.setRanges([rangeC])
.build();
var ruleCFalse = SpreadsheetApp.newConditionalFormatRule()
.whenTextContains("FALSE")
.setBackground("#b7e1cd")
.setRanges([rangeC])
.build();
var rangeH = usersSheet.getRange("H2:H1000");
var ruleH = SpreadsheetApp.newConditionalFormatRule()
.whenTextContains("FALSE")
.setBackground("#ffb6c1")
.setRanges([rangeH])
.build();
var ruleHFalse = SpreadsheetApp.newConditionalFormatRule()
.whenTextContains("TRUE")
.setBackground("#b7e1cd")
.setRanges([rangeH])
.build();
var rangeI = usersSheet.getRange("I2:I1000");
var ruleI = SpreadsheetApp.newConditionalFormatRule()
.whenTextContains("FALSE")
.setBackground("#ffb6c1")
.setRanges([rangeI])
.build();
var ruleIFalse = SpreadsheetApp.newConditionalFormatRule()
.whenTextContains("TRUE")
.setBackground("#b7e1cd")
.setRanges([rangeI])
.build();
var rangeD = usersSheet.getRange("D2:D1000");
var ruleD = SpreadsheetApp.newConditionalFormatRule()
.whenTextContains("TRUE")
.setBackground("#ffb6c1")
.setRanges([rangeD])
.build();
var ruleDFalse = SpreadsheetApp.newConditionalFormatRule()
.whenTextContains("FALSE")
.setBackground("#b7e1cd")
.setRanges([rangeD])
.build();
var rules = [ruleC, ruleCFalse, ruleH, ruleHFalse, ruleI, ruleIFalse, ruleD, ruleDFalse];
usersSheet.setConditionalFormatRules(rules);
// --- Add Filter View ---
const lastRow = usersSheet.getLastRow();
const filterRange = usersSheet.getRange('C1:J' + lastRow); // Filter columns C through J
filterRange.createFilter();
// Create named range for columns B, C, D, and E
const namedRange = spreadsheet.setNamedRange('UserStatus', usersSheet.getRange('B2:E' + lastRow));
// Auto resize all columns
usersSheet.autoResizeColumns(1, usersSheet.getLastColumn());
}