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

Google Apps Script เลิกใช้งานบริการ Contacts เมื่อวันที่ 16 ธันวาคม 2022 และจะปิดให้บริการในวันที่ 31 มกราคม 2025

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

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

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

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

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

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

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

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

ตัวอย่างโค้ดต่อไปนี้แสดงวิธีรับกลุ่มรายชื่อติดต่อตามชื่อ ซึ่ง เทียบเท่ากับ getContactGroup ในบริการ 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 ในบริการรายชื่อติดต่อ

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 ในบริการรายชื่อติดต่อ

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);
  }
}