บริการปฏิทินขั้นสูง

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

ข้อมูลอ้างอิง

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

หากต้องการรายงานปัญหาและค้นหาการสนับสนุนอื่นๆ โปรดดูคู่มือการสนับสนุนปฏิทิน

ส่วนหัวของคำขอ HTTP

บริการปฏิทินขั้นสูงสามารถยอมรับส่วนหัวของคำขอ HTTP If-Match และ If-None-Match ดูรายละเอียดได้ที่เอกสารอ้างอิง

รหัสตัวอย่าง

โค้ดตัวอย่างด้านล่างใช้ API เวอร์ชัน 3

การสร้างเหตุการณ์

ตัวอย่างต่อไปนี้แสดงวิธีสร้างกิจกรรมในปฏิทินเริ่มต้นของผู้ใช้

advanced/calendar.gs
/**
 * Creates an event in the user's default calendar.
 * @see https://developers.google.com/calendar/api/v3/reference/events/insert
 */
function createEvent() {
  const calendarId = 'primary';
  const start = getRelativeDate(1, 12);
  const end = getRelativeDate(1, 13);
  // event details for creating event.
  let event = {
    summary: 'Lunch Meeting',
    location: 'The Deli',
    description: 'To discuss our plans for the presentation next week.',
    start: {
      dateTime: start.toISOString()
    },
    end: {
      dateTime: end.toISOString()
    },
    attendees: [
      {email: 'gduser1@workspacesample.dev'},
      {email: 'gduser2@workspacesample.dev'}
    ],
    // Red background. Use Calendar.Colors.get() for the full list.
    colorId: 11
  };
  try {
    // call method to insert/create new event in provided calandar
    event = Calendar.Events.insert(event, calendarId);
    console.log('Event ID: ' + event.id);
  } catch (err) {
    console.log('Failed with error %s', err.message);
  }
}

/**
 * Helper function to get a new Date object relative to the current date.
 * @param {number} daysOffset The number of days in the future for the new date.
 * @param {number} hour The hour of the day for the new date, in the time zone
 *     of the script.
 * @return {Date} The new date.
 */
function getRelativeDate(daysOffset, hour) {
  const date = new Date();
  date.setDate(date.getDate() + daysOffset);
  date.setHours(hour);
  date.setMinutes(0);
  date.setSeconds(0);
  date.setMilliseconds(0);
  return date;
}

แสดงรายการปฏิทิน

ตัวอย่างต่อไปนี้แสดงวิธีเรียกดูรายละเอียดเกี่ยวกับปฏิทินที่แสดงในรายการปฏิทินของผู้ใช้

advanced/calendar.gs
/**
 * Lists the calendars shown in the user's calendar list.
 * @see https://developers.google.com/calendar/api/v3/reference/calendarList/list
 */
function listCalendars() {
  let calendars;
  let pageToken;
  do {
    calendars = Calendar.CalendarList.list({
      maxResults: 100,
      pageToken: pageToken

    });
    if (!calendars.items || calendars.items.length === 0) {
      console.log('No calendars found.');
      return;
    }
    // Print the calendar id and calendar summary
    for (const calendar of calendars.items) {
      console.log('%s (ID: %s)', calendar.summary, calendar.id);
    }
    pageToken = calendars.nextPageToken;
  } while (pageToken);
}

เหตุการณ์ในรายการ

ตัวอย่างต่อไปนี้แสดงวิธีแสดงรายการกิจกรรม 10 รายการถัดไปที่กำลังจะเกิดขึ้นในปฏิทินเริ่มต้นของผู้ใช้

advanced/calendar.gs
/**
 * Lists the next 10 upcoming events in the user's default calendar.
 * @see https://developers.google.com/calendar/api/v3/reference/events/list
 */
function listNext10Events() {
  const calendarId = 'primary';
  const now = new Date();
  const events = Calendar.Events.list(calendarId, {
    timeMin: now.toISOString(),
    singleEvents: true,
    orderBy: 'startTime',
    maxResults: 10
  });
  if (!events.items || events.items.length === 0) {
    console.log('No events found.');
    return;
  }
  for (const event of events.items) {
    if (event.start.date) {
      // All-day event.
      const start = new Date(event.start.date);
      console.log('%s (%s)', event.summary, start.toLocaleDateString());
      continue;
    }
    const start = new Date(event.start.dateTime);
    console.log('%s (%s)', event.summary, start.toLocaleString());
  }
}

การแก้ไขเหตุการณ์แบบมีเงื่อนไข

ตัวอย่างต่อไปนี้แสดงวิธีอัปเดตกิจกรรมในปฏิทินแบบมีเงื่อนไขโดยใช้ส่วนหัว If-Match สคริปต์นี้จะสร้างกิจกรรมใหม่ รอ 30 วินาที แล้วอัปเดตกิจกรรมเฉพาะในกรณีที่ไม่มีการเปลี่ยนแปลงรายละเอียดกิจกรรมนับตั้งแต่ที่สร้างกิจกรรมเท่านั้น

advanced/calendar.gs
/**
 * Creates an event in the user's default calendar, waits 30 seconds, then
 * attempts to update the event's location, on the condition that the event
 * has not been changed since it was created.  If the event is changed during
 * the 30-second wait, then the subsequent update will throw a 'Precondition
 * Failed' error.
 *
 * The conditional update is accomplished by setting the 'If-Match' header
 * to the etag of the new event when it was created.
 */
function conditionalUpdate() {
  const calendarId = 'primary';
  const start = getRelativeDate(1, 12);
  const end = getRelativeDate(1, 13);
  let event = {
    summary: 'Lunch Meeting',
    location: 'The Deli',
    description: 'To discuss our plans for the presentation next week.',
    start: {
      dateTime: start.toISOString()
    },
    end: {
      dateTime: end.toISOString()
    },
    attendees: [
      {email: 'gduser1@workspacesample.dev'},
      {email: 'gduser2@workspacesample.dev'}
    ],
    // Red background. Use Calendar.Colors.get() for the full list.
    colorId: 11
  };
  event = Calendar.Events.insert(event, calendarId);
  console.log('Event ID: ' + event.getId());
  // Wait 30 seconds to see if the event has been updated outside this script.
  Utilities.sleep(30 * 1000);
  // Try to update the event, on the condition that the event state has not
  // changed since the event was created.
  event.location = 'The Coffee Shop';
  try {
    event = Calendar.Events.update(
        event,
        calendarId,
        event.id,
        {},
        {'If-Match': event.etag}
    );
    console.log('Successfully updated event: ' + event.id);
  } catch (e) {
    console.log('Fetch threw an exception: ' + e);
  }
}

การเรียกข้อมูลกิจกรรมแบบมีเงื่อนไข

ตัวอย่างต่อไปนี้แสดงวิธีดึงข้อมูลกิจกรรมในปฏิทินแบบมีเงื่อนไขโดยใช้ส่วนหัว If-None-Match สคริปต์จะสร้างเหตุการณ์ใหม่ แล้วสำรวจกิจกรรมนั้นเพื่อดูการเปลี่ยนแปลงเป็นเวลา 30 วินาที เมื่อเหตุการณ์มีการเปลี่ยนแปลง ระบบจะดึงข้อมูลเวอร์ชันใหม่

advanced/calendar.gs
/**
 * Creates an event in the user's default calendar, then re-fetches the event
 * every second, on the condition that the event has changed since the last
 * fetch.
 *
 * The conditional fetch is accomplished by setting the 'If-None-Match' header
 * to the etag of the last known state of the event.
 */
function conditionalFetch() {
  const calendarId = 'primary';
  const start = getRelativeDate(1, 12);
  const end = getRelativeDate(1, 13);
  let event = {
    summary: 'Lunch Meeting',
    location: 'The Deli',
    description: 'To discuss our plans for the presentation next week.',
    start: {
      dateTime: start.toISOString()
    },
    end: {
      dateTime: end.toISOString()
    },
    attendees: [
      {email: 'gduser1@workspacesample.dev'},
      {email: 'gduser2@workspacesample.dev'}
    ],
    // Red background. Use Calendar.Colors.get() for the full list.
    colorId: 11
  };
  try {
    // insert event
    event = Calendar.Events.insert(event, calendarId);
    console.log('Event ID: ' + event.getId());
    // Re-fetch the event each second, but only get a result if it has changed.
    for (let i = 0; i < 30; i++) {
      Utilities.sleep(1000);
      event = Calendar.Events.get(calendarId, event.id, {}, {'If-None-Match': event.etag});
      console.log('New event description: ' + event.start.dateTime);
    }
  } catch (e) {
    console.log('Fetch threw an exception: ' + e);
  }
}

การซิงค์กิจกรรม

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

ตัวอย่างต่อไปนี้ใช้เมธอด getRelativeDate(daysOffset, hour) เดียวกันกับที่กำหนดไว้ในตัวอย่างด้านบน

advanced/calendar.gs
/**
 * Retrieve and log events from the given calendar that have been modified
 * since the last sync. If the sync token is missing or invalid, log all
 * events from up to a month ago (a full sync).
 *
 * @param {string} calendarId The ID of the calender to retrieve events from.
 * @param {boolean} fullSync If true, throw out any existing sync token and
 *        perform a full sync; if false, use the existing sync token if possible.
 */
function logSyncedEvents(calendarId, fullSync) {
  const properties = PropertiesService.getUserProperties();
  const options = {
    maxResults: 100
  };
  const syncToken = properties.getProperty('syncToken');
  if (syncToken && !fullSync) {
    options.syncToken = syncToken;
  } else {
    // Sync events up to thirty days in the past.
    options.timeMin = getRelativeDate(-30, 0).toISOString();
  }
  // Retrieve events one page at a time.
  let events;
  let pageToken;
  do {
    try {
      options.pageToken = pageToken;
      events = Calendar.Events.list(calendarId, options);
    } catch (e) {
      // Check to see if the sync token was invalidated by the server;
      // if so, perform a full sync instead.
      if (e.message === 'Sync token is no longer valid, a full sync is required.') {
        properties.deleteProperty('syncToken');
        logSyncedEvents(calendarId, true);
        return;
      }
      throw new Error(e.message);
    }
    if (events.items && events.items.length === 0) {
      console.log('No events found.');
      return;
    }
    for (const event of events.items) {
      if (event.status === 'cancelled') {
        console.log('Event id %s was cancelled.', event.id);
        return;
      }
      if (event.start.date) {
        const start = new Date(event.start.date);
        console.log('%s (%s)', event.summary, start.toLocaleDateString());
        return;
      }
      // Events that don't last all day; they have defined start times.
      const start = new Date(event.start.dateTime);
      console.log('%s (%s)', event.summary, start.toLocaleString());
    }
    pageToken = events.nextPageToken;
  } while (pageToken);
  properties.setProperty('syncToken', events.nextSyncToken);
}