Meringkas data dari beberapa sheet

Tingkat coding: Pemula
Durasi: 5 menit
Jenis project: Fungsi kustom

Tujuan

  • Pahami fungsi solusi tersebut.
  • Pahami fungsi layanan Apps Script dalam solusi tersebut.
  • Siapkan skrip.
  • Jalankan skrip.

Tentang solusi ini

Jika Anda memiliki data terstruktur serupa di beberapa sheet dalam spreadsheet, seperti metrik dukungan pelanggan untuk anggota tim, Anda dapat menggunakan fungsi kustom ini untuk membuat ringkasan setiap sheet. Solusi ini berfokus pada tiket dukungan pelanggan, tetapi Anda dapat menyesuaikannya dengan kebutuhan.

Screenshot output fungsi getSheetsData

Cara kerjanya

Fungsi kustom, yang disebut getSheetsData(), merangkum data dari setiap sheet di spreadsheet berdasarkan kolom Status sheet. Skrip ini mengabaikan sheet yang tidak boleh disertakan dalam agregasi, seperti sheet ReadMe dan Summary.

Layanan Apps Script

Solusi ini menggunakan layanan berikut:

  • Layanan spreadsheet–Mendapatkan sheet yang perlu diringkas dan menghitung jumlah item yang cocok dengan string yang ditentukan. Kemudian, skrip akan menambahkan informasi yang dihitung ke rentang yang sesuai dengan tempat fungsi kustom dipanggil di spreadsheet.

Prasyarat

Untuk menggunakan sampel ini, Anda memerlukan prasyarat berikut:

  • Akun Google (akun Google Workspace mungkin memerlukan persetujuan administrator).
  • Browser web dengan akses ke internet.

Menyiapkan skrip

Klik tombol di bawah untuk membuat salinan spreadsheet Merangkum data spreadsheet fungsi kustom data. Project Apps Script untuk solusi ini dilampirkan ke spreadsheet.
Buat salinan

Jalankan skrip:

  1. Di spreadsheet yang Anda salin, buka sheet Ringkasan.
  2. Klik sel A4. Fungsi getSheetsData() ada dalam sel ini.
  3. Buka salah satu sheet pemilik dan perbarui atau tambahkan data ke sheet tersebut. Beberapa tindakan yang dapat Anda coba meliputi hal berikut:
    • Tambahkan baris baru dengan contoh informasi tiket.
    • Di kolom Status, ubah status tiket yang ada.
    • Ubah posisi kolom Status. Misalnya, di sheet Owner1, pindahkan kolom Status dari kolom C ke kolom D.
  4. Buka sheet Ringkasan dan tinjau tabel ringkasan yang telah diperbarui yang dibuat getSheetsData() dari sel A4. Anda mungkin harus mencentang kotak di baris 10 untuk memperbarui hasil yang di-cache dari fungsi kustom. Google menyimpan fungsi kustom ke dalam cache untuk mengoptimalkan performa.
    • Jika Anda menambahkan atau memperbarui baris, skrip akan memperbarui jumlah tiket dan status.
    • Jika Anda memindahkan posisi kolom Status, skrip tetap berfungsi sebagaimana mestinya dengan indeks kolom baru.

Meninjau kode

Untuk meninjau kode Apps Script untuk solusi ini, klik Lihat kode sumber di bawah:

Melihat kode sumber

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

Modifikasi

Anda dapat mengedit fungsi kustom sesuai kebutuhan. Di bawah ini adalah tambahan opsional untuk memperbarui hasil fungsi kustom secara manual.

Muat ulang hasil yang di-cache

Tidak seperti fungsi bawaan, Google menyimpan fungsi kustom ke dalam cache untuk mengoptimalkan performa. Ini berarti bahwa jika Anda mengubah sesuatu dalam fungsi kustom Anda, seperti nilai yang sedang dihitung, pembaruan tersebut mungkin tidak langsung memaksakan pembaruan. Untuk memuat ulang hasil fungsi secara manual, lakukan langkah-langkah berikut:

  1. Tambahkan kotak centang ke sel kosong dengan mengklik Sisipkan > Kotak Centang.
  2. Tambahkan sel yang memiliki kotak centang sebagai parameter fungsi kustom, misalnya, getSheetsData(B11).
  3. Centang atau hapus centang pada kotak untuk memuat ulang hasil fungsi kustom.

Kontributor

Contoh ini dikelola oleh Google dengan bantuan Pakar Google Developers.

Langkah berikutnya