Ingeborg
Ingeborg

Reputation: 379

Getting only value of custom fields in Google Suite User list

I have made Custom Fields in my Users in Google Suite. Category: Foresatt Among them: Name: 'foresatt epost', type:email, number: multiple values

I would like to list these values using Google Script. I used this: https://developers.google.com/admin-sdk/directory/v1/quickstart/apps-script

To write this code:

function listUsers() {
  var optionalArgs = {
    customer: 'my_customer',
    maxResults: 10,
    orderBy: 'email',
    projection: 'custom',
    customFieldMask:'Foresatt' 
  };
  var response = AdminDirectory.Users.list(optionalArgs);
  var users = response.users;
  if (users && users.length > 0) {
    Logger.log('Users:');
    for (i = 0; i < users.length; i++) {
      var user = users[i];
      var foresatt = user.customSchemas;
      Logger.log('%s (%s)', user.primaryEmail, user.name.fullName, foresatt);
    }
  } else {
    Logger.log('No users found.');
  }
}

That works, but I would like to get only the values. What I get now:

{Foresatt={
  foresatt_mob=[{value=X#X#X#X#, type=work}, {type=work, value=X#X#X#X#}, {type=work, value=X#X#X#X#}], 
  foresatt_epost=[{[email protected], type=work}, {type=work, [email protected]}, {[email protected], type=work}], 
  foresatt_navn=[{type=work, value=Xx}, {value=Xy, type=work}, {type=work, value=Yy}]
  }
}

What I would like to get: [email protected], [email protected], [email protected]

I have tried several things, but I'm afraid I'm not experienced enough.

var epost = foresatt.foresatt_epost;

Results in: TypeError: Cannot read property 'foresatt_epost'

var epost = foresatt('foresatt_epost');

Results in: TypeError: foresatt is not a function

Please advise me, how do I get only the values fram the field 'foresatt epost'?

Upvotes: 1

Views: 354

Answers (1)

Tanaike
Tanaike

Reputation: 201503

I believe your goal as follows.

const object = {
  Foresatt: {
    foresatt_mob: [
      { value: "X#X#X#X#",type: "work"}, 
      { value: "X#X#X#X#",type: "work"},
      { value: "X#X#X#X#",type: "work"},
    ],
    foresatt_epost: [
      { value: "[email protected]", type: "work"},
      { value: "[email protected]", type: "work"},
      { value: "[email protected]", type: "work"},
    ],
    foresatt_navn: [
      { type: "work", value: "Xx"},
      { type: "work", value: "Xy"},
      { type: "work", value: "Yy"},
    ]
  }
}

In this case, the values can be retrieved from the object.Foresatt.foresatt_epost array.

Sample script:

const object = {}; //Your object

const res = object.Foresatt.foresatt_epost.map(e => e.value);
console.log(res)  // Outputs: [ '[email protected]', '[email protected]', '[email protected]' ]
  • If user.customSchemas is the above object, the script is as follows.
var foresatt = user.customSchemas;
const res = foresatt.Foresatt.foresatt_epost.map(e => e.value);
console.log(res)
  • If you want to retrieve the value as a comma separated string, you can use res.join(",").

References:

Note: If there is no guarantee your property will exist in your object, you can do (object.property||[]).map(...) instead of object.property.map(...) to avoid the error Uncaught TypeError: Cannot read property 'forEach' of undefined.

Upvotes: 2

Related Questions