टीम के साथ छुट्टियों के कैलेंडर को पॉप्युलेट करना

कोडिंग का लेवल: शुरुआती
अवधि: 15 मिनट
प्रोजेक्ट का टाइप: समय के हिसाब से ट्रिगर होने वाली सुविधा का इस्तेमाल करके ऑटोमेशन

मकसद

  • समझें कि समाधान क्या करता है.
  • समझें कि Apps Script की सेवाएं, समाधान में क्या करती हैं.
  • स्क्रिप्ट सेट अप करें.
  • स्क्रिप्ट चलाएं.

इस समाधान के बारे में जानकारी

छुट्टियों का शेयर किया गया कैलेंडर, टीम के सदस्यों को एक साथ मिलकर काम करने में मदद करने वाला एक बेहतरीन टूल है; इससे कोई भी व्यक्ति एक नज़र में यह पता लगा सकता है कि कौन छुट्टी पर है. इस समाधान की मदद से, आपको यह पता चल सकता है कि आपके सहकर्मी कब ऑफ़िस में नहीं हैं. इसके लिए, आपको मैन्युअल तरीके से जानकारी डालने की ज़रूरत नहीं होती.

छुट्टी वाले कैलेंडर का उदाहरण

यह कैसे काम करता है

यह समाधान, Google ग्रुप में शामिल हर व्यक्ति के कैलेंडर के आधार पर, शेयर किया गया छुट्टियों का कैलेंडर बनाता है. जब कोई व्यक्ति छुट्टी बुक करता है, तो वह अपने निजी Google Calendar में एक इवेंट जोड़ता है. इसके लिए, वह "छुट्टी" या "ऑफिस से बाहर" जैसे कीवर्ड का इस्तेमाल करता है.

यह स्क्रिप्ट हर घंटे, ग्रुप के सदस्यों के कैलेंडर स्कैन करती है. साथ ही, काम के इवेंट को शेयर किए गए कैलेंडर में सिंक करती है. आपके पास यह बदलने का विकल्प होता है कि स्क्रिप्ट कितनी बार नए इवेंट के लिए स्कैन करे.

यह समाधान सिर्फ़ उन Calendar इवेंट को ऐक्सेस करता है जिन्हें आपके सहयोगियों ने निजता सेटिंग के ज़रिए आपके लिए दिखने की अनुमति दी है.

Apps Script की सेवाएं

इस समाधान में इन सेवाओं का इस्तेमाल किया जाता है:

  • Groups सेवा–इससे Google ग्रुप के सदस्यों के बारे में पता चलता है.
  • Calendar की ऐडवांस सेवा–इससे Google Calendar API को ऐक्सेस किया जा सकता है. साथ ही, ग्रुप के सदस्यों के कैलेंडर पर इवेंट खोजे जा सकते हैं.

ज़रूरी शर्तें

इस सैंपल का इस्तेमाल करने के लिए, आपको इन ज़रूरी शर्तों को पूरा करना होगा:

  • Google खाता (Google Workspace खातों के लिए, एडमिन की अनुमति ज़रूरी हो सकती है).
  • इंटरनेट ऐक्सेस करने की सुविधा वाला वेब ब्राउज़र.

स्क्रिप्ट सेट अप करना

टीम की छुट्टियों का कैलेंडर बनाना

  1. Google Calendar खोलें
  2. "टीम की छुट्टियां" नाम का नया कैलेंडर बनाएं.
  3. कैलेंडर की सेटिंग में जाकर, कैलेंडर जोड़ें में मौजूद कैलेंडर आईडी को कॉपी करें.

Apps Script प्रोजेक्ट बनाना

  1. छुट्टियों का कैलेंडर Apps Script प्रोजेक्ट खोलने के लिए, इस बटन पर क्लिक करें.
    प्रोजेक्ट खोलें
  2. खास जानकारी पर क्लिक करें.
  3. खास जानकारी वाले पेज पर, कॉपी बनाएं कॉपी बनाने का आइकॉन पर क्लिक करें.
  4. Apps Script के कॉपी किए गए प्रोजेक्ट में, TEAM_CALENDAR_ID वैरिएबल को उस कैलेंडर का आईडी सेट करें जिसे आपने पहले बनाया था.
  5. वैरिएबल GROUP_EMAIL को उस Google ग्रुप के ईमेल पते पर सेट करें जिसमें आपकी टीम के सदस्य शामिल हैं.
  6. सेवाएं के बगल में, सेवा जोड़ें पर क्लिक करें.
  7. Google Calendar API को चुनें और जोड़ें पर क्लिक करें.

स्क्रिप्ट चलाना

  1. कॉपी किए गए Apps Script प्रोजेक्ट में, फ़ंक्शन ड्रॉपडाउन में जाकर setup चुनें.
  2. चलाएं पर क्लिक करें.
  3. जब आपसे कहा जाए, तब स्क्रिप्ट को अनुमति दें. अगर OAuth की सहमति वाली स्क्रीन पर, इस ऐप्लिकेशन की पुष्टि नहीं हुई है चेतावनी दिखती है, तो ऐडवांस > {Project Name} पर जाएं (सुरक्षित नहीं है) को चुनकर जारी रखें.

  4. यह प्रोसेस पूरी होने के बाद, Calendar पर वापस जाएं और पुष्टि करें कि टीम की छुट्टियों वाले कैलेंडर में इवेंट दिख रहे हैं.

कोड की समीक्षा करना

इस समाधान के लिए, Apps Script कोड की समीक्षा करने के लिए, यहां दिए गए सोर्स कोड देखें पर क्लिक करें:

सोर्स कोड देखें

Code.gs

solutions/automations/vacation-calendar/Code.js
// 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. Apps Script प्रोजेक्ट में, ट्रिगर पर क्लिक करें.
  2. ट्रिगर के बगल में, ट्रिगर में बदलाव करें पर क्लिक करें.
  3. बदलाव चुनें और सेव करें पर क्लिक करें.

योगदानकर्ता

इस सैंपल को Google, Google Developer Experts की मदद से मैनेज करता है.

अगले चरण