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

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

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

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

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

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

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

  • บัญชี Google for Education ที่เปิดใช้ Google Classroom

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

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

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

classroom/quickstart/quickstart.gs
/**
 * Lists 10 course names and ids.
 */
function listCourses() {
  /**  here pass pageSize Query parameter as argument to get maximum number of result
   * @see https://developers.google.com/classroom/reference/rest/v1/courses/list
   */
  const optionalArgs = {
    pageSize: 10,
    // Use other parameter here if needed
  };
  try {
    // call courses.list() method to list the courses in classroom
    const response = Classroom.Courses.list(optionalArgs);
    const courses = response.courses;
    if (!courses || courses.length === 0) {
      console.log("No courses found.");
      return;
    }
    // Print the course names and IDs of the courses
    for (const course of courses) {
      console.log("%s (%s)", course.name, course.id);
    }
  } catch (err) {
    // TODO (developer)- Handle Courses.list() exception from Classroom API
    // get errors like PERMISSION_DENIED/INVALID_ARGUMENT/NOT_FOUND
    console.log("Failed with error %s", err.message);
  }
}

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

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

เปิดใช้ Google Classroom API

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

  1. คลิก เครื่องมือแก้ไข
  2. คลิกเพิ่มบริการ ข้างบริการ
  3. เลือก Google Classroom API แล้วคลิกเพิ่ม

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

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

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

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

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

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