使用自定义字段创建联系人

时间:2020-06-29 12:22:02

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

我正在尝试使用自定义字段创建联系人,如文档here中所述。 因此,我尝试从文档中使用以下代码:

var contacts = ContactsApp.getContactsByName('John Doe');
var field = contacts[0].getCustomFields()[0];
field.setLabel('Mail application');
field.setValue('Gmail');

运行脚本时,出现以下错误:

TypeError:无法读取未定义的属性“ setLabel”

有什么想法吗?


这是我完整的代码,我想在其中添加一个名为“ ID号”的自定义字段:

var clientaddress = spreadsheet.getRange('L2').getValue();
  var clientcontact = ContactsApp.createContact(clientfirstname, clientlastname, clientemail);
    var contactgroup = spreadsheet.getRange('AL2').getValue(); // AL2 has the group name
    var group = ContactsApp.getContactGroup(contactgroup);
    clientcontact = clientcontact.addToGroup(group);
    var address = clientcontact.addAddress(ContactsApp.Field.HOME_ADDRESS,clientaddress);
    var phone = clientcontact.addPhone(ContactsApp.Field.MOBILE_PHONE, clientmobile);  
    var IDnumber = ?

这是我要作为测试运行的代码:

function quicktest () {
 var contacts = ContactsApp.getContactsByName('XXXX');
 var IDnumber = contacts.addCustomField("IDnumber","12345");
}

好的,现在我们知道它是一个数组,我在代码中添加了[0],但仍然没有乐趣。我确保选择的联系人没有重复,因此我可以确定它在编辑正确联系人。

function quicktest () {
 var contacts = ContactsApp.getContactsByName('XXX');
  var IDnumber = contacts[0].addCustomField("IDnumber","12345");
}

这是联系人“​​更多详细信息”页面的屏幕截图。 enter image description here


是的,它现在正在工作:) 谢谢大家! 修改后的代码:

function quicktest () {
 var contacts = ContactsApp.createContact("test1", "familytest1", "email@email.com").getId();
 ContactsApp.getContactById(contacts).addCustomField("IDnumber","12345");
}

Google ContactS的屏幕截图: enter image description here

1 个答案:

答案 0 :(得分:3)

field.setLabel('邮件应用程序');

无法读取未定义的属性'setLabel'

该错误表明fieldundefined,并且它无法读取setLabel中名为undefined的属性(因为undefined没有这样的方法;只有customField类可以)。

fieldundefined,因为联系人“ John Doe”没有与之关联的自定义字段。

使用Contact.addCustomField("fieldName","fieldValue")向联系人添加自定义字段:

clientcontact.addCustomField("IDnumber","12345");
相关问题