-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuserAliasesList.gs
More file actions
66 lines (61 loc) · 2.76 KB
/
userAliasesList.gs
File metadata and controls
66 lines (61 loc) · 2.76 KB
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
/*
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------
This script was modified from the one found at https://xfanatical.com/blog/list-users-with-email-aliases-in-google-workspace/ so instead of showing the results
in the console log section they are inserted into a Google Sheet for better management. Including their license as required for usage.
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------
* Usage:
* For this to be functional the 'Admin API' will have to be added from the 'Service' section as 'AdminDirectory'
* 1. copy and paste this source code to your Apps Script Editor
* 2. select the following function name
* 3. click 'Run'.
* 4. The users with email aliases will be printed in the 'Execution log'
*
* © 2021 xFanatical, Inc.
* @license MIT
*/
//This will add a menu/submenu to the Google Sheets files for the script to be executed easily
function onOpen(e) {
let subMenu = [{ name: "Get List", functionName: "listUsersWithEmailAliases" }];
SpreadsheetApp.getActiveSpreadsheet().addMenu("Get Email Aliases", subMenu);
}
function listUsersWithEmailAliases() {
//Sheets Prep
let userCol = 1, primaryCol = 2, aliasCol = 3;
let pageToken, page;
let activeRow = 2;
let sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
sheet.clear();
sheet.getRange(1, userCol).setValue("Name");
sheet.getRange(1, primaryCol).setValue("Primary Email");
sheet.getRange(1, aliasCol).setValue("Email Alias");
//Format column headers
sheet.getRange("A1:C1").setHorizontalAlignment("CENTER");
sheet.getRange("A1:C1").setBackground("#000000");
sheet.getRange("A1:C1").setFontColor("#FFFFFF")
sheet.getRange("A1:C1").setFontWeight("BOLD");
//User list extraction
do {
page = AdminDirectory.Users.list({
customer: 'my_customer',
maxResults: 500, //This value can be from 0 to 500 max
pageToken,
fields: 'users(name/fullName,primaryEmail,aliases),nextPageToken',
})
let users = page.users
if (users) {
for (let i = 0; i < users.length; i++) {
const user = users[i]
if (user.aliases && user.aliases.length > 0) {
//Inserting data into Google Sheets
sheet.getRange(activeRow, userCol).setValue(user.name.fullName);
sheet.getRange(activeRow, primaryCol).setValue(user.primaryEmail);
sheet.getRange(activeRow, aliasCol).setValue(JSON.stringify(user.aliases));
activeRow++
}
}
} else {
sheet.getRange(activeRow, userCol).setValue('Users not found.');
}
pageToken = page.nextPageToken
} while (pageToken)
}