ย้ายข้อมูลจากบริการ Contacts ไปยังบริการขั้นสูงของ People API

Apps Script เลิกใช้งานบริการ Contacts เมื่อวันที่ 16 ธันวาคม 2022 แต่ให้ใช้บริการขั้นสูงของ People API แทน People API ใช้ โปรโตคอล JSON เวอร์ชันใหม่กว่าและมี ฟีเจอร์ขั้นสูง เช่น การผสานรายชื่อติดต่อกับโปรไฟล์

ใช้คู่มือนี้เพื่อดูว่าเมธอดบริการ Contacts ใดที่ไม่มีเทียบเท่าใน บริการขั้นสูงของ People API ดูว่าคุณใช้อะไรแทนได้บ้าง และดูตัวอย่างโค้ด สำหรับการย้ายข้อมูลงานทั่วไป ดูข้อมูลเพิ่มเติมได้ที่คำแนะนำในการย้ายข้อมูล Contacts API

เมธอดที่ไม่มีเมธอดเทียบเท่าใน People API

รายการต่อไปนี้แสดงgetContactsเมธอดในบริการรายชื่อติดต่อที่ไม่มี วิธี เทียบเท่าในการค้นหารายชื่อติดต่อในบริการขั้นสูงของ People API บริการขั้นสูงของ People API ช่วยให้คุณค้นหาตามฟิลด์ names, nickNames, emailAddresses, phoneNumbers และ organizations ของรายชื่อติดต่อที่มาจากแหล่งที่มาของ CONTACT ได้

วิธีการที่ไม่มีรายการเทียบเท่า
  • 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)

รายการต่อไปนี้แสดงเมธอด getContacts จากบริการ Contacts ที่ใช้พารามิเตอร์ extra label คุณใช้ searchContacts จากบริการขั้นสูงของ People API เพื่อรับรายชื่อติดต่อตามช่องที่เทียบเท่าได้ แต่จะจำกัดการค้นหาให้แสดงเฉพาะป้ายกำกับที่เฉพาะเจาะจงไม่ได้

วิธีการที่มีส่วนที่เทียบเท่า
  • getContactsByEmailAddress(query, label)
  • getContactsByName(query, label)
  • getContactsByPhone(query, label)

ฟีเจอร์เพิ่มเติมที่ใช้ได้กับ People API

เมื่อย้ายข้อมูลไปยังบริการขั้นสูงของ People API คุณจะเข้าถึงฟีเจอร์ People API ต่อไปนี้ที่ไม่มีในบริการ Contacts ได้

  • ระบุแหล่งข้อมูล - เมื่อค้นหาข้อมูลเกี่ยวกับบุคคล คุณสามารถระบุตำแหน่งที่จะค้นหาได้ เช่น รายชื่อติดต่อใน Google หรือโปรไฟล์ Google
  • ค้นหาบุคคลตามสตริงการค้นหา - คุณจะได้รับรายการโปรไฟล์และรายชื่อติดต่อที่ตรงกับสตริงที่เฉพาะเจาะจง
  • คำขอแบบกลุ่ม - คุณสามารถจัดกลุ่มการเรียกใช้ People API เพื่อช่วยลดเวลาในการเรียกใช้สคริปต์ได้

ตัวอย่างโค้ดสำหรับงานที่พบบ่อย

ส่วนนี้แสดงรายการงานทั่วไปจากบริการ Contacts ตัวอย่างโค้ด แสดงวิธีสร้างงานโดยใช้บริการขั้นสูงของ People API

รับกลุ่มรายชื่อติดต่อตามชื่อ

ตัวอย่างโค้ดต่อไปนี้แสดงวิธีรับกลุ่มรายชื่อติดต่อตามชื่อ ซึ่ง เทียบเท่ากับ getContactGroup(name) ในบริการ Contacts

advanced/people.gs
/**
 * 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
 */
function getContactGroup(name) {
  try {
    const people = People.ContactGroups.list();
    // Finds the contact group for the person where the name matches.
    const group = people['contactGroups'].find((group) => group['name'] === name);
    // Prints the contact group
    console.log('Group: %s', JSON.stringify(group, null, 2));
  } catch (err) {
    // TODO (developers) - Handle exception
    console.log('Failed to get the contact group with an error %s', err.message);
  }
}

รับรายชื่อติดต่อทางอีเมล

ตัวอย่างโค้ดต่อไปนี้แสดงวิธีรับรายชื่อติดต่อตามอีเมล ซึ่งเทียบเท่ากับ getContact(emailAddress) ในบริการรายชื่อติดต่อ

advanced/people.gs
/**
 * 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
 */
function getContactByEmail(email) {
  try {
    // Gets the person with that email address by iterating over all contacts.
    const people = People.People.Connections.list('people/me', {
      personFields: 'names,emailAddresses'
    });
    const contact = people['connections'].find((connection) => {
      return connection['emailAddresses'].some((emailAddress) => emailAddress['value'] === email);
    });
    // Prints the contact.
    console.log('Contact: %s', JSON.stringify(contact, null, 2));
  } catch (err) {
    // TODO (developers) - Handle exception
    console.log('Failed to get the connection with an error %s', err.message);
  }
}

รับรายชื่อติดต่อทั้งหมด

ตัวอย่างโค้ดต่อไปนี้แสดงวิธีรับรายชื่อติดต่อทั้งหมดของผู้ใช้ ซึ่งเทียบเท่ากับ getContacts() ในบริการ Contacts

advanced/people.gs
/**
 * Gets a list of people in the user's contacts.
 * @see https://developers.google.com/people/api/rest/v1/people.connections/list
 */
function getConnections() {
  try {
    // Get the list of connections/contacts of user's profile
    const people = People.People.Connections.list('people/me', {
      personFields: 'names,emailAddresses'
    });
    // Print the connections/contacts
    console.log('Connections: %s', JSON.stringify(people, null, 2));
  } catch (err) {
    // TODO (developers) - Handle exception here
    console.log('Failed to get the connection with an error %s', err.message);
  }
}

รับชื่อและนามสกุลของผู้ติดต่อ

ตัวอย่างโค้ดต่อไปนี้แสดงวิธีรับชื่อเต็มของผู้ติดต่อ ซึ่งเทียบเท่ากับ getFullName() ในบริการ Contacts

advanced/people.gs
/**
 * 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
 */
function getFullName() {
  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.
    const people = 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 exception
    console.log('Failed to get the connection with an error %s', err.message);
  }
}

รับหมายเลขโทรศัพท์ทั้งหมดของรายชื่อติดต่อ

ตัวอย่างโค้ดต่อไปนี้แสดงวิธีรับหมายเลขโทรศัพท์ทั้งหมดสำหรับ รายชื่อติดต่อ ซึ่งเทียบเท่ากับ getPhones() ในบริการ Contacts

advanced/people.gs
/**
 * Gets all the phone numbers for this contact.
 * @see https://developers.google.com/people/api/rest/v1/people/get
 */
function getPhoneNumbers() {
  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.
    const people = People.People.get('people/me', {personFields: 'phoneNumbers'});
    // Prints the phone numbers.
    console.log(people['phoneNumbers']);
  } catch (err) {
    // TODO (developers) - Handle exception
    console.log('Failed to get the connection with an error %s', err.message);
  }
}

ดูหมายเลขโทรศัพท์ที่เฉพาะเจาะจงสำหรับรายชื่อติดต่อ

ตัวอย่างโค้ดต่อไปนี้แสดงวิธีรับหมายเลขโทรศัพท์ที่เฉพาะเจาะจงสำหรับ รายชื่อติดต่อ ซึ่งเทียบเท่ากับ getPhoneNumber() ในบริการรายชื่อติดต่อ

advanced/people.gs
/**
 * Gets a phone number by type, such as work or home.
 * @see https://developers.google.com/people/api/rest/v1/people/get
 */
function getPhone() {
  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.
    const people = People.People.get('people/me', {personFields: 'phoneNumbers'});
    // Gets phone number by type, such as home or work.
    const phoneNumber = people['phoneNumbers'].find((phone) => phone['type'] === 'home')['value'];
    // Prints the phone numbers.
    console.log(phoneNumber);
  } catch (err) {
    // TODO (developers) - Handle exception
    console.log('Failed to get the connection with an error %s', err.message);
  }
}