Class Selection

Selection

Access the current active selection in the active sheet. A selection is the set of cells the user has highlighted in the sheet, which can be non-adjacent ranges. One cell in the selection is the current cell, where the user's current focus is. The current cell is highlighted with a darker border in the Google Sheets UI.


 var activeSheet = SpreadsheetApp.getActiveSheet();
 var rangeList = activeSheet.getRangeList(['A1:B4', 'D1:E4']);
 rangeList.activate();

 var selection = activeSheet.getSelection();
 // Current Cell: D1
 Logger.log('Current Cell: ' + selection.getCurrentCell().getA1Notation());
 // Active Range: D1:E4
 Logger.log('Active Range: ' + selection.getActiveRange().getA1Notation());
 // Active Ranges: A1:B4, D1:E4
 var ranges =  selection.getActiveRangeList().getRanges();
 for (var i = 0; i < ranges.length; i++) {
   Logger.log('Active Ranges: ' + ranges[i].getA1Notation());
 }
 Logger.log('Active Sheet: ' + selection.getActiveSheet().getName());
 

Methods

MethodReturn typeBrief description
getActiveRange()RangeReturns the selected range in the active sheet, or null if there is no active range.
getActiveRangeList()RangeListReturns the list of active ranges in the active sheet or null if there are no active ranges.
getActiveSheet()SheetReturns the active sheet in the spreadsheet.
getCurrentCell()RangeReturns the current (highlighted) cell that is selected in one of the active ranges or null if there is no current cell.
getNextDataRange(direction)RangeStarting from the current cell and active range 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 while still covering the current cell.

Detailed documentation

getActiveRange()

Returns the selected range in the active sheet, or null if there is no active range. If multiple ranges are selected this method returns only the last selected range.


 var selection = SpreadsheetApp.getActiveSpreadsheet().getSelection();
 var activeRange = selection.getActiveRange();
 

Return

Range — the active range


getActiveRangeList()

Returns the list of active ranges in the active sheet or null if there are no active ranges.

If there is a single range selected, this behaves as a getActiveRange() call.


 var sheet = SpreadsheetApp.getActiveSheet();
 // Returns the list of active ranges.
 var activeRangeList = sheet.getActiveRangeList();
 

Return

RangeList — the list of active ranges


getActiveSheet()

Returns the active sheet in the spreadsheet.


 var selection = SpreadsheetApp.getActiveSpreadsheet().getSelection();
 var activeSheet = selection.getActiveSheet();
 

Return

Sheet — the active sheet in the spreadsheet


getCurrentCell()

Returns the current (highlighted) cell that is selected in one of the active ranges or null if there is no current cell.


 var selection = SpreadsheetApp.getActiveSpreadsheet().getSelection();
 // Returns the current highlighted cell in the one of the active ranges.
 var currentCell = selection.getCurrentCell();
 

Return

Range — the current cell


getNextDataRange(direction)

Starting from the current cell and active range 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 while still covering the current cell. If the active range is unbounded along the dimension of the direction, the original active range is returned. If there is no current cell or active range, null is returned. This is equivalent to selecting a range in the editor and hitting Ctrl+Shift+[arrow key].


 // Assume the active spreadsheet is blank.
 var ss = SpreadsheetApp.getActiveSpreadsheet();
 var sheet = ss.getSheets()[0];

 // Makes C3 the current cell and C3:E5 the active range.
 sheet.getRange("C3:E5").activate();
 // Logs "C1:E3"
 Logger.log(SpreadsheetApp.getSelection().getNextDataRange(Direction.UP).getA1Notation());
 

Parameters

NameTypeDescription
directionDirectionThe direction in which to find the next data region edge cell.

Return

Range — the adjusted range that includes the data cell, or null if there is no selection

Send feedback about...

Apps Script
Apps Script