【问题标题】:How can I add a label to a contact while creating it with Google Apps Script?如何在使用 Google Apps 脚本创建联系人时为其添加标签?
【发布时间】:2020-08-07 17:44:31
【问题描述】:

我可以在提交表单时创建新的 Google 联系人。我需要为这些联系人添加标签,但我找不到任何方法来做到这一点。可以加标签吗?这是我用于查找/创建联系人的代码的 sn-p:

var contact = ContactsApp.getContact(email);
if(!contact){
  contact = ContactsApp.createContact(firstName, lastName, email);
}

【问题讨论】:

  • 感谢您的回复。我很高兴你的问题得到了解决。从您的回复中,我可以理解我的回答对您的情况没有用处。这是由于我的技术不好,我对此深表歉意。我想删除我的答案,因为我不想混淆其他用户。那么您可以将答案发布为答案吗?这样,它将对遇到相同问题的其他用户有用。我再次为我糟糕的技术深表歉意。
  • 顺便说一句,小提示:var contact = ContactsApp.getContact(email) || ContactsApp.createContact(firstName, lastName, email);
  • 感谢您的提示。老实说,正是您的代码让我意识到“标签”在 API 中实际上被称为“联系人组”。我将发布我的解决方案作为答案。 :)
  • @AaryanGupta - 我假设你的意思是 Tanaike 的代码?顺便说一句,很高兴知道,我通常不使用联系人,所以很高兴知道:)

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


【解决方案1】:

要将标签添加到联系人,请将联系人添加到联系人组。这是一个示例代码:

  let labelName = 'myLabel';
  let firstName = 'John';
  let lastName = 'Doe';
  let email = 'john.doe@gmail.com';

  //find or create the Contact
  let contact = ContactsApp.getContact(email) || ContactsApp.createContact(firstName, lastName, email);
  }

  //find the Label
  let group = ContactsApp.getContactGroup(labelName);

  //add label to the contact
  group.addContact(contact);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-19
    • 1970-01-01
    • 2012-10-02
    • 1970-01-01
    相关资源
    最近更新 更多