เริ่มต้นใช้งาน Google Apps Script

สร้าง Google Apps Script ที่ส่งคำขอไปยัง People API

การเริ่มต้นอย่างรวดเร็วจะอธิบายวิธีตั้งค่าและเรียกใช้แอปที่เรียกใช้ Google Workspace API คู่มือเริ่มต้นฉบับย่อนี้ใช้วิธีการตรวจสอบสิทธิ์แบบ ง่ายที่เหมาะกับสภาพแวดล้อมการทดสอบ สําหรับสภาพแวดล้อมการใช้งานจริง เราขอแนะนําให้ศึกษาเกี่ยวกับ การตรวจสอบสิทธิ์และการให้สิทธิ์ ก่อน เลือกข้อมูลเข้าถึง ที่เหมาะสมกับแอปของคุณ

ใน Apps Script สตาร์ทเตอร์ด่วนของ Google Workspace จะใช้บริการขั้นสูงของ Google เพื่อเรียกใช้ Google Workspace API และจัดการรายละเอียดบางอย่างของขั้นตอนการตรวจสอบสิทธิ์ และการให้สิทธิ์

วัตถุประสงค์

  • กำหนดค่าสภาพแวดล้อม
  • สร้างและกำหนดค่าสคริปต์
  • เรียกใช้สคริปต์

ข้อกำหนดเบื้องต้น

  • สิทธิ์เข้าถึง Google ไดรฟ์

สร้างสคริปต์

  1. สร้างสคริปต์ใหม่ในเครื่องมือแก้ไขสคริปต์ Apps Script โดยไปที่ script.google.com/create
  2. แทนที่เนื้อหาของเครื่องมือแก้ไขสคริปต์ด้วยโค้ดต่อไปนี้

people/quickstart/quickstart.gs
/**
 * @typedef {Object} EmailAddress
 * @see https://developers.google.com/people/api/rest/v1/people#Person
 * @property {string} value
 * Note: This is a partial definition.
 */

/**
 * @typedef {Object} Name
 * @see https://developers.google.com/people/api/rest/v1/people#Person
 * @property {string} displayName
 * Note: This is a partial definition.
 */

/**
 * @typedef {Object} Person
 * @see https://developers.google.com/people/api/rest/v1/people#Person
 * @property {Name[]} names
 * @property {EmailAddress[]} [emailAddresses]
 * Note: This is a partial definition.
 */

/**
 * @typedef {Object} Connection
 * @see https://developers.google.com/people/api/rest/v1/people.connections/list
 * @property {Person[]} connections
 * Note: This is a partial definition.
 */

/**
 * Print the display name if available for 10 connections.
 */
function listConnectionNames() {
  // Use the People API to list the connections of the logged in user.
  // See: https://developers.google.com/people/api/rest/v1/people.connections/list
  if (!People || !People.People || !People.People.Connections) {
    throw new Error("People service not enabled.");
  }
  const connections = People.People.Connections.list("people/me", {
    pageSize: 10,
    personFields: "names,emailAddresses",
  });
  if (!connections.connections) {
    console.log("No connections found.");
    return;
  }
  for (const person of connections.connections) {
    if (
      person.names &&
      person.names.length > 0 &&
      person.names[0].displayName
    ) {
      console.log(person.names[0].displayName);
    } else {
      console.log("No display name found for connection.");
    }
  }
}

  1. คลิกบันทึก
  2. คลิกโปรเจ็กต์ที่ไม่มีชื่อ พิมพ์ Quickstart แล้วคลิกเปลี่ยนชื่อ

กำหนดค่าสคริปต์

เปิดใช้ People API

เปิดโปรเจ็กต์ Apps Script

  1. คลิกตัดต่อวิดีโอ
  2. คลิกเพิ่มบริการ ข้างบริการ
  3. เลือก People API แล้วคลิกเพิ่ม

เรียกใช้ตัวอย่าง

ในเครื่องมือแก้ไขสคริปต์ของ Apps Script ให้คลิกเรียกใช้

เมื่อเรียกใช้ตัวอย่างเป็นครั้งแรก ระบบจะแจ้งให้คุณให้สิทธิ์เข้าถึง

  1. คลิกตรวจสอบสิทธิ์
  2. เลือกบัญชี
  3. คลิกอนุญาต

บันทึกการดำเนินการของสคริปต์จะปรากฏที่ด้านล่างของหน้าต่าง

ขั้นตอนถัดไป