在Google Suite用户列表中只获取自定义字段的值。

问题描述 投票:0回答:1

我在Google Suite的用户中做了自定义字段。分类。ForesattAmong他们。姓名:'foresatt epost', type:email, number: multiple values: 'foresatt epost', type:email, number:多值。

我想用Google Script列出这些值。我用的是这个。https:/developers.google.comadmin-sdkdirectoryv1quickstartapps-script。

要写这段代码。

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.');
  }
}

这样就可以了,但我只想得到值。我现在得到的是什么。

{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}]
  }
}

我想得到的是: [email protected], [email protected], [email protected]

我试了好几种方法,但我怕自己经验不足。

var epost = foresatt.foresatt_epost;

结果是 TypeError: Cannot read property 'foresatt': 不能读取属性'foresatt_epost'

var epost = foresatt('foresatt_epost');

结果是:类型错误:foresatt不是函数。类型错误:foresatt不是一个函数

请告诉我,我如何只获取字段'foresatt epost'的值?

google-apps-script google-admin-sdk
1个回答
2
投票

我相信你的目标如下。

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"},
    ]
  }
}

在这种情况下,这些值可以从以下对象中获取: object.Foresatt.foresatt_epost 阵列。

示例脚本。

const object = {}; //Your object

const res = object.Foresatt.foresatt_epost.map(e => e.value);
console.log(res)  // Outputs: [ '[email protected]', '[email protected]', '[email protected]' ]
  • 如果 user.customSchemas 是上述对象,脚本如下。
var foresatt = user.customSchemas;
const res = foresatt.Foresatt.foresatt_epost.map(e => e.value);
console.log(res)
  • 如果你想以逗号分隔的字符串的形式获取值,你可以使用 res.join(",").

参考文献。

:如果不能保证你的财产将存在于你的。object,你可以做 (object.property||[]).map(...) 而不是 object.property.map(...) 以免出错 Uncaught TypeError: Cannot read property 'forEach' of undefined.

© www.soinside.com 2019 - 2024. All rights reserved.