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);
}
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.
This initially threw out a SyntaxError, and when changed to user.organizations[0].department
it threw out the error message:
TypeError: Cannot read property "0" from undefined
I omitted the brackets altogether and used user.organizations.department
, but got:
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}
The current display:
My desired output format: