สรุปข้อมูลจากหลายชีต

ระดับการเขียนโค้ด: ผู้เริ่มต้น
ระยะเวลา: 5 นาที
ประเภทโปรเจ็กต์: ฟังก์ชันที่กำหนดเอง

วัตถุประสงค์

  • ทำความเข้าใจว่าโซลูชันทำอะไรได้บ้าง
  • ทำความเข้าใจสิ่งที่บริการ Apps Script ทำภายในโซลูชัน
  • ตั้งค่าสคริปต์
  • เรียกใช้สคริปต์

เกี่ยวกับโซลูชันนี้

หากมีข้อมูลที่มีโครงสร้างคล้ายกันในชีตหลายชีตในสเปรดชีต เช่น เมตริกการสนับสนุนลูกค้าสำหรับสมาชิกในทีม คุณสามารถใช้ฟังก์ชันที่กำหนดเองนี้เพื่อสร้างข้อมูลสรุปของแต่ละชีตได้ โซลูชันนี้มุ่งเน้นที่คำขอรับการสนับสนุนจากลูกค้า แต่คุณสามารถปรับแต่งให้เหมาะกับความต้องการได้

ภาพหน้าจอของเอาต์พุตฟังก์ชัน getSheetsData

วิธีการทำงาน

ฟังก์ชันที่กำหนดเองชื่อ getSheetsData() จะสรุปข้อมูลจากแต่ละชีต ในสเปรดชีตตามคอลัมน์สถานะของชีต สคริปต์จะละเว้น ชีตที่ไม่ควรนำมารวม เช่น ชีต ReadMe และ Summary

บริการ Apps Script

โซลูชันนี้ใช้บริการต่อไปนี้

  • บริการสเปรดชีต - รับชีต ที่ต้องสรุปและนับจำนวนรายการที่ตรงกับสตริงที่ ระบุ จากนั้นสคริปต์จะเพิ่มข้อมูลที่คำนวณแล้วลงในช่วง ที่สัมพันธ์กับตำแหน่งที่มีการเรียกใช้ฟังก์ชันที่กำหนดเองในสเปรดชีต

ข้อกำหนดเบื้องต้น

หากต้องการใช้ตัวอย่างนี้ คุณต้องมีข้อกำหนดเบื้องต้นต่อไปนี้

  • บัญชี Google (บัญชี Google Workspace อาจต้องได้รับการอนุมัติจากผู้ดูแลระบบ)
  • เว็บเบราว์เซอร์ที่มีสิทธิ์เข้าถึงอินเทอร์เน็ต

ตั้งค่าสคริปต์

คลิกปุ่มด้านล่างเพื่อทำสำเนาสเปรดชีตฟังก์ชันที่กำหนดเอง สรุปข้อมูลสเปรดชีต โปรเจ็กต์ Apps Script สำหรับ โซลูชันนี้แนบมากับสเปรดชีต
ทำสำเนา

เรียกใช้สคริปต์

  1. ในสเปรดชีตที่คัดลอก ให้ไปที่ชีตสรุป
  2. คลิกเซลล์ A4 ฟังก์ชัน getSheetsData() อยู่ในเซลล์นี้
  3. ไปที่ชีตของเจ้าของชีตใดชีตหนึ่ง แล้วอัปเดตหรือเพิ่มข้อมูลลงในชีต การดำเนินการบางอย่างที่คุณลองทำได้มีดังนี้
    • เพิ่มแถวใหม่ที่มีข้อมูลตัวอย่างบัตร
    • ในคอลัมน์สถานะ ให้เปลี่ยนสถานะของคำขอที่มีอยู่
    • เปลี่ยนตำแหน่งของคอลัมน์สถานะ ตัวอย่างเช่น ในชีตเจ้าของ1 ให้ย้ายคอลัมน์สถานะจากคอลัมน์ C ไปยังคอลัมน์ D
  4. ไปที่ชีตสรุปและตรวจสอบตารางสรุปที่อัปเดตแล้ว ซึ่งgetSheetsData() สร้างจากเซลล์ A4 คุณอาจต้องเลือกช่องทำเครื่องหมาย ในแถวที่ 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 Developer Expert

ขั้นตอนถัดไป