为Google Apps用户添加外部电子邮件

时间:2014-07-11 18:05:56

标签: email google-apps-script google-sheets

我已在Google电子表格脚本中尝试使用此代码,为我的Google Apps域用户添加另一封(外部)电子邮件:

function createExternalEmail() {
  var userId = "sampleuser@mydomain.org";
  var update = {
    emails:
    {
      address: "sampleuser@homeemail.com",
      type: "home",
      customType: "",
      primary: false
    }, 
    addresses:
    {
      type: "home",
      customType: "",
      streetAddress: "1600 Amphitheatre Parkway",
      locality: "Mountain View",
      region: "CA",
      postalCode: "94043"
 }
  }
    update = AdminDirectory.Users.update(update, userId);
    var user = AdminDirectory.Users.get(userId);
    Logger.log('User emails:\n %s', JSON.stringify(user.emails, null, 2));
    Logger.log('User addresses:\n %s', JSON.stringify(user.addresses, null, 2));
}

但是Logger.log中没有返回更新的值。缺少什么?

1 个答案:

答案 0 :(得分:1)

根据Admin SDK Referenceemailsaddresses是列表(数组)。您已在更新中提供了对象。将它们用方括号括起来,使它们成为单元素数组。

尝试:

function createExternalEmail() {
  var userId = "sampleuser@mydomain.org";
  var update = {
      emails:
      [{
        address: "sampleuser@homeemail.com",
        type: "home",
        customType: "",
        primary: false
      }], 
      addresses:
      [{
        type: "home",
        customType: "",
        streetAddress: "1600 Amphitheatre Parkway",
        locality: "Mountain View",
        region: "CA",
        postalCode: "94043"
      }]
    }
    update = AdminDirectory.Users.update(update, userId);  // You should check your response!!!
    var user = AdminDirectory.Users.get(userId);
    Logger.log('User emails:\n %s', JSON.stringify(user.emails, null, 2));
    Logger.log('User addresses:\n %s', JSON.stringify(user.addresses, null, 2));
}