You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Apps Script中筛选带特定标签的联系人邮箱列表?

在Apps Script中筛选带特定标签的联系人邮箱列表

你当前的请求未包含标签相关字段,People API里联系人的标签信息存储在memberships字段中,所以首先需要修改请求参数,将该字段加入personFields,之后再遍历联系人筛选匹配目标标签的条目。

完整实现代码

function getContactsByLabel(targetLabel) {
  // 请求联系人时包含标签字段memberships
  const contacts = People.People.Connections.list('people/me', {
    personFields: 'names,emailAddresses,memberships'
  });
  
  const filteredContacts = [];
  
  // 处理无联系人的情况
  if (!contacts.connections) return filteredContacts;
  
  contacts.connections.forEach(person => {
    // 跳过无标签的联系人
    if (!person.memberships) return;
    
    // 检查是否包含目标标签
    const matchesLabel = person.memberships.some(membership => {
      return membership.contactGroupMembership && 
             membership.contactGroupMembership.contactGroupDisplayName === targetLabel;
    });
    
    // 匹配标签且有邮箱时,提取信息
    if (matchesLabel && person.emailAddresses) {
      filteredContacts.push({
        name: person.names[0].displayName,
        email: person.emailAddresses[0].value
      });
    }
  });
  
  return filteredContacts;
}

// 调用示例:替换成你要筛选的标签名称
const targetContacts = getContactsByLabel("重要客户");
console.log(targetContacts);

关键说明

  • 必须添加memberships字段:只有在personFields中指定该字段,API才会返回联系人的标签数据
  • 标签的层级结构:标签名称存储在membership.contactGroupMembership.contactGroupDisplayName中,需要逐层判断避免空引用错误
  • 边界情况处理:代码中处理了无联系人、无标签、无邮箱的场景,避免运行报错
  • 多邮箱处理:示例中取第一个邮箱,若需要所有邮箱,可遍历person.emailAddresses数组

内容的提问来源于stack exchange,提问作者Carré d'As

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.05 20:34:53