การเริ่มต้น Google Apps Script อย่างรวดเร็ว

การเริ่มต้นอย่างรวดเร็วอธิบายวิธีตั้งค่าและเรียกใช้แอปที่เรียกใช้ Google Workspace API

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

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

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

  • สร้างสคริปต์
  • เปิดใช้งาน Google Calendar API
  • เรียกใช้ตัวอย่าง

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

  • บัญชี Google ที่เปิดใช้ Google ปฏิทิน

  • การเข้าถึง Google ไดรฟ์

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

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

calendar/quickstart/quickstart.gs
/**
 * Lists 10 upcoming events in the user's calendar.
 * @see https://developers.google.com/calendar/api/v3/reference/events/list
 */
function listUpcomingEvents() {
  const calendarId = 'primary';
  // Add query parameters in optionalArgs
  const optionalArgs = {
    timeMin: (new Date()).toISOString(),
    showDeleted: false,
    singleEvents: true,
    maxResults: 10,
    orderBy: 'startTime'
    // use other optional query parameter here as needed.
  };
  try {
    // call Events.list method to list the calendar events using calendarId optional query parameter
    const response = Calendar.Events.list(calendarId, optionalArgs);
    const events = response.items;
    if (events.length === 0) {
      console.log('No upcoming events found');
      return;
    }
    // Print the calendar events
    for (const event of events) {
      let when = event.start.dateTime;
      if (!when) {
        when = event.start.date;
      }
      console.log('%s (%s)', event.summary, when);
    }
  } catch (err) {
    // TODO (developer) - Handle exception from Calendar API
    console.log('Failed with error %s', err.message);
  }
}

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

เปิดใช้ Google Calendar API

  1. เปิดโปรเจ็กต์ Apps Script
  2. คลิกเอดิเตอร์
  3. ถัดจากบริการ ให้คลิกเพิ่มบริการ
  4. เลือก Calendar API และคลิกเพิ่ม

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

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

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

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

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

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