উন্নত ক্যালেন্ডার পরিষেবা

উন্নত ক্যালেন্ডার পরিষেবা আপনাকে Apps স্ক্রিপ্টে সর্বজনীন Google ক্যালেন্ডার API ব্যবহার করার অনুমতি দেয়৷ অনেকটা Apps Script-এর অন্তর্নির্মিত ক্যালেন্ডার পরিষেবার মতো, এই API স্ক্রিপ্টগুলিকে ব্যবহারকারীর Google ক্যালেন্ডারে অ্যাক্সেস এবং পরিবর্তন করার অনুমতি দেয়, ব্যবহারকারীর সদস্যতা নেওয়া অতিরিক্ত ক্যালেন্ডারগুলি সহ৷ বেশিরভাগ ক্ষেত্রে, অন্তর্নির্মিত পরিষেবাটি ব্যবহার করা সহজ, তবে এই উন্নত পরিষেবাটি পৃথক ইভেন্টগুলির জন্য পটভূমির রঙ সেট করা সহ কয়েকটি অতিরিক্ত বৈশিষ্ট্য সরবরাহ করে।

রেফারেন্স

এই পরিষেবার বিস্তারিত তথ্যের জন্য, সর্বজনীন Google ক্যালেন্ডার 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);
}