تلخيص البيانات من أوراق متعددة

مستوى البرمجة: مبتدئ
المدة: 5 دقائق
نوع المشروع: دالة مخصّصة

الأهداف

  • فهم ما يفعله الحلّ
  • فهم وظائف خدمات "برمجة تطبيقات Google" ضمن الحل
  • إعداد النص البرمجي
  • شغِّل النص البرمجي.

لمحة عن هذا الحل

إذا كانت لديك بيانات منظَّمة بشكل مشابه في عدّة أوراق ضمن جدول بيانات، مثل مقاييس دعم العملاء لأفراد الفريق، يمكنك استخدام هذه الدالة المخصّصة لإنشاء ملخّص لكل ورقة. يركّز هذا الحلّ على تذاكر دعم العملاء، ولكن يمكنك تخصيصه ليناسب احتياجاتك.

لقطة شاشة لناتج الدالة getSheetsData

آلية العمل

تلخّص الدالة المخصّصة، المسماة getSheetsData()، البيانات من كل ورقة في جدول البيانات استنادًا إلى عمود الحالة في الورقة. يتجاهل النص البرمجي الجداول التي لا يجب تضمينها في التجميع، مثل جدولَي ReadMe وSummary.

خدمات "برمجة تطبيقات Google"

يستخدم هذا الحل الخدمة التالية:

  • خدمة جداول البيانات: للحصول على الأوراق التي يجب تلخيصها وحساب عدد العناصر التي تطابق سلسلة محددة. بعد ذلك، يضيف النص البرمجي المعلومات المحسوبة إلى نطاق مرتبط بالمكان الذي تم فيه استدعاء الدالة المخصّصة في جدول البيانات.

المتطلبات الأساسية

لاستخدام هذا النموذج، يجب استيفاء المتطلبات الأساسية التالية:

  • حساب Google (قد تتطلّب حسابات Google Workspace موافقة المشرف).
  • متصفّح ويب يمكنه الوصول إلى الإنترنت

إعداد النص البرمجي

انقر على الزر أدناه لإنشاء نسخة من جدول بيانات دالة "تلخيص بيانات جدول البيانات" المخصّصة. مشروع "برمجة تطبيقات Google" الخاص بهذا الحلّ مرفق بجدول البيانات.
إنشاء نسخة

تشغيل النص البرمجي

  1. في جدول البيانات الذي نسخته، انتقِل إلى ورقة الملخّص.
  2. انقر على الخلية A4. تحتوي هذه الخلية على الدالة getSheetsData().
  3. انتقِل إلى إحدى أوراق المالك وعدِّل البيانات في الورقة أو أضِفها. في ما يلي بعض الإجراءات التي يمكنك تجربتها:
    • أضِف صفًا جديدًا يتضمّن معلومات نموذجية عن التذكرة.
    • في عمود الحالة، غيِّر حالة تذكرة حالية.
    • غيِّر موضع عمود الحالة. على سبيل المثال، في ورقة المالك1، انقل عمود الحالة من العمود C إلى العمود D.
  4. انتقِل إلى ورقة الملخّص وراجِع جدول الملخّص المعدَّل الذي تم إنشاؤه من الخلية A4.getSheetsData() قد تحتاج إلى وضع علامة في مربّع الاختيار في الصف 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 المطوّرين.

الخطوات التالية