एक से ज़्यादा शीट के डेटा की खास जानकारी देखना

कोडिंग का लेवल: शुरुआत की तरह
कुल समय: 5 मिनट
प्रोजेक्ट टाइप: कस्टम फ़ंक्शन

मकसद

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

इस सलूशन के बारे में जानकारी

अगर स्प्रेडशीट में एक से ज़्यादा शीट पर इसी तरह का स्ट्रक्चर्ड डेटा है, जैसे कि टीम के सदस्यों के लिए ग्राहक सहायता मेट्रिक, तो हर शीट की खास जानकारी बनाने के लिए इस कस्टम फ़ंक्शन का इस्तेमाल किया जा सकता है. यह समाधान ग्राहक सहायता टिकटों पर केंद्रित है लेकिन आप इसे अपनी ज़रूरतों के हिसाब से बना सकते हैं.

getSheetsData फ़ंक्शन के आउटपुट का स्क्रीनशॉट

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

कस्टम फ़ंक्शन, जिसे getSheetsData() कहा जाता है, स्प्रेडशीट में मौजूद हर शीट के डेटा की खास जानकारी दिखाता है. यह डेटा शीट के स्टेटस कॉलम के आधार पर तय होता है. स्क्रिप्ट उन शीट को अनदेखा कर देती है जिन्हें एग्रीगेशन में शामिल नहीं करना चाहिए. जैसे, ReadMe और Summary शीट.

Apps Script सेवाएं

यह समाधान इस सेवा का इस्तेमाल करता है:

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

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

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

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

स्क्रिप्ट सेट अप करें

स्प्रेडशीट के डेटा के बारे में खास जानकारी दें कस्टम फ़ंक्शन वाली स्प्रेडशीट की कॉपी बनाने के लिए, नीचे दिए गए बटन पर क्लिक करें. इस समाधान का Apps Script प्रोजेक्ट, स्प्रेडशीट में अटैच किया गया है.
कॉपी बनाएं

स्क्रिप्ट चलाएं

  1. कॉपी की गई स्प्रेडशीट में, खास जानकारी शीट पर जाएं.
  2. सेल A4 पर क्लिक करें. इस सेल में getSheetsData() फ़ंक्शन है.
  3. किसी एक मालिक शीट पर जाएं और शीट में डेटा अपडेट करें या जोड़ें. कुछ ऐसी कार्रवाइयां जिन्हें आज़माया जा सकता है, उनमें ये शामिल हैं:
    • टिकट की जानकारी के नमूने के साथ एक नई लाइन जोड़ें.
    • स्थिति कॉलम में, किसी मौजूदा टिकट का स्टेटस बदलें.
    • स्थिति कॉलम की जगह बदलें. उदाहरण के लिए, Owner1 शीट में, स्टेटस कॉलम को कॉलम C से कॉलम D में ले जाएं.
  4. खास जानकारी शीट पर जाएं और अपडेट की गई खास जानकारी वाली उस टेबल की समीक्षा करें जिसे getSheetsData() सेल A4 से बनाया गया है. कस्टम फ़ंक्शन के कैश मेमोरी में सेव किए गए नतीजों को रीफ़्रेश करने के लिए, आपको पंक्ति 10 में मौजूद चेकबॉक्स को चुनना पड़ सकता है. Google, परफ़ॉर्मेंस को ऑप्टिमाइज़ करने के लिए, कस्टम फ़ंक्शन को कैश मेमोरी में सेव करता है.
    • अगर आपने लाइनें जोड़ी या अपडेट की हैं, तो स्क्रिप्ट टिकट और स्टेटस की गिनती अपडेट कर देती है.
    • अगर आपने स्थिति कॉलम की जगह बदल दी है, तो भी स्क्रिप्ट नए कॉलम इंडेक्स के साथ उम्मीद के मुताबिक काम करती है.

कोड की समीक्षा करें

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

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

Code.gs

solutions/custom-functions/summarize-sheets-data/Code.js
// To learn how to use this script, refer to the documentation:
// https://developers.google.com/apps-script/samples/custom-functions/summarize-sheets-data

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

/**
 * Gets summary data from other sheets. The sheets you want to summarize must have columns with headers that match the names of the columns this function summarizes data from.
 * 
 * @return {string} Summary data from other sheets.
 * @customfunction
 */

// The following sheets are ignored. Add additional constants for other sheets that should be ignored.
const READ_ME_SHEET_NAME = "ReadMe";
const PM_SHEET_NAME = "Summary";

/**
 * Reads data ranges for each sheet. Filters and counts based on 'Status' columns. To improve performance, the script uses arrays 
 * until all summary data is gathered. Then the script writes the summary array starting at the cell of the custom function.
 */
function getSheetsData() {
  let ss = SpreadsheetApp.getActiveSpreadsheet();
  let sheets = ss.getSheets();
  let outputArr = [];

  // For each sheet, summarizes the data and pushes to a temporary array.
  for (let s in sheets) {
    // Gets sheet name.
    let sheetNm = sheets[s].getName();
    // Skips ReadMe and Summary sheets.
    if (sheetNm === READ_ME_SHEET_NAME || sheetNm === PM_SHEET_NAME) { continue; }
    // Gets sheets data.
    let values = sheets[s].getDataRange().getValues();
    // Gets the first row of the sheet which is the header row.
    let headerRowValues = values[0];
    // Finds the columns with the heading names 'Owner Name' and 'Status' and gets the index value of each.
    // Using 'indexOf()' to get the position of each column prevents the script from breaking if the columns change positions in a sheet.
    let columnOwner = headerRowValues.indexOf("Owner Name");
    let columnStatus = headerRowValues.indexOf("Status");
    // Removes header row.
    values.splice(0,1);
    // Gets the 'Owner Name' column value by retrieving the first data row in the array.
    let owner = values[0][columnOwner];
    // Counts the total number of tasks.
    let taskCnt = values.length;
    // Counts the number of tasks that have the 'Complete' status.
    // If the options you want to count in your spreadsheet differ, update the strings below to match the text of each option.
    // To add more options, copy the line below and update the string to the new text.
    let completeCnt = filterByPosition(values,'Complete', columnStatus).length;
    // Counts the number of tasks that have the 'In-Progress' status.
    let inProgressCnt = filterByPosition(values,'In-Progress', columnStatus).length;
    // Counts the number of tasks that have the 'Scheduled' status.
    let scheduledCnt = filterByPosition(values,'Scheduled', columnStatus).length;
    // Counts the number of tasks that have the 'Overdue' status.
    let overdueCnt = filterByPosition(values,'Overdue', columnStatus).length;
    // Builds the output array.
    outputArr.push([owner,taskCnt,completeCnt,inProgressCnt,scheduledCnt,overdueCnt,sheetNm]);
  }
  // Writes the output array.
  return outputArr;
}

/**
 * Below is a helper function that filters a 2-dimenstional array.
 */
function filterByPosition(array, find, position) {
  return array.filter(innerArray => innerArray[position] === find);
}

बदलाव

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

कैश मेमोरी में सेव किए गए नतीजों को रीफ़्रेश करें

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

  1. शामिल करें > चेकबॉक्स पर क्लिक करके, किसी खाली सेल में चेकबॉक्स जोड़ें.
  2. वह सेल जोड़ें जिसमें कस्टम फ़ंक्शन के पैरामीटर के रूप में चेकबॉक्स है, उदाहरण के लिए, getSheetsData(B11).
  3. कस्टम फ़ंक्शन के नतीजों को रीफ़्रेश करने के लिए, चेकबॉक्स पर सही का निशान लगाएं या हटाएं.

योगदानकर्ता

Google, इस सैंपल को मैनेज करने के लिए Google Developer के विशेषज्ञों की मदद लेता है.

अगले चरण