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

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

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

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

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

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

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

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

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

บริการ Apps Script

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

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

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

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

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

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

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

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

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

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