首页 > 解决方案 > 无法通过 Google Apps 脚本向联系人添加地址 [已解决]

问题描述

我可以通过 Google Apps 脚本创建联系人并添加电话号码,但我无法添加地址,出现错误“找不到您请求的资源”。

注意:fName、actualLastName、email、address & phone1 都是字符串

//  create the Contact
var newContact = ContactsApp.createContact(fName, actualLastName, email);
var newName = newContact.getFullName();
Logger.log("newName: " + newName);
Logger.log("New contact added");



//  attempt to add the address - DOESN'T WORK
try {
  Logger.log("Wanting to add this address: ", address);
  newContact.addAddress(ContactsApp.Field.WORK_ADDRESS, address);  
  Logger.log("Address added");
} catch(err) {
  Logger.log("Stumbled while trying to add address: " + err.message);
  Browser.msgBox("Stumbled while trying to add address to contact");
}

记录的错误消息是:“尝试添加地址时出现信息错误:找不到您请求的资源。”

添加电话号码可以正常工作:

newContact.addPhone(ContactsApp.Field.MOBILE_PHONE, phone1);

并且联系人被添加到适当的组中:

var group = ContactsApp.getContactGroup("System Group: My Contacts");
group.addContact(newContact);

标签: google-apps-scriptgoogle-contacts-api

解决方案


作为一种解决方法,我想提出以下修改。在此修改中,在创建联系人时检索联系人 ID。和,addPhoneaddAddress用于使用联系人 ID 检索联系人对象。

修改后的脚本:

var newContact = ContactsApp.createContact(fName, actualLastName, email);
var contactId = newContact.getId();
var c = ContactsApp.getContactById(contactId);
c.addAddress(ContactsApp.Field.WORK_ADDRESS, address);

参考:


推荐阅读