خلاصه کردن داده ها از چندین برگه

سطح کدنویسی : مبتدی
مدت زمان : ۵ دقیقه
نوع پروژه : تابع سفارشی

اهداف

  • بفهمید که راه حل چه کاری انجام می‌دهد.
  • درک کنید که سرویس‌های Apps Script در این راهکار چه کاری انجام می‌دهند.
  • اسکریپت را تنظیم کنید.
  • اسکریپت را اجرا کنید.

درباره این راهکار

اگر داده‌های ساختاریافته‌ی مشابهی در چندین برگه در یک صفحه گسترده دارید، مانند معیارهای پشتیبانی مشتری برای اعضای تیم، می‌توانید از این تابع سفارشی برای ایجاد خلاصه‌ای از هر برگه استفاده کنید. این راهکار بر روی تیکت‌های پشتیبانی مشتری تمرکز دارد، اما می‌توانید آن را متناسب با نیازهای خود سفارشی کنید.

تصویری از خروجی تابع getSheetsData

چگونه کار می‌کند؟

تابع سفارشی که getSheetsData() نام دارد، داده‌های هر برگه در صفحه گسترده را بر اساس ستون وضعیت آن برگه خلاصه می‌کند. این اسکریپت برگه‌هایی را که نباید در تجمیع گنجانده شوند، مانند برگه‌های ReadMe و Summary ، نادیده می‌گیرد.

سرویس‌های اسکریپت برنامه‌ها

این راهکار از سرویس زیر استفاده می‌کند:

  • سرویس صفحه گسترده - صفحاتی را که باید خلاصه شوند دریافت می‌کند و تعداد مواردی را که با یک رشته مشخص مطابقت دارند، شمارش می‌کند. سپس، اسکریپت اطلاعات محاسبه شده را به محدوده‌ای نسبت به جایی که تابع سفارشی در صفحه گسترده فراخوانی شده است، اضافه می‌کند.

پیش‌نیازها

برای استفاده از این نمونه، به پیش‌نیازهای زیر نیاز دارید:

  • یک حساب گوگل (حساب‌های کاربری گوگل ورک‌اسپیس ممکن است نیاز به تأیید مدیر داشته باشند).
  • یک مرورگر وب با دسترسی به اینترنت.

اسکریپت را تنظیم کنید

برای ایجاد یک کپی از صفحه گسترده تابع سفارشی Summarize spreadsheet data ، روی دکمه زیر کلیک کنید. پروژه Apps Script برای این راهکار به صفحه گسترده پیوست شده است.
یک کپی تهیه کنید

اسکریپت را اجرا کنید

  1. در صفحه گسترده کپی شده خود، به برگه خلاصه (Summary Sheet) بروید.
  2. روی سلول A4 کلیک کنید. تابع getSheetsData() در این سلول قرار دارد.
  3. به یکی از برگه‌های مالک بروید و داده‌ها را به برگه به‌روزرسانی یا اضافه کنید. برخی از اقداماتی که می‌توانید امتحان کنید شامل موارد زیر است:
    • یک ردیف جدید با اطلاعات نمونه بلیط اضافه کنید.
    • در ستون وضعیت ، وضعیت یک تیکت موجود را تغییر دهید.
    • موقعیت ستون وضعیت (Status) را تغییر دهید. برای مثال، در شیت Owner1 ، ستون وضعیت (Status) را از ستون C به ستون D منتقل کنید.
  4. به برگه خلاصه بروید و جدول خلاصه به‌روزرسانی‌شده‌ای را که getSheetsData() از سلول A4 ایجاد کرده است، بررسی کنید. ممکن است لازم باشد کادر انتخاب ردیف 10 را علامت بزنید تا نتایج ذخیره‌شده تابع سفارشی به‌روزرسانی شود . گوگل توابع سفارشی را برای بهینه‌سازی عملکرد ذخیره می‌کند.
    • اگر ردیف‌هایی را اضافه یا به‌روزرسانی کرده باشید، اسکریپت تعداد تیکت‌ها و وضعیت‌ها را به‌روزرسانی می‌کند.
    • اگر موقعیت ستون وضعیت را تغییر داده باشید، اسکریپت همچنان با اندیس ستون جدید طبق انتظار عمل می‌کند.

کد را مرور کنید

برای بررسی کد Apps Script برای این راهکار، روی مشاهده کد منبع در زیر کلیک کنید:

مشاهده کد منبع

کد.gs

راهکارها/توابع-سفارشی/خلاصه-برگه‌ها-داده‌ها/کد.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);
}

اصلاحات

شما می‌توانید تابع سفارشی را تا جایی که می‌خواهید متناسب با نیازهایتان ویرایش کنید. در زیر یک گزینه اختیاری برای به‌روزرسانی دستی نتایج تابع سفارشی وجود دارد.

نتایج ذخیره شده در حافظه پنهان را تازه کنید

برخلاف توابع داخلی، گوگل توابع سفارشی را برای بهینه‌سازی عملکرد، کش می‌کند. این بدان معناست که اگر چیزی را در تابع سفارشی خود تغییر دهید، مانند مقداری که در حال محاسبه است، ممکن است بلافاصله به‌روزرسانی نشود. برای به‌روزرسانی دستی نتیجه تابع، مراحل زیر را انجام دهید:

  1. با کلیک روی Insert > Checkbox ، یک چک‌باکس به یک سلول خالی اضافه کنید.
  2. سلولی که کادر انتخاب را به عنوان پارامتر تابع سفارشی دارد، مثلاً getSheetsData(B11) اضافه کنید.
  3. برای به‌روزرسانی نتایج تابع سفارشی، کادر انتخاب را علامت بزنید یا علامت آن را بردارید.

مشارکت‌کنندگان

این نمونه توسط گوگل و با کمک متخصصان توسعه‌دهنده گوگل نگهداری می‌شود.

مراحل بعدی