خلاصه کردن داده ها از چندین برگه
با مجموعهها، منظم بمانید
ذخیره و طبقهبندی محتوا براساس اولویتهای شما.
سطح کدنویسی : مبتدی
مدت زمان : 5 دقیقه
نوع پروژه : تابع سفارشی
اهداف
- درک کنید که راه حل چه کاری انجام می دهد.
- آنچه را که سرویس های Apps Script در راه حل انجام می دهند، بدانید.
- اسکریپت را تنظیم کنید
- اسکریپت را اجرا کنید.
در مورد این راه حل
اگر دادههای ساختاری مشابهی در چندین صفحه در یک صفحه گسترده دارید، مانند معیارهای پشتیبانی مشتری برای اعضای تیم، میتوانید از این تابع سفارشی برای ایجاد خلاصهای از هر صفحه استفاده کنید. این راه حل بر روی بلیط های پشتیبانی مشتری تمرکز دارد، اما شما می توانید آن را مطابق با نیازهای خود سفارشی کنید.


چگونه کار می کند
تابع سفارشی که getSheetsData()
نامیده می شود، داده های هر صفحه در صفحه گسترده را بر اساس ستون وضعیت یک برگه خلاصه می کند. این اسکریپت برگه هایی را که نباید در انبوه گنجانده شود، نادیده می گیرد، مانند برگه های ReadMe و Summary .
خدمات اسکریپت برنامه ها
این راه حل از سرویس زیر استفاده می کند:
- سرویس صفحه گسترده - برگه هایی را دریافت می کند که باید خلاصه شوند و تعداد مواردی را که با یک رشته مشخص مطابقت دارند را می شمارد. سپس، اسکریپت اطلاعات محاسبه شده را به محدوده ای نسبت به جایی که تابع سفارشی در صفحه گسترده فراخوانی شده است، اضافه می کند.
پیش نیازها
برای استفاده از این نمونه به پیش نیازهای زیر نیاز دارید:
- یک حساب Google (حسابهای Google Workspace ممکن است به تأیید سرپرست نیاز داشته باشند).
- یک مرورگر وب با دسترسی به اینترنت.
اسکریپت را تنظیم کنید
روی دکمه زیر کلیک کنید تا یک کپی از صفحه گسترده عملکرد سفارشی داده های صفحه گسترده خلاصه کنید. پروژه Apps Script برای این راه حل به صفحه گسترده پیوست شده است.
یک کپی تهیه کنید
اسکریپت را اجرا کنید
- در صفحهگسترده کپی شده خود، به صفحه خلاصه بروید.
- روی سلول
A4
کلیک کنید. تابع getSheetsData()
در این سلول است. - به یکی از برگه های مالک بروید و داده ها را به صفحه به روز کنید یا به آن اضافه کنید. برخی از اقداماتی که می توانید امتحان کنید شامل موارد زیر است:
- یک ردیف جدید با نمونه اطلاعات بلیط اضافه کنید.
- در ستون وضعیت ، وضعیت یک بلیط موجود را تغییر دهید.
- موقعیت ستون Status را تغییر دهید. به عنوان مثال، در برگه Owner1 ، ستون Status را از ستون C به ستون D منتقل کنید.
- به برگه Summary بروید و جدول خلاصه به روز شده ای را که
getSheetsData()
از سلول A4
ایجاد کرده است مرور کنید. ممکن است لازم باشد چک باکس ردیف 10 را علامت بزنید تا نتایج ذخیره شده در حافظه پنهان تابع سفارشی بازخوانی شود . گوگل توابع سفارشی را برای بهینه سازی عملکرد ذخیره می کند.- اگر ردیفهایی را اضافه کرده یا بهروزرسانی کردهاید، اسکریپت بلیط و وضعیت را بهروزرسانی میکند.
- اگر موقعیت ستون وضعیت را جابجا کرده باشید، اسکریپت همچنان همانطور که در فهرست ستون جدید در نظر گرفته شده است کار می کند.
کد را مرور کنید
برای بررسی کد Apps Script برای این راه حل، روی مشاهده کد منبع در زیر کلیک کنید:
اصلاحات
شما می توانید تابع سفارشی را تا جایی که می خواهید متناسب با نیاز خود ویرایش کنید. در زیر یک افزونه اختیاری برای بازخوانی دستی نتایج عملکرد سفارشی وجود دارد.
نتایج ذخیره شده را بازخوانی کنید
برخلاف توابع داخلی، گوگل توابع سفارشی را برای بهینه سازی عملکرد در حافظه پنهان ذخیره می کند. این بدان معنی است که اگر چیزی را در تابع سفارشی خود تغییر دهید، مانند مقداری که در حال محاسبه است، ممکن است فوراً مجبور به به روز رسانی نشود. برای بازخوانی دستی نتیجه عملکرد، مراحل زیر را انجام دهید:
- با کلیک کردن روی Insert > Checkbox یک کادر انتخاب را به یک سلول خالی اضافه کنید.
- سلولی را که دارای چک باکس است به عنوان پارامتر تابع سفارشی اضافه کنید، به عنوان مثال،
getSheetsData(B11)
. - برای بازخوانی نتایج عملکرد سفارشی، کادر انتخاب را علامت بزنید یا علامت آن را بردارید.
مشارکت کنندگان
این نمونه توسط گوگل با کمک کارشناسان توسعه دهنده گوگل نگهداری می شود.
مراحل بعدی
جز در مواردی که غیر از این ذکر شده باشد،محتوای این صفحه تحت مجوز Creative Commons Attribution 4.0 License است. نمونه کدها نیز دارای مجوز Apache 2.0 License است. برای اطلاع از جزئیات، به خطمشیهای سایت Google Developers مراجعه کنید. جاوا علامت تجاری ثبتشده Oracle و/یا شرکتهای وابسته به آن است.
تاریخ آخرین بهروزرسانی 2025-08-29 بهوقت ساعت هماهنگ جهانی.
[null,null,["تاریخ آخرین بهروزرسانی 2025-08-29 بهوقت ساعت هماهنگ جهانی."],[[["\u003cp\u003eThis custom function, \u003ccode\u003egetSheetsData()\u003c/code\u003e, summarizes data from multiple sheets in a spreadsheet, excluding "ReadMe" and "Summary" sheets, based on the "Status" column.\u003c/p\u003e\n"],["\u003cp\u003eThe function utilizes the Spreadsheet service to retrieve sheet data, count items matching specific statuses, and display the summarized information in a table.\u003c/p\u003e\n"],["\u003cp\u003eUsers need a Google Account and a web browser to use this solution; to set it up, they should make a copy of the provided spreadsheet containing the script.\u003c/p\u003e\n"],["\u003cp\u003eTo run the script, users navigate to the "Summary" sheet, observe the function output, modify data in other sheets, and check for updates on the "Summary" sheet, optionally refreshing the cache if necessary.\u003c/p\u003e\n"]]],["The custom function `getSheetsData()` summarizes data from multiple sheets in a spreadsheet, excluding \"ReadMe\" and \"Summary\" sheets. It analyzes each sheet's \"Status\" column, counts tasks based on status (e.g., Complete, In-Progress), and totals all tasks per sheet. The script uses the Spreadsheet service to get sheet data and then adds the calculated information in a summary format to the location in the spreadsheet where the custom function is entered. It is designed for sheets with similarly structured data.\n"],null,["# Summarize data from multiple sheets\n\n**Coding level** : Beginner \n\n**Duration** : 5 minutes \n\n**Project type** : [Custom function](/apps-script/guides/sheets/functions)\n\nObjectives\n----------\n\n- Understand what the solution does.\n- Understand what the Apps Script services do within the solution.\n- Set up the script.\n- Run the script.\n\nAbout this solution\n-------------------\n\nIf you have similarly structured data on multiple sheets in a spreadsheet, such\nas customer support metrics for team members, you can use this custom function\nto create a summary of each sheet. This solution focuses on customer support\ntickets, but you can customize it to fit your needs.\n\n### How it works\n\nThe custom function, called `getSheetsData()`, summarizes data from each sheet\nin the spreadsheet based on a sheet's **Status** column. The script ignores\nsheets that shouldn't be included in the aggregation, such as the **ReadMe** and\n**Summary** sheets.\n\n### Apps Script services\n\nThis solution uses the following service:\n\n- [Spreadsheet service](/apps-script/reference/spreadsheet)--Gets the sheets that need to be summarized and counts the number of items that match a specified string. Then, the script adds the calculated information to a range relative to where the custom function was called in the spreadsheet.\n\nPrerequisites\n-------------\n\nTo use this sample, you need the following prerequisites:\n\n- A Google Account (Google Workspace accounts might require administrator approval).\n- A web browser with access to the internet.\n\nSet up the script\n-----------------\n\nClick the button below to make a copy of the **Summarize spreadsheet data\ncustom function** spreadsheet. The Apps Script project for\nthis solution is attached to the spreadsheet.\n\n[Make a copy](https://docs.google.com/spreadsheets/d/1ZyhrTm9q98oOWzKgD6OVb31Yc38ODEHINOOjgVwQpCg/copy)\n\nRun the script\n--------------\n\n1. In your copied spreadsheet, go to the **Summary** sheet.\n2. Click cell `A4`. The `getSheetsData()` function is in this cell.\n3. Go to one of the owner sheets and update or add data to the sheet. Some actions you can try include the following:\n - Add a new row with sample ticket information.\n - In the **Status** column, change the status of an existing ticket.\n - Change the position of the **Status** column. For example, in the **Owner1** sheet, move the **Status** column from column C to column D.\n4. Go to the **Summary** sheet and review the updated summary table that `getSheetsData()` created from cell `A4`. You might need to check the checkbox in row 10 to [refresh the cached results](#cache) of the custom function. Google caches custom functions to optimize performance.\n - If you added or updated rows, the script updates the ticket and status counts.\n - If you moved the position of the **Status** column, the script still works as intended with the new column index.\n\nReview the code\n---------------\n\nTo review the Apps Script code for this solution, click **View source code**\nbelow: \n\n#### View source code\n\n### Code.gs\n\n\u003cbr /\u003e\n\nsolutions/custom-functions/summarize-sheets-data/Code.js \n[View on GitHub](https://github.com/googleworkspace/apps-script-samples/blob/main/solutions/custom-functions/summarize-sheets-data/Code.js) \n\n```javascript\n// To learn how to use this script, refer to the documentation:\n// https://developers.google.com/apps-script/samples/custom-functions/summarize-sheets-data\n\n/*\nCopyright 2022 Google LLC\n\nLicensed under the Apache License, Version 2.0 (the \"License\");\nyou may not use this file except in compliance with the License.\nYou may obtain a copy of the License at\n\n https://www.apache.org/licenses/LICENSE-2.0\n\nUnless required by applicable law or agreed to in writing, software\ndistributed under the License is distributed on an \"AS IS\" BASIS,\nWITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\nSee the License for the specific language governing permissions and\nlimitations under the License.\n*/\n\n/**\n * 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.\n * \n * @return {string} Summary data from other sheets.\n * @customfunction\n */\n\n// The following sheets are ignored. Add additional constants for other sheets that should be ignored.\nconst READ_ME_SHEET_NAME = \"ReadMe\";\nconst PM_SHEET_NAME = \"Summary\";\n\n/**\n * Reads data ranges for each sheet. Filters and counts based on 'Status' columns. To improve performance, the script uses arrays \n * until all summary data is gathered. Then the script writes the summary array starting at the cell of the custom function.\n */\nfunction getSheetsData() {\n let ss = SpreadsheetApp.getActiveSpreadsheet();\n let sheets = ss.getSheets();\n let outputArr = [];\n\n // For each sheet, summarizes the data and pushes to a temporary array.\n for (let s in sheets) {\n // Gets sheet name.\n let sheetNm = sheets[s].getName();\n // Skips ReadMe and Summary sheets.\n if (sheetNm === READ_ME_SHEET_NAME || sheetNm === PM_SHEET_NAME) { continue; }\n // Gets sheets data.\n let values = sheets[s].getDataRange().getValues();\n // Gets the first row of the sheet which is the header row.\n let headerRowValues = values[0];\n // Finds the columns with the heading names 'Owner Name' and 'Status' and gets the index value of each.\n // Using 'indexOf()' to get the position of each column prevents the script from breaking if the columns change positions in a sheet.\n let columnOwner = headerRowValues.indexOf(\"Owner Name\");\n let columnStatus = headerRowValues.indexOf(\"Status\");\n // Removes header row.\n values.splice(0,1);\n // Gets the 'Owner Name' column value by retrieving the first data row in the array.\n let owner = values[0][columnOwner];\n // Counts the total number of tasks.\n let taskCnt = values.length;\n // Counts the number of tasks that have the 'Complete' status.\n // If the options you want to count in your spreadsheet differ, update the strings below to match the text of each option.\n // To add more options, copy the line below and update the string to the new text.\n let completeCnt = filterByPosition(values,'Complete', columnStatus).length;\n // Counts the number of tasks that have the 'In-Progress' status.\n let inProgressCnt = filterByPosition(values,'In-Progress', columnStatus).length;\n // Counts the number of tasks that have the 'Scheduled' status.\n let scheduledCnt = filterByPosition(values,'Scheduled', columnStatus).length;\n // Counts the number of tasks that have the 'Overdue' status.\n let overdueCnt = filterByPosition(values,'Overdue', columnStatus).length;\n // Builds the output array.\n outputArr.push([owner,taskCnt,completeCnt,inProgressCnt,scheduledCnt,overdueCnt,sheetNm]);\n }\n // Writes the output array.\n return outputArr;\n}\n\n/**\n * Below is a helper function that filters a 2-dimenstional array.\n */\nfunction filterByPosition(array, find, position) {\n return array.filter(innerArray =\u003e innerArray[position] === find);\n}\n```\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\nModifications\n-------------\n\nYou can edit the custom function as much as you'd like to fit your needs. Below\nis an optional addition to manually refresh custom function results. \n\n#### Refresh cached results\n\n\nUnlike built-in functions, Google caches custom functions to optimize\nperformance. This means that if you change something within your custom\nfunction, such as a value that's being calculated, it might not immediately\nforce an update. To refresh the function result manually, take the following\nsteps:\n\n1. Add a checkbox to an empty cell by clicking **Insert** \\\u003e **Checkbox**.\n2. Add the cell that has the checkbox as a parameter of the custom function, for example, `getSheetsData(B11)`.\n3. Check or uncheck the checkbox to refresh the custom function results.\n\nContributors\n------------\n\nThis sample is maintained by Google with the help of Google Developer Experts.\n\nNext steps\n----------\n\n- [Custom functions in Google Sheets](/apps-script/guides/sheets/functions)\n- [Extending Google Sheets](/apps-script/guides/sheets)"]]