Class Filter

กรอง

ใช้คลาสนี้เพื่อแก้ไขตัวกรองที่มีอยู่ในชีต ซึ่งเป็นชีตประเภทเริ่มต้นGrid ชีตตารางกริดคือชีตปกติที่มีข้อมูลที่ไม่ได้เชื่อมต่อกับฐานข้อมูล

หากยังไม่มีตัวกรองในชีต ให้สร้างโดยใช้ Range.createFilter()

หากต้องการใช้คลาสนี้ คุณต้องเข้าถึงตัวกรองชีตตารางกริดก่อนโดยใช้ Range.getFilter() หรือ Sheet.getFilter()

การใช้งานทั่วไป

นำตัวกรองออก

ตัวอย่างด้านล่างจะรับตัวกรองในชีตที่ใช้งานอยู่และนำออก
const ss = SpreadsheetApp.getActiveSheet();
const filter = ss.getFilter();
// Removes the filter from the active sheet.
filter.remove();

รับช่วงที่ตัวกรองใช้

ตัวอย่างด้านล่างจะรับตัวกรองในชีตที่ใช้งานอยู่ จากนั้นใช้วิธี getRange() จาก คลาสนี้เพื่อบันทึกช่วงที่ตัวกรองใช้
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());

เมธอด

วิธีการประเภทการแสดงผลรายละเอียดแบบย่อ
getColumnFilterCriteria(columnPosition)FilterCriteria|nullรับเกณฑ์ตัวกรองในคอลัมน์ที่ระบุ หรือ null หากคอลัมน์ไม่มี เกณฑ์ตัวกรองที่ใช้กับคอลัมน์
getRange()Rangeรับช่วงที่ตัวกรองนี้ใช้
remove()voidนำตัวกรองนี้ออก
removeColumnFilterCriteria(columnPosition)Filterนำเกณฑ์ตัวกรองออกจากคอลัมน์ที่ระบุ
setColumnFilterCriteria(columnPosition, filterCriteria)Filterตั้งค่าเกณฑ์ตัวกรองในคอลัมน์ที่ระบุ
sort(columnPosition, ascending)Filterจัดเรียงช่วงที่กรองตามคอลัมน์ที่ระบุ โดยไม่รวมแถวแรก (แถวส่วนหัว) ในช่วงที่ตัวกรองนี้ใช้

เอกสารโดยละเอียด

getColumnFilterCriteria(columnPosition)

รับเกณฑ์ตัวกรองในคอลัมน์ที่ระบุ หรือ null หากคอลัมน์ไม่มี เกณฑ์ตัวกรองที่ใช้กับคอลัมน์

หากต้องการดูรายละเอียดเพิ่มเติมเกี่ยวกับเกณฑ์ตัวกรอง ให้เชื่อมโยงเมธอดนี้กับเมธอดจากคลาส FilterCriteria

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

พารามิเตอร์

ชื่อประเภทคำอธิบาย
columnPositionIntegerตำแหน่งของคอลัมน์ที่จัดทำดัชนี 1 เช่น ดัชนีของคอลัมน์ B คือ 2

รีเทิร์น

FilterCriteria|null — เกณฑ์ตัวกรอง

การให้สิทธิ์

สคริปต์ที่ใช้วิธีนี้ต้องมีการให้สิทธิ์ที่มีขอบเขตต่อไปนี้อย่างน้อย 1 รายการ

  • https://www.googleapis.com/auth/spreadsheets.currentonly
  • https://www.googleapis.com/auth/spreadsheets

getRange()

รับช่วงที่ตัวกรองนี้ใช้

// 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

removeColumnFilterCriteria(columnPosition)

นำเกณฑ์ตัวกรองออกจากคอลัมน์ที่ระบุ

// Removes the filter criteria from column B.
const ss = SpreadsheetApp.getActiveSheet();
const filter = ss.getFilter();
filter.removeColumnFilterCriteria(2);

พารามิเตอร์

ชื่อประเภทคำอธิบาย
columnPositionIntegerตำแหน่งของคอลัมน์ที่จัดทำดัชนี 1 เช่น ดัชนีของคอลัมน์ B คือ 2

รีเทิร์น

Filter - ตัวกรองสำหรับการเชื่อมโยง

การให้สิทธิ์

สคริปต์ที่ใช้วิธีนี้ต้องมีการให้สิทธิ์ที่มีขอบเขตต่อไปนี้อย่างน้อย 1 รายการ

  • https://www.googleapis.com/auth/spreadsheets.currentonly
  • https://www.googleapis.com/auth/spreadsheets

setColumnFilterCriteria(columnPosition, filterCriteria)

ตั้งค่าเกณฑ์ตัวกรองในคอลัมน์ที่ระบุ ก่อนอื่น ให้สร้างเครื่องมือสร้างเกณฑ์ตัวกรอง โดยใช้ SpreadsheetApp.newFilterCriteria() จากนั้นเพิ่มเกณฑ์ลงในเครื่องมือสร้าง โดยใช้คลาส FilterCriteriaBuilder หลังจากสร้างเกณฑ์แล้ว ให้ตั้งค่าเป็นพารามิเตอร์ filterCriteria สำหรับเมธอดนี้

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

พารามิเตอร์

ชื่อประเภทคำอธิบาย
columnPositionIntegerตำแหน่งของคอลัมน์ที่จัดทำดัชนี 1 เช่น ดัชนีของคอลัมน์ B คือ 2
filterCriteriaFilterCriteriaเกณฑ์ตัวกรองที่จะตั้งค่า หากตั้งค่าเกณฑ์เป็น null ระบบจะ นำเกณฑ์ตัวกรองออกจากคอลัมน์ที่ระบุ หรือจะใช้ removeColumnFilterCriteria(columnPosition) ก็ได้

รีเทิร์น

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

พารามิเตอร์

ชื่อประเภทคำอธิบาย
columnPositionIntegerตำแหน่งของคอลัมน์ที่จัดทำดัชนี 1 เช่น ดัชนีของคอลัมน์ B คือ 2
ascendingBooleanหากเป็น true จะจัดเรียงช่วงที่กรองจากน้อยไปมาก หากเป็น false จะจัดเรียงช่วงที่กรองจากมากไปน้อย

รีเทิร์น

Filter - ตัวกรองสำหรับการเชื่อมโยง

การให้สิทธิ์

สคริปต์ที่ใช้วิธีนี้ต้องมีการให้สิทธิ์ที่มีขอบเขตต่อไปนี้อย่างน้อย 1 รายการ

  • https://www.googleapis.com/auth/spreadsheets.currentonly
  • https://www.googleapis.com/auth/spreadsheets