একটি দলের ছুটির ক্যালেন্ডার তৈরি করুন

কোডিং স্তর : শিক্ষানবিস
সময়কাল : ১৫ মিনিট
প্রকল্পের ধরণ : সময়-চালিত ট্রিগার সহ অটোমেশন

উদ্দেশ্য

  • সমাধানটি কী করে তা বুঝুন।
  • সমাধানের মধ্যে অ্যাপস স্ক্রিপ্ট পরিষেবাগুলি কী করে তা বুঝুন।
  • স্ক্রিপ্ট সেট আপ করুন।
  • স্ক্রিপ্টটি চালান।

এই সমাধান সম্পর্কে

একটি ভাগ করা ছুটির ক্যালেন্ডার আপনার দলকে সহযোগিতা করতে সাহায্য করার জন্য একটি দুর্দান্ত হাতিয়ার; যে কেউ এক নজরে নির্ধারণ করতে পারে কে অফিসের বাইরে। এই সমাধানটি আপনাকে আপনার সহকর্মীরা কখন অফিসের বাইরে আছেন তা দেখতে দেয়, কোনও ম্যানুয়াল এন্ট্রির প্রয়োজন হয় না।

ছুটির ক্যালেন্ডারের উদাহরণ

কিভাবে এটা কাজ করে

এই সমাধানটি গুগল গ্রুপের প্রতিটি ব্যক্তির পৃথক ক্যালেন্ডারের উপর ভিত্তি করে একটি ভাগ করা ছুটির ক্যালেন্ডার তৈরি করে। যখন কেউ সময় বুক করে, তখন তারা "অবকাশ" বা "অফিসের বাইরে" এর মতো কীওয়ার্ড ব্যবহার করে তাদের ব্যক্তিগত গুগল ক্যালেন্ডারে একটি ইভেন্ট যোগ করে।

প্রতি ঘন্টায়, স্ক্রিপ্টটি গ্রুপের সদস্যদের ক্যালেন্ডার স্ক্যান করে এবং উপযুক্ত ইভেন্টগুলিকে শেয়ার করা ক্যালেন্ডারের সাথে সিঙ্ক করে। নতুন ইভেন্টের জন্য স্ক্রিপ্ট কতবার স্ক্যান করবে তা আপনি পরিবর্তন করতে পারেন।

এই সমাধানটি কেবলমাত্র সেই ক্যালেন্ডার ইভেন্টগুলি অ্যাক্সেস করে যা আপনার সহকর্মীরা তাদের গোপনীয়তা সেটিংসের মাধ্যমে আপনার কাছে দৃশ্যমান করেছেন।

অ্যাপস স্ক্রিপ্ট পরিষেবা

এই সমাধানটি নিম্নলিখিত পরিষেবাগুলি ব্যবহার করে:

পূর্বশর্ত

এই নমুনাটি ব্যবহার করার জন্য, আপনার নিম্নলিখিত পূর্বশর্তগুলি প্রয়োজন:

  • একটি Google অ্যাকাউন্ট (Google Workspace অ্যাকাউন্টের জন্য প্রশাসকের অনুমোদনের প্রয়োজন হতে পারে)।
  • ইন্টারনেট অ্যাক্সেস সহ একটি ওয়েব ব্রাউজার।

স্ক্রিপ্ট সেট আপ করুন

একটি দলগত ছুটির ক্যালেন্ডার তৈরি করুন

  1. গুগল ক্যালেন্ডার খুলুন।
  2. "টিম ভ্যাকেশনস" নামে একটি নতুন ক্যালেন্ডার তৈরি করুন
  3. ক্যালেন্ডারের সেটিংসে, "ইন্টিগ্রেট ক্যালেন্ডার" এর অধীনে, ক্যালেন্ডার আইডিটি কপি করুন।

অ্যাপস স্ক্রিপ্ট প্রজেক্ট তৈরি করুন

  1. ভ্যাকেশন ক্যালেন্ডার অ্যাপস স্ক্রিপ্ট প্রজেক্ট খুলতে নিচের বোতামটি ক্লিক করুন।
    প্রকল্পটি খুলুন
  2. ওভারভিউ ক্লিক করুন।
  3. ওভারভিউ পৃষ্ঠায়, একটি অনুলিপি তৈরি করুন ক্লিক করুন একটি কপি তৈরির আইকন .
  4. আপনার কপি করা অ্যাপস স্ক্রিপ্ট প্রজেক্টে, TEAM_CALENDAR_ID ভেরিয়েবলটিকে আপনার আগে তৈরি করা ক্যালেন্ডারের আইডিতে সেট করুন।
  5. আপনার দলের সদস্যদের ধারণকারী একটি Google গ্রুপের ইমেল ঠিকানায় GROUP_EMAIL ভেরিয়েবল সেট করুন।
  6. Services এর পাশে, Add a service ক্লিক করুন।
  7. গুগল ক্যালেন্ডার এপিআই নির্বাচন করুন এবং অ্যাড এ ক্লিক করুন।

স্ক্রিপ্টটি চালান

  1. আপনার কপি করা অ্যাপস স্ক্রিপ্ট প্রজেক্টে, ফাংশন ড্রপডাউনে, সেটআপ নির্বাচন করুন।
  2. রান ক্লিক করুন।
  3. অনুরোধ করা হলে, স্ক্রিপ্টটি অনুমোদন করুন। যদি OAuth সম্মতি স্ক্রিনে "This app is not verified" সতর্কতা প্রদর্শিত হয়, তাহলে Advanced > Go to {Project Name} (unsafe) নির্বাচন করে এগিয়ে যান।

  4. সম্পূর্ণ হয়ে গেলে, টিম ভ্যাকেশন ক্যালেন্ডার ইভেন্টে পূর্ণ কিনা তা নিশ্চিত করতে ক্যালেন্ডারে ফিরে যান।

কোডটি পর্যালোচনা করুন

এই সমাধানের জন্য অ্যাপস স্ক্রিপ্ট কোড পর্যালোচনা করতে, নীচের "সোর্স কোড দেখুন" এ ক্লিক করুন:

সোর্স কোড দেখুন

কোড.জিএস

সমাধান/অটোমেশন/ভ্যাকেশন-ক্যালেন্ডার/কোড.জেএস
// To learn how to use this script, refer to the documentation:
// https://developers.google.com/apps-script/samples/automations/vacation-calendar

/*
Copyright 2022 Google LLC

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    https://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

// Set the ID of the team calendar to add events to. You can find the calendar's
// ID on the settings page.
const TEAM_CALENDAR_ID = "ENTER_TEAM_CALENDAR_ID_HERE";
// Set the email address of the Google Group that contains everyone in the team.
// Ensure the group has less than 500 members to avoid timeouts.
// Change to an array in order to add indirect members frrm multiple groups, for example:
// let GROUP_EMAIL = ['ENTER_GOOGLE_GROUP_EMAIL_HERE', 'ENTER_ANOTHER_GOOGLE_GROUP_EMAIL_HERE'];
const GROUP_EMAIL = "ENTER_GOOGLE_GROUP_EMAIL_HERE";

const ONLY_DIRECT_MEMBERS = false;

const KEYWORDS = ["vacation", "ooo", "out of office", "offline"];
const MONTHS_IN_ADVANCE = 3;

/**
 * Sets up the script to run automatically every hour.
 */
function setup() {
  const triggers = ScriptApp.getProjectTriggers();
  if (triggers.length > 0) {
    throw new Error("Triggers are already setup.");
  }
  ScriptApp.newTrigger("sync").timeBased().everyHours(1).create();
  // Runs the first sync immediately.
  sync();
}

/**
 * Looks through the group members' public calendars and adds any
 * 'vacation' or 'out of office' events to the team calendar.
 */
function sync() {
  // Defines the calendar event date range to search.
  const today = new Date();
  const maxDate = new Date();
  maxDate.setMonth(maxDate.getMonth() + MONTHS_IN_ADVANCE);

  // Determines the time the the script was last run.
  let lastRun = PropertiesService.getScriptProperties().getProperty("lastRun");
  lastRun = lastRun ? new Date(lastRun) : null;

  // Gets the list of users in the Google Group.
  let users = getAllMembers(GROUP_EMAIL);
  if (ONLY_DIRECT_MEMBERS) {
    users = GroupsApp.getGroupByEmail(GROUP_EMAIL).getUsers();
  } else if (Array.isArray(GROUP_EMAIL)) {
    users = getUsersFromGroups(GROUP_EMAIL);
  }

  // For each user, finds events having one or more of the keywords in the event
  // summary in the specified date range. Imports each of those to the team
  // calendar.
  let count = 0;
  for (const user of users) {
    const username = user.getEmail().split("@")[0];
    const events = findEvents(user, today, maxDate, lastRun);
    for (const event of events) {
      importEvent(username, event);
      count++;
    }
  }

  PropertiesService.getScriptProperties().setProperty("lastRun", today);
  console.log(`Imported ${count} events`);
}

/**
 * Imports the given event from the user's calendar into the shared team
 * calendar.
 * @param {string} username The team member that is attending the event.
 * @param {Calendar.Event} event The event to import.
 */
function importEvent(username, event) {
  event.summary = `[${username}] ${event.summary}`;
  event.organizer = {
    id: TEAM_CALENDAR_ID,
  };
  event.attendees = [];

  // If the event is not of type 'default', it can't be imported, so it needs
  // to be changed.
  if (event.eventType !== "default") {
    event.eventType = "default";
    event.outOfOfficeProperties = undefined;
    event.focusTimeProperties = undefined;
  }

  console.log("Importing: %s", event.summary);
  try {
    Calendar.Events.import(event, TEAM_CALENDAR_ID);
  } catch (e) {
    console.error(
      "Error attempting to import event: %s. Skipping.",
      e.toString(),
    );
  }
}

/**
 * In a given user's calendar, looks for occurrences of the given keyword
 * in events within the specified date range and returns any such events
 * found.
 * @param {Session.User} user The user to retrieve events for.
 * @param {string} keyword The keyword to look for.
 * @param {Date} start The starting date of the range to examine.
 * @param {Date} end The ending date of the range to examine.
 * @param {Date} optSince A date indicating the last time this script was run.
 * @return {Calendar.Event[]} An array of calendar events.
 */
function findEvents(user, start, end, optSince) {
  const params = {
    eventTypes: "outOfOffice",
    timeMin: formatDateAsRFC3339(start),
    timeMax: formatDateAsRFC3339(end),
    showDeleted: true,
  };
  if (optSince) {
    // This prevents the script from examining events that have not been
    // modified since the specified date (that is, the last time the
    // script was run).
    params.updatedMin = formatDateAsRFC3339(optSince);
  }
  let pageToken = null;
  let events = [];
  do {
    params.pageToken = pageToken;
    let response;
    try {
      response = Calendar.Events.list(user.getEmail(), params);
    } catch (e) {
      console.error(
        "Error retriving events for %s, %s: %s; skipping",
        user,
        keyword,
        e.toString(),
      );
      continue;
    }
    events = events.concat(response.items);
    pageToken = response.nextPageToken;
  } while (pageToken);
  return events;
}

/**
 * Returns an RFC3339 formated date String corresponding to the given
 * Date object.
 * @param {Date} date a Date.
 * @return {string} a formatted date string.
 */
function formatDateAsRFC3339(date) {
  return Utilities.formatDate(date, "UTC", "yyyy-MM-dd'T'HH:mm:ssZ");
}

/**
 * Get both direct and indirect members (and delete duplicates).
 * @param {string} the e-mail address of the group.
 * @return {object} direct and indirect members.
 */
function getAllMembers(groupEmail) {
  const group = GroupsApp.getGroupByEmail(groupEmail);
  let users = group.getUsers();
  const childGroups = group.getGroups();
  for (let i = 0; i < childGroups.length; i++) {
    const childGroup = childGroups[i];
    users = users.concat(getAllMembers(childGroup.getEmail()));
  }
  // Remove duplicate members
  const uniqueUsers = [];
  const userEmails = {};
  for (let i = 0; i < users.length; i++) {
    const user = users[i];
    if (!userEmails[user.getEmail()]) {
      uniqueUsers.push(user);
      userEmails[user.getEmail()] = true;
    }
  }
  return uniqueUsers;
}

/**
 * Get indirect members from multiple groups (and delete duplicates).
 * @param {array} the e-mail addresses of multiple groups.
 * @return {object} indirect members of multiple groups.
 */
function getUsersFromGroups(groupEmails) {
  const users = [];
  for (const groupEmail of groupEmails) {
    const groupUsers = GroupsApp.getGroupByEmail(groupEmail).getUsers();
    for (const user of groupUsers) {
      if (!users.some((u) => u.getEmail() === user.getEmail())) {
        users.push(user);
      }
    }
  }
  return users;
}

পরিবর্তন

আপনার প্রয়োজন অনুসারে আপনি যত খুশি টিম ভ্যাকেশন ক্যালেন্ডার অটোমেশন সম্পাদনা করতে পারেন। ট্রিগার পরিবর্তন করার জন্য নীচে একটি ঐচ্ছিক পরিবর্তন দেওয়া হল।

নতুন ইভেন্টের জন্য স্ক্রিপ্ট কত ঘন ঘন স্ক্যান করবে তা পরিবর্তন করুন।

স্ক্রিপ্টটি কতবার চলবে তা পরিবর্তন করতে, নিম্নলিখিত পদক্ষেপগুলি গ্রহণ করুন:

  1. অ্যাপস স্ক্রিপ্ট প্রজেক্টে, ট্রিগারস ক্লিক করুন।
  2. ট্রিগারের পাশে, সম্পাদনা করুন ট্রিগার ক্লিক করুন।
  3. আপনার পরিবর্তনগুলি নির্বাচন করুন এবং সংরক্ষণ করুন এ ক্লিক করুন।

অবদানকারীরা

এই নমুনাটি গুগল ডেভেলপার বিশেষজ্ঞদের সহায়তায় গুগল দ্বারা রক্ষণাবেক্ষণ করা হয়।

পরবর্তী পদক্ষেপ