Birden fazla sayfadaki verileri özetle

Kodlama seviyesi: Başlangıç
Süre: 5 dakika
Proje türü: Özel işlev

Hedefler

  • Çözümün ne işe yaradığını anlayın.
  • Apps Komut Dosyası hizmetlerinin çözüm kapsamında neler yaptığını anlayın.
  • Komut dosyasını ayarlayın.
  • Komut dosyasını çalıştırın.

Bu çözüm hakkında

Bir e-tablodaki birden fazla sayfada benzer şekilde yapılandırılmış verileriniz varsa (ör. ekip üyeleri için müşteri desteği metrikleri) her sayfanın özetini oluşturmak için bu özel işlevi kullanabilirsiniz. Bu çözüm müşteri destek biletlerine odaklanıyor ancak çözümü ihtiyaçlarınıza göre özelleştirebilirsiniz.

getsheetsData fonksiyonu çıkışının ekran görüntüsü

İşleyiş şekli

getSheetsData() adlı özel işlev, e-tablodaki her bir sayfadaki verileri, sayfanın Durum sütununa göre özetler. Komut dosyası, ReadMe ve Özet sayfaları gibi toplama işlemine dahil edilmemesi gereken sayfaları yoksayar.

Apps Komut Dosyası hizmetleri

Bu çözüm aşağıdaki hizmeti kullanır:

  • E-tablo hizmeti - Özetlenmesi gereken sayfaları alır ve belirtilen bir dizeyle eşleşen öğeleri sayar. Ardından komut dosyası, hesaplanan bilgileri özel işlevin e-tabloda çağrıldığı yere göre bir aralığa ekler.

Ön koşullar

Bu örneği kullanmak için aşağıdaki önkoşullara ihtiyacınız vardır:

  • Google Hesabı (Google Workspace hesapları için yönetici onayı gerekebilir).
  • İnternete erişimi olan bir web tarayıcısı.

Komut dosyasını kurma

E-tablo verilerini özetle özel işlevi e-tablosunun kopyasını oluşturmak için aşağıdaki düğmeyi tıklayın. Bu çözüm için Apps Komut Dosyası projesi e-tabloya eklendi.
Kopya oluştur

Komut dosyasını çalıştırma

  1. Kopyalanan e-tablonuzda Özet sayfasına gidin.
  2. A4 hücresini tıklayın. getSheetsData() işlevi bu hücrede yer alır.
  3. Sayfa sahibi sayfalarından birine gidin ve sayfayı güncelleyin veya e-tabloya veri ekleyin. Deneyebileceğiniz işlemlerden bazıları şunlardır:
    • Örnek bilet bilgilerini içeren yeni bir satır ekleyin.
    • Durum sütununda, mevcut bir kaydın durumunu değiştirin.
    • Durum sütununun konumunu değiştirin. Örneğin, Owner1 sayfasında Owner1 sütununu C sütunundan D sütununa taşıyın.
  4. Özet sayfasına gidin ve getSheetsData() tarafından A4 hücresinden oluşturulan güncellenmiş özet tablosunu inceleyin. Özel işlevin önbelleğe alınmış sonuçlarını yenilemek için 10. satırdaki onay kutusunu işaretlemeniz gerekebilir. Google, performansı optimize etmek için özel işlevleri önbelleğe alır.
    • Satır eklediyseniz veya güncellediyseniz komut dosyası bileti ve durum sayılarını günceller.
    • Durum sütununun konumunu taşıdıysanız komut dosyası, yeni sütun dizininde beklendiği gibi çalışmaya devam eder.

Kodu inceleyin

Bu çözümün Apps Komut Dosyası kodunu incelemek için aşağıdaki Kaynak kodu görüntüle'yi tıklayın:

Kaynak kodu göster

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

Değişiklikler

Özel işlevi ihtiyaçlarınıza uyacak şekilde düzenleyebilirsiniz. Aşağıda, özel işlev sonuçlarını manuel olarak yenilemek için isteğe bağlı bir ekleme yer almaktadır.

Önbelleğe alınan sonuçları yenile

Yerleşik işlevlerin aksine Google, performansı optimize etmek için özel işlevleri önbelleğe alır. Diğer bir deyişle, özel işlevinizde bir şeyi (ör. hesaplanmakta olan bir değer) değiştirirseniz güncelleme hemen zorunlu kılınmayabilir. İşlev sonucunu manuel olarak yenilemek için aşağıdaki adımları uygulayın:

  1. Ekle > Onay kutusu'nu tıklayarak boş bir hücreye onay kutusu ekleyin.
  2. Özel işlevin parametresi olarak onay kutusu bulunan hücreyi ekleyin (örneğin, getSheetsData(B11)).
  3. Özel işlev sonuçlarını yenilemek için onay kutusunu işaretleyin veya kutunun işaretini kaldırın.

Katılımcılar

Bu örnek, Google Geliştirici Uzmanlarının yardımıyla Google tarafından sağlanmaktadır.

Sonraki adımlar