google-apps-script - 无法通过 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);
解决方案
作为一种解决方法,我想提出以下修改。在此修改中,在创建联系人时检索联系人 ID。和,addPhone
和addAddress
用于使用联系人 ID 检索联系人对象。
修改后的脚本:
var newContact = ContactsApp.createContact(fName, actualLastName, email);
var contactId = newContact.getId();
var c = ContactsApp.getContactById(contactId);
c.addAddress(ContactsApp.Field.WORK_ADDRESS, address);
参考:
推荐阅读
- python - 如果没有,如果没有找到字符串 python
- pascal - 在数组中插入从最小到最大排序的值
- python - django 类型错误,“ModelBase”类型的对象没有 len()
- javascript - 如何在 chrome 中调试 Web 应用程序?
- python - 如何静默或重定向 shutil.move() 的输出或以静默模式移动文件的其他方法?
- php - PHP $_POST 数组形式的内容
- mysql - MySQL:限制结果集中每个玩家返回的记录数
- mysql - 如何按州计算订单
- c - C 中的编译器错误,错误:预期表达式
- python - 数组中的 Numpy“放置对象”