Skip to content
Advertisement

Nothing being Outputted after first Function Call – Javascript

I’m working on a javascript project that will list everything from user name, email, phone number, and notes from AD into into a spreadsheet, so far i have two functions. however anything i put after getExternalID(users[i].externalIds ||[] does not output any information. here’s a snippet of what i have. I’m a bit of a novice, would this be due to how I formatted the script?

function writeToSpreadsheet(){
    var values = [];
    var users = AdminDirectory.Users.list({domain:'domain'}).users; 
    for (var i=0; i<users.length; i++){
      values.push([users[i].name.fullName, getExternalID(users[i].externalIds ||[], getPhones(users[i].phones ||[], ))]); // accounts for blank data 
    }
    
    var spreadsheetUrl = 'https://docslink';
    SpreadsheetApp.openByUrl(spreadsheetUrl).getSheets()[0].getRange(1, 1, values.length, values[0].length).setValues(values);
}


function getExternalID(arr) {
    return arr.map(function(ExternalIDsObj) {
        return ExternalIDsObj.value;
    }).join(', ') + ('@differentemail')  //takes employeeID and adds Email Address
    
}


function getPhones(arr) {
    return arr.map(function(phoneObj) {
        return phoneObj.value;
    }).join(', ')
}





                     

Advertisement

Answer

Since you are aiming to output the data from Active Directory into a spreadsheet, you need to use Sheets API to do that after populating the values array.

Also, for best practice the two get functions are separated from the main function.

Edit: There must be a typo on your closing parenthesis on your getExternalID() call. It’s only supposed to get one argument.

Sample Code:

function writeToSpreadsheet(){
  var values = [];
  var users = AdminDirectory.Users.list({domain:'domain'}).users; 
  for (var i=0; i<users.length; i++){
    values.push([users[i].name.fullName, getExternalID(users[i].externalIds || []), getPhones(users[i].phones || [])]);
  }

  var sheet = SpreadsheetApp.getActiveSheet();
  sheet.getRange(1,1,values.length,values[0].length).setValues(values);

}  
    
function getExternalID(arr) {
  return arr.map(function(ExternalIDsObj) {
    return ExternalIDsObj.value;
  }).join(', ') + ('@differentemail.com');
}
        
function getPhones(arr) {
  return arr.map(function(phoneObj) {
    return phoneObj.value;
  }).join(', ');
}   

Sample Output: (note that these “accounts” do not have external ID information in Admin Console)

enter image description here

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement