Reputation: 85
I want to get some user data of our Google Apps for Work users to a Google Spreadsheet via the Directory API. I use the following Google Apps Script and it works so far:
function writeToSpreadsheet(){
var values = [];
var optionalArgs = {
customer: 'my_customer',
maxResults: 500,
orderBy: 'email',
viewType: 'domain_public'
};
var users = AdminDirectory.Users.list(optionalArgs).users; //example: ignitesynergy.com
for (var i=0; i<users.length; i++){
values.push([users[i].id, users[i].name.familyName, users[i].name.givenName, users[i].primaryEmail, users[i].phones ]); //Look in the docs or use auto complete to see what you can access
}
var spreadsheetUrl = 'https://docs.google.com/a/liganova.com/spreadsheets/d/1HwSMiBjW9lAw55GJaTdW_LDUjkLiP473ntuV2DpIeqQ/';
SpreadsheetApp.openByUrl(spreadsheetUrl).getSheets()[0].getRange(2, 1, values.length, values[0].length).setValues(values);
}
But: The values in the column for the phone number looks like this:
{type=work, value=+123456789}
I only want to have the value in the column. When I try
users.phones[0].primary
I get an TypeError.
Any ideas anyone?
Upvotes: 0
Views: 1202
Reputation: 11
Cameron's answer great! However, the "type" should be set to a different value, something like 'mobile' to display the numeric phone only.
https://developers.google.com/admin-sdk/directory/v1/reference/users/update
for (var i=0; i<users.length; i++){
var phone = '';
for(var j in users[i].phones){
if(users[i].phones[j].type == 'mobile')
phone = users[i].phones[j].value;
}
values.push([users[i].name.familyName, users[i].name.givenName, users[i].primaryEmail, phone ]);
}
Upvotes: 0
Reputation: 7367
Users.list is returning an array of phone number Objects, rather than just a plain number. I can tell this by seeing the {type=.., value=..} notation you are getting, the {} indicates it is a javascript Object, and I see properties named type and value.
You'll need to loop over the phone number objects returned and identify the primary number with an If statement.
try something like this:
for (var i=0; i<users.length; i++){
var phone = '';
for(var j in users[i].phones){
if(users[i].phones[j].type == 'primary')
phone = users[i].phones[j].value;
}
values.push([users[i].id, users[i].name.familyName, users[i].name.givenName, users[i].primaryEmail, phone ]); //Look in the docs or use auto complete to see what you can access
}
Upvotes: 1