2014-07-11 75 views
0

我試過這個代碼在谷歌電子表格腳本添加另一個(外部)電子郵件我的谷歌Apps的域用戶:添加外部電子郵件的谷歌Apps用戶,

function createExternalEmail() { 
    var userId = "[email protected]"; 
    var update = { 
    emails: 
    { 
     address: "[email protected]", 
     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

根據Admin SDK Reference,emailsaddresses是列表(數組)。您在更新中提供了對象。將它們用方括號括起來,使它們成爲一個元素的數組。

嘗試:

function createExternalEmail() { 
    var userId = "[email protected]"; 
    var update = { 
     emails: 
     [{ 
     address: "[email protected]", 
     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)); 
} 
+0

優秀。我一直用方括號來支持它,但我一直給我錯誤。我如何檢查回覆。 – ghozx

+0

API doc說:「如果成功,此方法將在響應正文中返回[Users資源](https://developers.google.com/admin-sdk/directory/v1/reference/users#resource)。」如果不成功,那麼我預計會有其他的迴應。 (NULL?0?錯誤字符串?異常?沒有記錄,因此您需要試驗才能知道。)通過[Try It](https://developers.google.com/admin-sdk/directory/v1/reference/users/update#try-it)鏈接,會生成異常 - 但這並不意味着通過GAS API流過相同的行爲。您可以檢查響應是否爲非空或響應的類型是用戶。 – Mogsdad

相關問題