ใช้คลาสนี้เพื่อแก้ไขตัวกรองที่มีอยู่ในชีต ซึ่งเป็นชีตประเภทเริ่มต้นGrid ชีตตารางกริดคือชีตปกติที่มีข้อมูลที่ไม่ได้เชื่อมต่อกับฐานข้อมูล
หากยังไม่มีตัวกรองในชีต ให้สร้างโดยใช้ Range.createFilter()
หากต้องการใช้คลาสนี้ คุณต้องเข้าถึงตัวกรองชีตตารางกริดก่อนโดยใช้ Range.getFilter() หรือ Sheet.getFilter()
การใช้งานทั่วไป
นำตัวกรองออก
ตัวอย่างด้านล่างจะรับตัวกรองในชีตที่ใช้งานอยู่และนำออกconst ss = SpreadsheetApp.getActiveSheet(); const filter = ss.getFilter(); // Removes the filter from the active sheet. filter.remove();
รับช่วงที่ตัวกรองใช้
ตัวอย่างด้านล่างจะรับตัวกรองในชีตที่ใช้งานอยู่ จากนั้นใช้วิธีget Range() จาก
คลาสนี้เพื่อบันทึกช่วงที่ตัวกรองใช้
const ss = SpreadsheetApp.getActiveSheet(); // Gets the existing filter on the active sheet. const filter = ss.getFilter(); // Logs the range that the filter applies to in A1 notation. console.log(filter.getRange().getA1Notation());
เมธอด
| วิธีการ | ประเภทการแสดงผล | รายละเอียดแบบย่อ |
|---|---|---|
get | Filter | รับเกณฑ์ตัวกรองในคอลัมน์ที่ระบุ หรือ null หากคอลัมน์ไม่มี
เกณฑ์ตัวกรองที่ใช้กับคอลัมน์ |
get | Range | รับช่วงที่ตัวกรองนี้ใช้ |
remove() | void | นำตัวกรองนี้ออก |
remove | Filter | นำเกณฑ์ตัวกรองออกจากคอลัมน์ที่ระบุ |
set | Filter | ตั้งค่าเกณฑ์ตัวกรองในคอลัมน์ที่ระบุ |
sort(columnPosition, ascending) | Filter | จัดเรียงช่วงที่กรองตามคอลัมน์ที่ระบุ โดยไม่รวมแถวแรก (แถวส่วนหัว) ในช่วงที่ตัวกรองนี้ใช้ |
เอกสารโดยละเอียด
get Column Filter Criteria(columnPosition)
รับเกณฑ์ตัวกรองในคอลัมน์ที่ระบุ หรือ null หากคอลัมน์ไม่มี
เกณฑ์ตัวกรองที่ใช้กับคอลัมน์
หากต้องการดูรายละเอียดเพิ่มเติมเกี่ยวกับเกณฑ์ตัวกรอง ให้เชื่อมโยงเมธอดนี้กับเมธอดจากคลาส Filter
const ss = SpreadsheetApp.getActiveSheet(); const filter = ss.getFilter(); // Gets the filter criteria applied to column B of the active sheet // and logs the hidden values. const filterCriteria = filter.getColumnFilterCriteria(2).getHiddenValues(); console.log(filterCriteria);
พารามิเตอร์
| ชื่อ | ประเภท | คำอธิบาย |
|---|---|---|
column | Integer | ตำแหน่งของคอลัมน์ที่จัดทำดัชนี 1 เช่น ดัชนีของคอลัมน์ B คือ 2 |
รีเทิร์น
Filter — เกณฑ์ตัวกรอง
การให้สิทธิ์
สคริปต์ที่ใช้วิธีนี้ต้องมีการให้สิทธิ์ที่มีขอบเขตต่อไปนี้อย่างน้อย 1 รายการ
-
https://www.googleapis.com/auth/spreadsheets.currentonly -
https://www.googleapis.com/auth/spreadsheets
get Range()
รับช่วงที่ตัวกรองนี้ใช้
// Gets the existing filter on the active sheet. const ss = SpreadsheetApp.getActiveSheet(); const filter = ss.getFilter(); // Logs the range that the filter applies to in A1 notation. console.log(filter.getRange().getA1Notation());
รีเทิร์น
Range — ช่วงของตัวกรอง หากต้องการรับช่วงในรูปแบบ A1 ให้เชื่อมโยงวิธีการนี้กับ Range.getA1Notation()
การให้สิทธิ์
สคริปต์ที่ใช้วิธีนี้ต้องมีการให้สิทธิ์ที่มีขอบเขตต่อไปนี้อย่างน้อย 1 รายการ
-
https://www.googleapis.com/auth/spreadsheets.currentonly -
https://www.googleapis.com/auth/spreadsheets
remove()
นำตัวกรองนี้ออก
// Removes the filter from the active sheet. const ss = SpreadsheetApp.getActiveSheet(); const filter = ss.getFilter(); filter.remove();
การให้สิทธิ์
สคริปต์ที่ใช้วิธีนี้ต้องมีการให้สิทธิ์ที่มีขอบเขตต่อไปนี้อย่างน้อย 1 รายการ
-
https://www.googleapis.com/auth/spreadsheets.currentonly -
https://www.googleapis.com/auth/spreadsheets
remove Column Filter Criteria(columnPosition)
นำเกณฑ์ตัวกรองออกจากคอลัมน์ที่ระบุ
// Removes the filter criteria from column B. const ss = SpreadsheetApp.getActiveSheet(); const filter = ss.getFilter(); filter.removeColumnFilterCriteria(2);
พารามิเตอร์
| ชื่อ | ประเภท | คำอธิบาย |
|---|---|---|
column | Integer | ตำแหน่งของคอลัมน์ที่จัดทำดัชนี 1 เช่น ดัชนีของคอลัมน์ B คือ 2 |
รีเทิร์น
Filter - ตัวกรองสำหรับการเชื่อมโยง
การให้สิทธิ์
สคริปต์ที่ใช้วิธีนี้ต้องมีการให้สิทธิ์ที่มีขอบเขตต่อไปนี้อย่างน้อย 1 รายการ
-
https://www.googleapis.com/auth/spreadsheets.currentonly -
https://www.googleapis.com/auth/spreadsheets
set Column Filter Criteria(columnPosition, filterCriteria)
ตั้งค่าเกณฑ์ตัวกรองในคอลัมน์ที่ระบุ ก่อนอื่น ให้สร้างเครื่องมือสร้างเกณฑ์ตัวกรอง
โดยใช้ Spreadsheet จากนั้นเพิ่มเกณฑ์ลงในเครื่องมือสร้าง
โดยใช้คลาส Filter หลังจากสร้างเกณฑ์แล้ว ให้ตั้งค่าเป็นพารามิเตอร์ filter สำหรับเมธอดนี้
const ss = SpreadsheetApp.getActiveSheet(); const filter = ss.getFilter(); // Builds the filter criteria to use as a parameter for setColumnFilterCriteria. const criteria = SpreadsheetApp.newFilterCriteria() .setHiddenValues(['Hello', 'World']) .build(); // Sets the filter criteria for column C. filter.setColumnFilterCriteria(3, criteria);
พารามิเตอร์
| ชื่อ | ประเภท | คำอธิบาย |
|---|---|---|
column | Integer | ตำแหน่งของคอลัมน์ที่จัดทำดัชนี 1 เช่น ดัชนีของคอลัมน์ B คือ 2 |
filter | Filter | เกณฑ์ตัวกรองที่จะตั้งค่า หากตั้งค่าเกณฑ์เป็น null ระบบจะ
นำเกณฑ์ตัวกรองออกจากคอลัมน์ที่ระบุ หรือจะใช้ remove ก็ได้ |
รีเทิร์น
Filter - ตัวกรองสำหรับการเชื่อมโยง
การให้สิทธิ์
สคริปต์ที่ใช้วิธีนี้ต้องมีการให้สิทธิ์ที่มีขอบเขตต่อไปนี้อย่างน้อย 1 รายการ
-
https://www.googleapis.com/auth/spreadsheets.currentonly -
https://www.googleapis.com/auth/spreadsheets
sort(columnPosition, ascending)
จัดเรียงช่วงที่กรองตามคอลัมน์ที่ระบุ โดยไม่รวมแถวแรก (แถวส่วนหัว) ในช่วงที่ตัวกรองนี้ใช้
// Gets the existing filter and sorts it by column B in ascending order. const ss = SpreadsheetApp.getActiveSheet(); const filter = ss.getFilter(); filter.sort(2, true);
พารามิเตอร์
| ชื่อ | ประเภท | คำอธิบาย |
|---|---|---|
column | Integer | ตำแหน่งของคอลัมน์ที่จัดทำดัชนี 1 เช่น ดัชนีของคอลัมน์ B คือ 2 |
ascending | Boolean | หากเป็น true จะจัดเรียงช่วงที่กรองจากน้อยไปมาก หากเป็น false จะจัดเรียงช่วงที่กรองจากมากไปน้อย |
รีเทิร์น
Filter - ตัวกรองสำหรับการเชื่อมโยง
การให้สิทธิ์
สคริปต์ที่ใช้วิธีนี้ต้องมีการให้สิทธิ์ที่มีขอบเขตต่อไปนี้อย่างน้อย 1 รายการ
-
https://www.googleapis.com/auth/spreadsheets.currentonly -
https://www.googleapis.com/auth/spreadsheets