कर्मचारी के नए उपकरण के अनुरोधों को मैनेज करें

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

मकसद

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

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

नए कर्मचारियों को आम तौर पर, सिस्टम ऐक्सेस करने और आईटी से उपकरण पाने की ज़रूरत होती है. इन अनुरोधों को मैनेज करने के लिए, Google Forms की मदद से एक फ़ॉर्म बनाया जा सकता है. इससे उपयोगकर्ता यह बता पाएंगे कि कर्मचारियों को किस तरह के ऐक्सेस और डिवाइसों की ज़रूरत है. आईटी टीम की ओर से अनुरोध पूरा करने और उसकी स्थिति अपडेट करने के बाद, अनुरोध करने वाले लोगों को ईमेल से सूचना मिलती है.

उपकरण के अनुरोध वाले फ़ॉर्म का स्क्रीनशॉट

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

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

Apps Script की सेवाएं

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

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

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

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

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

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

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

  1. कर्मचारी के लिए उपकरण के अनुरोध मैनेज करें स्प्रेडशीट की कॉपी बनाने के लिए, इस बटन पर क्लिक करें. इस समाधान के लिए Apps Script प्रोजेक्ट, स्प्रैडशीट से अटैच किया गया है.
    कॉपी बनाएं
  2. एक्सटेंशन > Apps Script पर क्लिक करें.
  3. REQUEST_NOTIFICATION_EMAIL वैरिएबल के बगल में, सैंपल ईमेल को अपने ईमेल से बदलें.
  4. सेव करें सेव करने का आइकॉन पर क्लिक करें.

स्प्रेडशीट सेट अप करना

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

  3. उपकरण के अनुरोध > सेट अप करें पर फिर से क्लिक करें.

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

  1. टूल > फ़ॉर्म मैनेज करें > लाइव फ़ॉर्म पर जाएं पर क्लिक करें.
  2. फ़ॉर्म भरें और सबमिट करें.
  3. उपकरण के अनुरोध के बारे में सूचना पाने के लिए, अपना ईमेल देखें.
  4. स्प्रेडशीट पर वापस जाएं और ऐसे अनुरोध जिनके लिए मंज़ूरी मिलना बाकी है वाली शीट में, अनुरोध की स्थिति को पूरा किया गया के तौर पर बदलें.
  5. पांच मिनट के अंदर, स्क्रिप्ट आपको एक और ईमेल भेजती है. इसमें बताया जाता है कि अनुरोध पूरा हो गया है. यह स्क्रिप्ट, अनुरोध को ऐसे अनुरोध जिन पर मंज़ूरी मिलना बाकी है शीट से पूरे किए गए अनुरोध शीट में ले जाती है.

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

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

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

Code.gs

solutions/automations/equipment-requests/Code.js
// To learn how to use this script, refer to the documentation:
// https://developers.google.com/apps-script/samples/automations/equipment-requests

/*
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.
*/

// Update this variable with the email address you want to send equipment requests to.
const REQUEST_NOTIFICATION_EMAIL = "request_intake@example.com";

// Update the following variables with your own equipment options.
const AVAILABLE_LAPTOPS = [
  '15" high Performance Laptop (OS X)',
  '15" high Performance Laptop (Windows)',
  '15" high performance Laptop (Linux)',
  '13" lightweight laptop (Windows)',
];
const AVAILABLE_DESKTOPS = [
  "Standard workstation (Windows)",
  "Standard workstation (Linux)",
  "High performance workstation (Windows)",
  "High performance workstation (Linux)",
  "Mac Pro (OS X)",
];
const AVAILABLE_MONITORS = ['Single 27"', 'Single 32"', 'Dual 24"'];

// Form field titles, used for creating the form and as keys when handling
// responses.
/**
 * Adds a custom menu to the spreadsheet.
 */
function onOpen() {
  SpreadsheetApp.getUi()
    .createMenu("Equipment requests")
    .addItem("Set up", "setup_")
    .addItem("Clean up", "cleanup_")
    .addToUi();
}

/**
 * Creates the form and triggers for the workflow.
 */
function setup_() {
  const ss = SpreadsheetApp.getActiveSpreadsheet();
  if (ss.getFormUrl()) {
    const msg = "Form already exists. Unlink the form and try again.";
    SpreadsheetApp.getUi().alert(msg);
    return;
  }
  const form = FormApp.create("Equipment Requests")
    .setCollectEmail(true)
    .setDestination(FormApp.DestinationType.SPREADSHEET, ss.getId())
    .setLimitOneResponsePerUser(false);
  form.addTextItem().setTitle("Employee name").setRequired(true);
  form.addTextItem().setTitle("Desk location").setRequired(true);
  form.addDateItem().setTitle("Due date").setRequired(true);
  form.addListItem().setTitle("Laptop").setChoiceValues(AVAILABLE_LAPTOPS);
  form.addListItem().setTitle("Desktop").setChoiceValues(AVAILABLE_DESKTOPS);
  form.addListItem().setTitle("Monitor").setChoiceValues(AVAILABLE_MONITORS);

  // Hide the raw form responses.
  for (const sheet of ss.getSheets()) {
    if (sheet.getFormUrl() === ss.getFormUrl()) {
      sheet.hideSheet();
    }
  }
  // Start workflow on each form submit
  ScriptApp.newTrigger("onFormSubmit_").forForm(form).onFormSubmit().create();
  // Archive completed items every 5m.
  ScriptApp.newTrigger("processCompletedItems_")
    .timeBased()
    .everyMinutes(5)
    .create();
}

/**
 * Cleans up the project (stop triggers, form submission, etc.)
 */
function cleanup_() {
  const formUrl = SpreadsheetApp.getActiveSpreadsheet().getFormUrl();
  if (!formUrl) {
    return;
  }
  for (const trigger of ScriptApp.getProjectTriggers()) {
    ScriptApp.deleteTrigger(trigger);
  }
  FormApp.openByUrl(formUrl).deleteAllResponses().setAcceptingResponses(false);
}

/**
 * Handles new form submissions to trigger the workflow.
 *
 * @param {Object} event - Form submit event
 */
function onFormSubmit_(event) {
  const response = mapResponse_(event.response);
  sendNewEquipmentRequestEmail_(response);
  const equipmentDetails = Utilities.formatString(
    "%s\n%s\n%s",
    response.Laptop,
    response.Desktop,
    response.Monitor,
  );
  const row = [
    "New",
    "",
    response["Due date"],
    response["Employee name"],
    response["Desk location"],
    equipmentDetails,
    response.email,
  ];
  const ss = SpreadsheetApp.getActiveSpreadsheet();
  const sheet = ss.getSheetByName("Pending requests");
  sheet.appendRow(row);
}

/**
 * Sweeps completed and cancelled requests, notifying the requestors and archiving them
 * to the completed sheet.
 *
 * @param {Object} event
 */
function processCompletedItems_() {
  const ss = SpreadsheetApp.getActiveSpreadsheet();
  const pending = ss.getSheetByName("Pending requests");
  const completed = ss.getSheetByName("Completed requests");
  const rows = pending.getDataRange().getValues();
  for (let i = rows.length; i >= 2; i--) {
    const row = rows[i - 1];
    const status = row[0];
    if (status === "Completed" || status === "Cancelled") {
      pending.deleteRow(i);
      completed.appendRow(row);
      console.log(`Deleted row: ${i}`);
      sendEquipmentRequestCompletedEmail_({
        "Employee name": row[3],
        "Desk location": row[4],
        email: row[6],
      });
    }
  }
}

/**
 * Sends an email notification that a new equipment request has been submitted.
 *
 * @param {Object} request - Request details
 */
function sendNewEquipmentRequestEmail_(request) {
  const template = HtmlService.createTemplateFromFile(
    "new-equipment-request.html",
  );
  template.request = request;
  template.sheetUrl = SpreadsheetApp.getActiveSpreadsheet().getUrl();
  const msg = template.evaluate();
  MailApp.sendEmail({
    to: REQUEST_NOTIFICATION_EMAIL,
    subject: "New equipment request",
    htmlBody: msg.getContent(),
  });
}

/**
 * Sends an email notifying the requestor that the request is complete.
 *
 * @param {Object} request - Request details
 */
function sendEquipmentRequestCompletedEmail_(request) {
  const template = HtmlService.createTemplateFromFile("request-complete.html");
  template.request = request;
  const msg = template.evaluate();
  MailApp.sendEmail({
    to: request.email,
    subject: "Equipment request completed",
    htmlBody: msg.getContent(),
  });
}

/**
 * Converts a form response to an object keyed by the item titles. Allows easier
 * access to response values.
 *
 * @param {FormResponse} response
 * @return {Object} Form values keyed by question title
 */
function mapResponse_(response) {
  const initialValue = {
    email: response.getRespondentEmail(),
    timestamp: response.getTimestamp(),
  };
  return response.getItemResponses().reduce((obj, itemResponse) => {
    const key = itemResponse.getItem().getTitle();
    obj[key] = itemResponse.getResponse();
    return obj;
  }, initialValue);
}

new-equipment-request.html

solutions/automations/equipment-requests/new-equipment-request.html
<!DOCTYPE html>
<!--
 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

      http://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.
-->

<html>
  <body>
    <p>
    A new equipment request has been made by <?= request.email ?>.
    </p>

    <p>
    Employee name: <?= request['Employee name'] ?><br/>
    Desk location name: <?= request['Desk location'] ?><br/>
    Due date: <?= request['Due date'] ?><br/>
    Laptop model: <?= request['Laptop'] ?><br/>
    Desktop model: <?= request['Desktop'] ?><br/>
    Monitor(s): <?= request['Monitor'] ?><br/>
    </p>

    See <a href="<?= sheetUrl ?>">the spreadsheet</a> to take or assign this item.
  </body>
</html>

request-complete.html

solutions/automations/equipment-requests/request-complete.html
<!DOCTYPE html>
<!--
 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

      http://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.
-->

<html>
  <body>
    <p>
    An equipment request has been completed.
    </p>

    <p>
    Employee name: <?= request['Employee name'] ?><br/>
    Desk location name: <?= request['Desk location'] ?><br/>
    </p>
  </body>
</html>

योगदानकर्ता

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

अगले चरण