Podsumowywanie danych z wielu arkuszy

Poziom kodowania: początkujący
Czas trwania: 5 minut
Typ projektu: Funkcja niestandardowa

Cele

  • Dowiedz się, do czego służy dane rozwiązanie.
  • dowiedzieć się, jak usługi Apps Script działają w danym rozwiązaniu.
  • Skonfiguruj skrypt.
  • Uruchom skrypt.

Informacje o rozwiązaniu

Jeśli masz podobne uporządkowane dane w wielu arkuszach w arkuszu kalkulacyjnym, np. dane związane z obsługą klienta w przypadku członków zespołu, możesz użyć tej funkcji niestandardowej, aby utworzyć podsumowanie każdego arkusza. To rozwiązanie koncentruje się na zgłoszeniach do obsługi klienta, ale możesz je dostosować do swoich potrzeb.

Zrzut ekranu z wynikami funkcji getSheetsData

Jak to działa

Funkcja niestandardowa o nazwie getSheetsData() podsumowuje dane z poszczególnych arkuszy w arkuszu kalkulacyjnym na podstawie jego kolumny Stan. Skrypt ignoruje arkusze, które nie powinny być uwzględniane w agregacji, np. arkusze ReadMe i Summary.

Usługi Apps Script

To rozwiązanie korzysta z następującej usługi:

  • Usługa arkusza kalkulacyjnego – pobiera arkusze wymagające podsumowania i zlicza liczbę elementów pasujących do określonego ciągu. Następnie skrypt dodaje obliczone informacje do zakresu w odniesieniu do miejsca, w którym funkcja niestandardowa została wywołana w arkuszu kalkulacyjnym.

Wymagania wstępne

Aby korzystać z tego przykładu, musisz spełnić te wymagania wstępne:

  • konta Google (konta Google Workspace mogą wymagać zatwierdzenia przez administratora),
  • Przeglądarka z dostępem do internetu.

Konfigurowanie skryptu

Kliknij przycisk poniżej, aby utworzyć kopię arkusza kalkulacyjnego Podsumuj dane w arkuszu kalkulacyjnym. Projekt Apps Script dla tego rozwiązania jest dołączony do arkusza kalkulacyjnego.
Utwórz kopię

Uruchom skrypt

  1. W skopiowanym arkuszu kalkulacyjnym otwórz arkusz Podsumowanie.
  2. Kliknij komórkę A4. Funkcja getSheetsData() znajduje się w tej komórce.
  3. Otwórz jeden z arkuszy właściciela i zaktualizuj lub dodaj dane do arkusza. Oto kilka przykładów:
    • Dodaj nowy wiersz z przykładowymi informacjami o biletach.
    • W kolumnie Stan zmień stan istniejącego zgłoszenia.
    • Zmień położenie kolumny Stan. Na przykład w arkuszu Owner1 przenieś kolumnę Stan z kolumny C do kolumny D.
  4. Otwórz arkusz Podsumowanie i sprawdź zaktualizowaną tabelę podsumowania, która została utworzona przez usługę getSheetsData() z komórki A4. Może być konieczne zaznaczenie pola wyboru w wierszu 10, aby odświeżyć wyniki funkcji niestandardowej w pamięci podręcznej. Google zapisuje w pamięci podręcznej funkcje niestandardowe, aby optymalizować wydajność.
    • Po dodaniu lub zaktualizowaniu wierszy skrypt aktualizuje liczbę zgłoszeń i stanów.
    • Jeśli zmienisz położenie kolumny Stan, skrypt z nowym indeksem kolumn będzie nadal działać zgodnie z oczekiwaniami.

Sprawdź kod

Aby sprawdzić kod Apps Script dla tego rozwiązania, kliknij Wyświetl kod źródłowy poniżej:

Pokaż kod źródłowy

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);
}

Modyfikacje

Funkcję niestandardową możesz edytować tak długo, jak chcesz. Poniżej znajduje się opcjonalny dodatek umożliwiający ręczne odświeżenie wyników funkcji niestandardowej.

Odśwież wyniki w pamięci podręcznej

W przeciwieństwie do funkcji wbudowanych Google zapisuje w pamięci podręcznej funkcje niestandardowe, aby zoptymalizować wydajność. Oznacza to, że jeśli zmienisz coś w funkcji niestandardowej, np. obliczaną wartość, aktualizacja może nie być od razu wymuszona. Aby ręcznie odświeżyć wynik funkcji:

  1. Dodaj pole wyboru do pustej komórki, klikając Wstaw > Pole wyboru.
  2. Dodaj komórkę z polem wyboru jako parametr funkcji niestandardowej, np. getSheetsData(B11).
  3. Zaznacz lub odznacz pole wyboru, aby odświeżyć wyniki funkcji niestandardowej.

Współtwórcy

Ta próbka jest opracowywana przez Google z pomocą Ekspertów Produktowych Google.

Dalsze kroki