เข้าถึงการเลือกที่ใช้งานอยู่ในชีตที่ใช้งานอยู่ การเลือกคือชุดของเซลล์ที่ผู้ใช้ ไฮไลต์ในชีต ซึ่งอาจเป็นช่วงที่อยู่ไม่ติดกัน เซลล์หนึ่งในส่วนที่เลือกคือเซลล์ปัจจุบัน ซึ่งเป็นตำแหน่งที่ผู้ใช้โฟกัสอยู่ เซลล์ปัจจุบันจะไฮไลต์ด้วย เส้นขอบสีเข้มกว่าใน UI ของ Google ชีต
const activeSheet = SpreadsheetApp.getActiveSheet(); const rangeList = activeSheet.getRangeList(['A1:B4', 'D1:E4']); rangeList.activate(); const selection = activeSheet.getSelection(); // Current Cell: D1 console.log(`Current Cell: ${selection.getCurrentCell().getA1Notation()}`); // Active Range: D1:E4 console.log(`Active Range: ${selection.getActiveRange().getA1Notation()}`); // Active Ranges: A1:B4, D1:E4 const ranges = selection.getActiveRangeList().getRanges(); for (let i = 0; i < ranges.length; i++) { console.log(`Active Ranges: ${ranges[i].getA1Notation()}`); } console.log(`Active Sheet: ${selection.getActiveSheet().getName()}`);
เมธอด
| วิธีการ | ประเภทการแสดงผล | รายละเอียดแบบย่อ |
|---|---|---|
get | Range|null | แสดงช่วงที่เลือกในชีตที่ใช้งานอยู่ หรือ null หากไม่มีช่วงที่ใช้งานอยู่ |
get | Range | แสดงรายการช่วงที่ใช้งานอยู่ในชีตที่ใช้งานอยู่หรือ null หากไม่มีช่วงที่ใช้งานอยู่
|
get | Sheet | แสดงผลชีตที่ใช้งานอยู่ในสเปรดชีต |
get | Range|null | แสดงเซลล์ปัจจุบัน (ไฮไลต์) ที่เลือกในหนึ่งในช่วงที่ใช้งานอยู่ หรือ null หากไม่มีเซลล์ปัจจุบัน |
get | Range|null | เริ่มจาก current cell และ active range
และเลื่อนไปในทิศทางที่กำหนด จะแสดงผลช่วงที่ปรับแล้วซึ่งขอบที่เหมาะสมของ
ช่วงจะเลื่อนไปครอบคลุม next data cell ในขณะที่ยังคง
ครอบคลุมเซลล์ปัจจุบัน |
เอกสารโดยละเอียด
get Active Range()
แสดงช่วงที่เลือกในชีตที่ใช้งานอยู่ หรือ null หากไม่มีช่วงที่ใช้งานอยู่ หากเลือกหลายช่วง วิธีนี้จะแสดงเฉพาะช่วงที่เลือกสุดท้าย
const selection = SpreadsheetApp.getActiveSpreadsheet().getSelection(); const activeRange = selection.getActiveRange();
รีเทิร์น
Range|null - ช่วงที่ใช้งานอยู่
การให้สิทธิ์
สคริปต์ที่ใช้วิธีนี้ต้องมีการให้สิทธิ์ที่มีขอบเขตต่อไปนี้อย่างน้อย 1 รายการ
-
https://www.googleapis.com/auth/spreadsheets.currentonly -
https://www.googleapis.com/auth/spreadsheets
get Active Range List()
แสดงรายการช่วงที่ใช้งานอยู่ในชีตที่ใช้งานอยู่หรือ null หากไม่มีช่วงที่ใช้งานอยู่
หากเลือกช่วงเดียว ฟังก์ชันนี้จะทำงานเหมือนget
const sheet = SpreadsheetApp.getActiveSheet(); // Returns the list of active ranges. const activeRangeList = sheet.getActiveRangeList();
รีเทิร์น
Range — รายการช่วงที่ใช้งานอยู่
การให้สิทธิ์
สคริปต์ที่ใช้วิธีนี้ต้องมีการให้สิทธิ์ที่มีขอบเขตต่อไปนี้อย่างน้อย 1 รายการ
-
https://www.googleapis.com/auth/spreadsheets.currentonly -
https://www.googleapis.com/auth/spreadsheets
get Active Sheet()
แสดงผลชีตที่ใช้งานอยู่ในสเปรดชีต
const selection = SpreadsheetApp.getActiveSpreadsheet().getSelection(); const activeSheet = selection.getActiveSheet();
รีเทิร์น
Sheet — ชีตที่ใช้งานอยู่ในสเปรดชีต
การให้สิทธิ์
สคริปต์ที่ใช้วิธีนี้ต้องมีการให้สิทธิ์ที่มีขอบเขตต่อไปนี้อย่างน้อย 1 รายการ
-
https://www.googleapis.com/auth/spreadsheets.currentonly -
https://www.googleapis.com/auth/spreadsheets
get Current Cell()
แสดงเซลล์ปัจจุบัน (ไฮไลต์) ที่เลือกในหนึ่งในช่วงที่ใช้งานอยู่ หรือ null หากไม่มีเซลล์ปัจจุบัน
const selection = SpreadsheetApp.getActiveSpreadsheet().getSelection(); // Returns the current highlighted cell in the one of the active ranges. const currentCell = selection.getCurrentCell();
รีเทิร์น
Range|null — เซลล์ปัจจุบัน
การให้สิทธิ์
สคริปต์ที่ใช้วิธีนี้ต้องมีการให้สิทธิ์ที่มีขอบเขตต่อไปนี้อย่างน้อย 1 รายการ
-
https://www.googleapis.com/auth/spreadsheets.currentonly -
https://www.googleapis.com/auth/spreadsheets
get Next Data Range(direction)
เริ่มจาก current cell และ active range
และเลื่อนไปในทิศทางที่กำหนด จะแสดงผลช่วงที่ปรับแล้วซึ่งขอบที่เหมาะสมของ
ช่วงจะเลื่อนไปครอบคลุม next data cell ในขณะที่ยังคง
ครอบคลุมเซลล์ปัจจุบัน หากช่วงที่ใช้งานอยู่ไม่มีขอบเขตตามdimensionของทิศทาง ระบบจะแสดงช่วงที่ใช้งานอยู่เดิม หากไม่มีเซลล์ปัจจุบัน
หรือช่วงที่ใช้งานอยู่ ระบบจะแสดงผล null ซึ่งเทียบเท่ากับการเลือกช่วงใน
เอดิเตอร์แล้วกด Ctrl+Shift+[arrow key]
// Assume the active spreadsheet is blank. const ss = SpreadsheetApp.getActiveSpreadsheet(); const sheet = ss.getSheets()[0]; // Makes C3 the current cell and C3:E5 the active range. sheet.getRange('C3:E5').activate(); // Logs 'C1:E3' console.log( SpreadsheetApp.getSelection() .getNextDataRange(SpreadsheetApp.Direction.UP) .getA1Notation(), );
พารามิเตอร์
| ชื่อ | ประเภท | คำอธิบาย |
|---|---|---|
direction | Direction | ทิศทางที่จะค้นหาเซลล์ขอบเขตข้อมูลถัดไป |
รีเทิร์น
Range|null — ช่วงที่ปรับแล้วซึ่งรวมเซลล์ข้อมูล หรือ null หากไม่มีการเลือก
การให้สิทธิ์
สคริปต์ที่ใช้วิธีนี้ต้องมีการให้สิทธิ์ที่มีขอบเขตต่อไปนี้อย่างน้อย 1 รายการ
-
https://www.googleapis.com/auth/spreadsheets.currentonly -
https://www.googleapis.com/auth/spreadsheets