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

สำคัญ: ย้ายข้อมูลสคริปต์จากบริการ Contacts ไปยังบริการขั้นสูงของ People API ก่อนที่ Apps Script จะปิดให้บริการ Contacts ในเดือนมีนาคม 2023

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

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

เมธอดที่ไม่มีรายการที่เทียบเท่า People API

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

เมธอดที่มีค่าเทียบเท่าบางส่วน
  • getContactsByEmailAddress(query, label)
  • getContactsByName(query, label)
  • getContactsByPhone(query, label)

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

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

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

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

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

เรียกกลุ่มรายชื่อติดต่อตามชื่อ

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

ขั้นสูง/บุคคล.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) ในบริการ Contacts

ขั้นสูง/บุคคล.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

ขั้นสูง/บุคคล.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

ขั้นสูง/บุคคล.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() ในบริการรายชื่อติดต่อ

ขั้นสูง/บุคคล.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() ในบริการรายชื่อติดต่อ

ขั้นสูง/บุคคล.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);
  }
}