-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgetOrgUnits.gs
136 lines (109 loc) · 4.65 KB
/
getOrgUnits.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
133
134
135
136
function getOrgUnits() {
const spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
const sheets = spreadsheet.getSheets();
const lastSheetIndex = sheets.length;
let orgUnitsSheet = spreadsheet.getSheetByName("Org Units");
// Check if the sheet exists, delete it if it does
if (orgUnitsSheet) {
spreadsheet.deleteSheet(orgUnitsSheet);
}
// Create a new 'Org Units' sheet at the last index
orgUnitsSheet = spreadsheet.insertSheet("Org Units", lastSheetIndex);
// Add headers to the sheet
const headers = [
"Org Name ID",
"Org Unit Name",
"OrgUnit Path",
"Description",
"Parent Org Unit ID",
"Parent Org Unit Path",
];
orgUnitsSheet.appendRow(headers);
// Format the headers
const headerRange = orgUnitsSheet.getRange("A1:F1");
headerRange.setFontWeight("bold").setFontColor("#ffffff").setFontFamily("Montserrat");
headerRange.setBackground("#fc3165");
// Fetch org units, explicitly including the root organizational unit
const orgUnitsResponse = AdminDirectory.Orgunits.list("my_customer", {
type: "ALL",
customer: 'my_customer'
});
let orgUnits = [];
if (orgUnitsResponse && orgUnitsResponse.organizationUnits) {
orgUnits = orgUnitsResponse.organizationUnits;
// Sort the orgUnits array based on the orgUnitPath, then by name (case-insensitive) if paths are equal
orgUnits.sort((a, b) => {
// Split the paths into components
const pathA = a.orgUnitPath.split("/");
const pathB = b.orgUnitPath.split("/");
// Compare paths component by component
for (let i = 0; i < Math.min(pathA.length, pathB.length); i++) {
if (pathA[i] < pathB[i]) return -1;
if (pathA[i] > pathB[i]) return 1;
}
// If all components match, sort by name (case-insensitive)
if (pathA.length === pathB.length) {
return a.name.toLowerCase().localeCompare(b.name.toLowerCase());
}
// If all components match and names are equal, shorter path comes first (unlikely)
return pathA.length - pathB.length;
});
}
// If no OUs were fetched (meaning only the root OU exists),
// extract its details from the orgUnitsResponse itself or fetch it directly
if (orgUnits.length === 0) {
let rootOrgUnit = {
orgUnitId: orgUnitsResponse.orgUnitId || "",
name: orgUnitsResponse.name || "",
orgUnitPath: orgUnitsResponse.orgUnitPath || "/",
description: orgUnitsResponse.description || "",
parentOrgUnitId: "",
parentOrgUnitPath: "",
};
// If name is still empty, fetch the domain info and use the organization name
if (rootOrgUnit.name === "") {
const domainInfo = AdminDirectory.Customers.get('my_customer');
rootOrgUnit.name = domainInfo.postalAddress.organizationName || "";
}
// Set description to "Root level OU with no sub-OUs" if there are no other OUs
rootOrgUnit.description = "Root level OU with no sub-OUs";
console.log("Fetched Root OU (from response or direct fetch):", rootOrgUnit);
orgUnits.push(rootOrgUnit);
}
// Prepare data for the sheet (including headers)
const fileArray = [];
orgUnits.forEach((orgUnit) => {
fileArray.push([
orgUnit.orgUnitId ? orgUnit.orgUnitId.slice(3) : "",
orgUnit.name,
orgUnit.orgUnitPath,
orgUnit.description,
orgUnit.parentOrgUnitId ? orgUnit.parentOrgUnitId.replace(/^id:/, "") : "",
orgUnit.parentOrgUnitPath,
]);
});
// Write data back to the sheet, including headers only if orgUnits has data.
// If no data, only headers row will be present
if (fileArray.length > 0) {
orgUnitsSheet.getRange(2, 1, fileArray.length, fileArray[0].length).setValues(fileArray);
}
// Get the last row of the sheet. If it's just the header, then it's 1, otherwise get the last row.
const lastRow = orgUnitsSheet.getLastRow();
// Delete columns G-Z
orgUnitsSheet.deleteColumns(7, 20);
// Auto-resize all columns - only if there's more than just the header.
if (lastRow > 1) {
orgUnitsSheet.autoResizeColumns(1, orgUnitsSheet.getLastColumn());
}
// Define ranges
// Only set named ranges and filter if there's data in the sheet (orgUnits is not empty)
if (orgUnits.length > 0) {
if (lastRow > 1) { //Set NamedRanges only if we have more than just the header row
spreadsheet.setNamedRange('Org2ParentPath', orgUnitsSheet.getRange('E:F')); // Updated range for Org2ParentPath
spreadsheet.setNamedRange('OrgID2Path', orgUnitsSheet.getRange('A:C'));
}
// --- Add Filter View ---
const filterRange = orgUnitsSheet.getRange('A1:F' + lastRow); // Adjust filter range if needed
filterRange.createFilter();
}
}