Class Selection
จัดทุกอย่างให้เป็นระเบียบอยู่เสมอด้วยคอลเล็กชัน
บันทึกและจัดหมวดหมู่เนื้อหาตามค่ากำหนดของคุณ
การเลือก
เข้าถึงการเลือกที่ใช้งานอยู่ในปัจจุบันในชีตที่ใช้งานอยู่ การเลือกคือชุดเซลล์ที่ผู้ใช้ไฮไลต์ไว้ในชีต ซึ่งอาจเป็นช่วงที่ไม่ได้อยู่ติดกัน เซลล์หนึ่งในส่วนที่เลือกคือเซลล์ปัจจุบัน ซึ่งเป็นจุดที่ผู้ใช้โฟกัสอยู่ เซลล์ปัจจุบันจะไฮไลต์ด้วยเส้นขอบที่เข้มกว่าใน 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()}`);
เอกสารประกอบโดยละเอียด
getActiveRange()
แสดงผลช่วงที่คุณเลือกในสเปรดชีตที่ใช้งานอยู่ หรือ null
หากไม่มีช่วงทำงานอยู่ หากเลือกหลายช่วง วิธีการนี้จะแสดงเฉพาะช่วงล่าสุดที่เลือก
const selection = SpreadsheetApp.getActiveSpreadsheet().getSelection();
const activeRange = selection.getActiveRange();
รีเทิร์น
Range
— ช่วงที่มีการใช้งาน
การให้สิทธิ์
สคริปต์ที่ใช้วิธีการนี้ต้องได้รับอนุญาตด้วยขอบเขตต่อไปนี้อย่างน้อย 1 รายการ
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
getActiveRangeList()
แสดงรายการช่วงที่มีการใช้งานในสเปรดชีตที่ใช้งานอยู่ หรือ null
หากไม่มีช่วงที่มีการใช้งาน
หากเลือกช่วงเดียว การดำเนินการนี้จะทํางานแบบการเรียก getActiveRange()
const sheet = SpreadsheetApp.getActiveSheet();
// Returns the list of active ranges.
const activeRangeList = sheet.getActiveRangeList();
รีเทิร์น
RangeList
— รายการช่วงที่มีการใช้งาน
การให้สิทธิ์
สคริปต์ที่ใช้วิธีการนี้ต้องได้รับอนุญาตด้วยขอบเขตต่อไปนี้อย่างน้อย 1 รายการ
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
getActiveSheet()
แสดงผลชีตที่ใช้งานอยู่ในสเปรดชีต
const selection = SpreadsheetApp.getActiveSpreadsheet().getSelection();
const activeSheet = selection.getActiveSheet();
รีเทิร์น
Sheet
— ชีตที่ใช้งานอยู่ในสเปรดชีต
การให้สิทธิ์
สคริปต์ที่ใช้วิธีการนี้ต้องได้รับอนุญาตด้วยขอบเขตต่อไปนี้อย่างน้อย 1 รายการ
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
getCurrentCell()
แสดงผลเซลล์ปัจจุบัน (ที่ไฮไลต์) ซึ่งเลือกไว้ในหนึ่งในช่วงที่ใช้งานอยู่ หรือ null
หากไม่มีเซลล์ปัจจุบัน
const selection = SpreadsheetApp.getActiveSpreadsheet().getSelection();
// Returns the current highlighted cell in the one of the active ranges.
const currentCell = selection.getCurrentCell();
รีเทิร์น
Range
— เซลล์ปัจจุบัน
การให้สิทธิ์
สคริปต์ที่ใช้วิธีการนี้ต้องได้รับอนุญาตด้วยขอบเขตต่อไปนี้อย่างน้อย 1 รายการ
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
getNextDataRange(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
หากไม่มีการเลือก
การให้สิทธิ์
สคริปต์ที่ใช้วิธีการนี้ต้องได้รับอนุญาตด้วยขอบเขตต่อไปนี้อย่างน้อย 1 รายการ
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
เนื้อหาของหน้าเว็บนี้ได้รับอนุญาตภายใต้ใบอนุญาตที่ต้องระบุที่มาของครีเอทีฟคอมมอนส์ 4.0 และตัวอย่างโค้ดได้รับอนุญาตภายใต้ใบอนุญาต Apache 2.0 เว้นแต่จะระบุไว้เป็นอย่างอื่น โปรดดูรายละเอียดที่นโยบายเว็บไซต์ Google Developers Java เป็นเครื่องหมายการค้าจดทะเบียนของ Oracle และ/หรือบริษัทในเครือ
อัปเดตล่าสุด 2025-07-26 UTC
[null,null,["อัปเดตล่าสุด 2025-07-26 UTC"],[[["\u003cp\u003eAccess and manipulate the active selection, which includes the highlighted cells and the current cell within a Google Sheet.\u003c/p\u003e\n"],["\u003cp\u003eUtilize methods like \u003ccode\u003egetActiveRange()\u003c/code\u003e, \u003ccode\u003egetCurrentCell()\u003c/code\u003e, and \u003ccode\u003egetActiveRangeList()\u003c/code\u003e to retrieve specific parts of the selection.\u003c/p\u003e\n"],["\u003cp\u003eThe \u003ccode\u003egetNextDataRange()\u003c/code\u003e method allows for expanding the selection to encompass adjacent data cells.\u003c/p\u003e\n"],["\u003cp\u003eEach of these methods requires specific authorization scopes for access to spreadsheet data.\u003c/p\u003e\n"],["\u003cp\u003eA selection can include non-adjacent ranges and provides programmatic control over user interactions with the sheet's data.\u003c/p\u003e\n"]]],["The content describes how to interact with cell selections in Google Sheets using Apps Script. Key actions include retrieving the `active range` (last selected range), `active range list` (all selected ranges), `current cell` (user's focus), and `active sheet`. The `getNextDataRange` method adjusts the active range to include the next data cell in a specified direction. These operations require authorization scopes related to spreadsheets. The script provided an example to retrieve all of those key elements from the selection.\n"],null,["# Class Selection\n\nSelection\n\nAccess the current active selection in the active sheet. A selection is the set of cells the user\nhas highlighted in the sheet, which can be non-adjacent ranges. One cell in the selection is the\n*current cell*, where the user's current focus is. The current cell is highlighted with a\ndarker border in the Google Sheets UI.\n\n```javascript\nconst activeSheet = SpreadsheetApp.getActiveSheet();\nconst rangeList = activeSheet.getRangeList(['A1:B4', 'D1:E4']);\nrangeList.activate();\n\nconst selection = activeSheet.getSelection();\n// Current Cell: D1\nconsole.log(`Current Cell: ${selection.getCurrentCell().getA1Notation()}`);\n// Active Range: D1:E4\nconsole.log(`Active Range: ${selection.getActiveRange().getA1Notation()}`);\n// Active Ranges: A1:B4, D1:E4\nconst ranges = selection.getActiveRangeList().getRanges();\nfor (let i = 0; i \u003c ranges.length; i++) {\n console.log(`Active Ranges: ${ranges[i].getA1Notation()}`);\n}\nconsole.log(`Active Sheet: ${selection.getActiveSheet().getName()}`);\n``` \n\n### Methods\n\n| Method | Return type | Brief description |\n|-------------------------------------------------------------|------------------------------------------------------------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| [getActiveRange()](#getActiveRange()) | [Range](/apps-script/reference/spreadsheet/range) | Returns the selected range in the active sheet, or `null` if there is no active range. |\n| [getActiveRangeList()](#getActiveRangeList()) | [RangeList](/apps-script/reference/spreadsheet/range-list) | Returns the list of active ranges in the active sheet or `null` if there are no active ranges. |\n| [getActiveSheet()](#getActiveSheet()) | [Sheet](/apps-script/reference/spreadsheet/sheet) | Returns the active sheet in the spreadsheet. |\n| [getCurrentCell()](#getCurrentCell()) | [Range](/apps-script/reference/spreadsheet/range) | Returns the current (highlighted) cell that is selected in one of the active ranges or `null` if there is no current cell. |\n| [getNextDataRange(direction)](#getNextDataRange(Direction)) | [Range](/apps-script/reference/spreadsheet/range) | Starting from the [current cell](#getCurrentCell()) and [active range](#getActiveRange()) and moving in the given direction, returns an adjusted range where the appropriate edge of the range has been shifted to cover the [next data cell](/apps-script/reference/spreadsheet/range#getNextDataCell(Direction)) while still covering the current cell. |\n\nDetailed documentation\n----------------------\n\n### `get``Active``Range()`\n\nReturns the selected range in the active sheet, or `null` if there is no active range. If\nmultiple ranges are selected this method returns only the last selected range.\n\n```javascript\nconst selection = SpreadsheetApp.getActiveSpreadsheet().getSelection();\nconst activeRange = selection.getActiveRange();\n```\n\n#### Return\n\n\n[Range](/apps-script/reference/spreadsheet/range) --- The active range.\n\n#### Authorization\n\nScripts that use this method require authorization with one or more of the following [scopes](/apps-script/concepts/scopes#setting_explicit_scopes):\n\n- `https://www.googleapis.com/auth/spreadsheets.currentonly`\n- `https://www.googleapis.com/auth/spreadsheets`\n\n*** ** * ** ***\n\n### `get``Active``Range``List()`\n\nReturns the list of active ranges in the active sheet or `null` if there are no active\nranges.\n\nIf there is a single range selected, this behaves as a [getActiveRange()](#getActiveRange()) call.\n\n```javascript\nconst sheet = SpreadsheetApp.getActiveSheet();\n// Returns the list of active ranges.\nconst activeRangeList = sheet.getActiveRangeList();\n```\n\n#### Return\n\n\n[RangeList](/apps-script/reference/spreadsheet/range-list) --- The list of active ranges.\n\n#### Authorization\n\nScripts that use this method require authorization with one or more of the following [scopes](/apps-script/concepts/scopes#setting_explicit_scopes):\n\n- `https://www.googleapis.com/auth/spreadsheets.currentonly`\n- `https://www.googleapis.com/auth/spreadsheets`\n\n*** ** * ** ***\n\n### `get``Active``Sheet()`\n\nReturns the active sheet in the spreadsheet.\n\n```javascript\nconst selection = SpreadsheetApp.getActiveSpreadsheet().getSelection();\nconst activeSheet = selection.getActiveSheet();\n```\n\n#### Return\n\n\n[Sheet](/apps-script/reference/spreadsheet/sheet) --- The active sheet in the spreadsheet.\n\n#### Authorization\n\nScripts that use this method require authorization with one or more of the following [scopes](/apps-script/concepts/scopes#setting_explicit_scopes):\n\n- `https://www.googleapis.com/auth/spreadsheets.currentonly`\n- `https://www.googleapis.com/auth/spreadsheets`\n\n*** ** * ** ***\n\n### `get``Current``Cell()`\n\nReturns the current (highlighted) cell that is selected in one of the active ranges or `null` if there is no current cell.\n\n```javascript\nconst selection = SpreadsheetApp.getActiveSpreadsheet().getSelection();\n// Returns the current highlighted cell in the one of the active ranges.\nconst currentCell = selection.getCurrentCell();\n```\n\n#### Return\n\n\n[Range](/apps-script/reference/spreadsheet/range) --- The current cell.\n\n#### Authorization\n\nScripts that use this method require authorization with one or more of the following [scopes](/apps-script/concepts/scopes#setting_explicit_scopes):\n\n- `https://www.googleapis.com/auth/spreadsheets.currentonly`\n- `https://www.googleapis.com/auth/spreadsheets`\n\n*** ** * ** ***\n\n### `get``Next``Data``Range(direction)`\n\nStarting from the [current cell](#getCurrentCell()) and [active range](#getActiveRange())\nand moving in the given direction, returns an adjusted range where the appropriate edge of the\nrange has been shifted to cover the [next data cell](/apps-script/reference/spreadsheet/range#getNextDataCell(Direction)) while still\ncovering the current cell. If the active range is unbounded along the [dimension](/apps-script/reference/spreadsheet/dimension) of the direction, the original active range is returned. If there is no current cell\nor active range, `null` is returned. This is equivalent to selecting a range in the\neditor and hitting `Ctrl+Shift+[arrow key]`.\n\n```javascript\n// Assume the active spreadsheet is blank.\nconst ss = SpreadsheetApp.getActiveSpreadsheet();\nconst sheet = ss.getSheets()[0];\n\n// Makes C3 the current cell and C3:E5 the active range.\nsheet.getRange('C3:E5').activate();\n// Logs 'C1:E3'\nconsole.log(\n SpreadsheetApp.getSelection()\n .getNextDataRange(SpreadsheetApp.Direction.UP)\n .getA1Notation(),\n);\n```\n\n#### Parameters\n\n| Name | Type | Description |\n|-------------|-----------------------------------------------------------|----------------------------------------------------------------|\n| `direction` | [Direction](/apps-script/reference/spreadsheet/direction) | The direction in which to find the next data region edge cell. |\n\n#### Return\n\n\n[Range](/apps-script/reference/spreadsheet/range) --- The adjusted range that includes the data cell, or `null` if there is no\nselection.\n\n#### Authorization\n\nScripts that use this method require authorization with one or more of the following [scopes](/apps-script/concepts/scopes#setting_explicit_scopes):\n\n- `https://www.googleapis.com/auth/spreadsheets.currentonly`\n- `https://www.googleapis.com/auth/spreadsheets`"]]