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

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

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

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

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

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

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

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

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

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

adminSDK/directory/quickstart.gs
/**
 * Lists users in a Google Workspace domain.
 * @see https://developers.google.com/admin-sdk/directory/reference/rest/v1/users/list
 */
function listUsers() {
  const optionalArgs = {
    customer: "my_customer",
    maxResults: 10,
    orderBy: "email",
  };
  if (!AdminDirectory || !AdminDirectory.Users) {
    throw new Error("Enable the AdminDirectory Advanced Service.");
  }
  const response = AdminDirectory.Users.list(optionalArgs);
  const users = response.users;
  if (!users || users.length === 0) {
    console.log("No users found.");
    return;
  }
  // Print the list of user's full name and email
  console.log("Users:");
  for (const user of users) {
    if (user.primaryEmail) {
      if (user.name?.fullName) {
        console.log("%s (%s)", user.primaryEmail, user.name.fullName);
      } else {
        console.log("%s", user.primaryEmail);
      }
    }
  }
}

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

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

เปิดใช้ Directory API

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

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

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

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

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

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

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

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