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

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

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

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

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

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

  • ระบุแหล่งข้อมูล - เมื่อ ค้นหาข้อมูลเกี่ยวกับบุคคล คุณสามารถระบุตำแหน่งที่จะค้นหาได้ เช่น รายชื่อติดต่อใน 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 ในบริการ Contacts

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 ในบริการ Contacts

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