Apps 脚本已于 2022 年 12 月 16 日弃用通讯录服务。请改用 People API 高级服务。People API 使用较新的 JSON 协议,并提供高级功能,例如将联系人与个人资料合并。
通过本指南,您可以了解哪些通讯录服务方法在 People API 高级服务中没有等效方法,了解可以使用哪些替代方法,以及查找用于迁移常见任务的代码示例。如需了解详情,请参阅 Contacts API 迁移指南。
没有 People API 等效方法的方法
以下列出了通讯录服务中没有等效方法在 People API 高级服务中搜索联系人的 getContacts 方法。借助 People API 高级服务,您可以按联系人的 CONTACT 来源的 names、nickNames、emailAddresses、phoneNumbers 和 organizations 字段进行搜索。
没有等效方法的方法
getContactsByAddress(query)
getContactsByAddress(query, label)
getContactsByAddress(query, label)
getContactsByCustomField(query, label)
getContactsByDate(month, day, label)
getContactsByDate(month, day, year, label)
getContactsByDate(month, day, year, label)
getContactsByIM(query)
getContactsByIM(query, label)
getContactsByJobTitle(query)
getContactsByNotes(query)
getContactsByUrl(query)
getContactsByUrl(query, label)
getContactsByGroup(group)
以下列出了使用额外 label 参数的通讯录服务中的 getContacts 方法。您可以使用 People API 高级服务中的 searchContacts 按等效字段获取联系人,但无法将搜索范围限制为特定标签。
具有部分等效项的方法
getContactsByEmailAddress(query, label)
getContactsByName(query, label)
getContactsByPhone(query, label)
People API 提供的其他功能
迁移到 People API 高级服务后,您可以使用 Contacts 服务中不提供的以下 People API 功能:
指定数据源 - 搜索某个人的相关信息时,您可以指定搜索位置,例如 Google 通讯录或 Google 个人资料。
/** * Gets a contact group with the given name * @param {string} name The group name. * @see https://developers.google.com/people/api/rest/v1/contactGroups/list */functiongetContactGroup(name){try{constpeople=People.ContactGroups.list();// Finds the contact group for the person where the name matches.constgroup=people['contactGroups'].find((group)=>group['name']===name);// Prints the contact groupconsole.log('Group:%s',JSON.stringify(group,null,2));}catch(err){// TODO (developers) - Handle exceptionconsole.log('Failedtogetthecontactgroupwithanerror%s',err.message);}}
/** * Gets a contact by the email address. * @param {string} email The email address. * @see https://developers.google.com/people/api/rest/v1/people.connections/list */functiongetContactByEmail(email){try{// Gets the person with that email address by iterating over all contacts.constpeople=People.People.Connections.list('people/me',{personFields:'names,emailAddresses'});constcontact=people['connections'].find((connection)=>{returnconnection['emailAddresses'].some((emailAddress)=>emailAddress['value']===email);});// Prints the contact.console.log('Contact:%s',JSON.stringify(contact,null,2));}catch(err){// TODO (developers) - Handle exceptionconsole.log('Failedtogettheconnectionwithanerror%s',err.message);}}
/** * Gets a list of people in the user's contacts. * @see https://developers.google.com/people/api/rest/v1/people.connections/list */functiongetConnections(){try{// Get the list of connections/contacts of user's profileconstpeople=People.People.Connections.list('people/me',{personFields:'names,emailAddresses'});// Print the connections/contactsconsole.log('Connections:%s',JSON.stringify(people,null,2));}catch(err){// TODO (developers) - Handle exception hereconsole.log('Failedtogettheconnectionwithanerror%s',err.message);}}
/** * Gets the full name (given name and last name) of the contact as a string. * @see https://developers.google.com/people/api/rest/v1/people/get */functiongetFullName(){try{// Gets the person by specifying resource name/account ID// in the first parameter of People.People.get.// This example gets the person for the user running the script.constpeople=People.People.get('people/me',{personFields:'names'});// Prints the full name (given name + family name)console.log(`${people['names'][0]['givenName']}${people['names'][0]['familyName']}`);}catch(err){// TODO (developers) - Handle exceptionconsole.log('Failedtogettheconnectionwithanerror%s',err.message);}}
/** * Gets all the phone numbers for this contact. * @see https://developers.google.com/people/api/rest/v1/people/get */functiongetPhoneNumbers(){try{// Gets the person by specifying resource name/account ID// in the first parameter of People.People.get.// This example gets the person for the user running the script.constpeople=People.People.get('people/me',{personFields:'phoneNumbers'});// Prints the phone numbers.console.log(people['phoneNumbers']);}catch(err){// TODO (developers) - Handle exceptionconsole.log('Failedtogettheconnectionwithanerror%s',err.message);}}
/** * Gets a phone number by type, such as work or home. * @see https://developers.google.com/people/api/rest/v1/people/get */functiongetPhone(){try{// Gets the person by specifying resource name/account ID// in the first parameter of People.People.get.// This example gets the person for the user running the script.constpeople=People.People.get('people/me',{personFields:'phoneNumbers'});// Gets phone number by type, such as home or work.constphoneNumber=people['phoneNumbers'].find((phone)=>phone['type']==='home')['value'];// Prints the phone numbers.console.log(phoneNumber);}catch(err){// TODO (developers) - Handle exceptionconsole.log('Failedtogettheconnectionwithanerror%s',err.message);}}
[[["易于理解","easyToUnderstand","thumb-up"],["解决了我的问题","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["没有我需要的信息","missingTheInformationINeed","thumb-down"],["太复杂/步骤太多","tooComplicatedTooManySteps","thumb-down"],["内容需要更新","outOfDate","thumb-down"],["翻译问题","translationIssue","thumb-down"],["示例/代码问题","samplesCodeIssue","thumb-down"],["其他","otherDown","thumb-down"]],["最后更新时间 (UTC):2025-06-05。"],[[["The Apps Script Contacts service is being shut down on January 31, 2025, and scripts should be migrated to the People API advanced service."],["The People API advanced service offers enhanced features like merging contacts with profiles and searching by various contact fields."],["Some Contacts service methods have no direct equivalents in the People API, requiring alternative approaches for searching contacts."],["The People API provides additional features including specifying data sources, searching by query strings, and batching requests."],["Code samples are available to help developers migrate common tasks from the Contacts service to the People API advanced service."]]],[]]