gssi
gssi

Reputation: 5071

getActiveCell() always returns the upper left-hand cell, regardless of which cell is selected in the range

The following code always returns the reference to the upper left-hand cell regardless of the actual corner cell selected.

var actvC = SpreadsheetApp.getActiveRange();
var txt_actvC = actvC.getA1Notation();
actvC = SpreadsheetApp.getActiveSheet().getActiveCell();
txt_actvC = actvC.getA1Notation();

Is there anyway of identifying the actual selected cell?

Upvotes: 1

Views: 9154

Answers (3)

hani E
hani E

Reputation: 51

There is a getCurrentCell() now to return the current selected cell.

Returns the current cell in the active sheet or null if there is no current cell. The current cell is the cell that has focus in the Google Sheets UI, and is highlighted by a dark border. There is never more than one current cell. When a user selects one or more cell ranges, one of the cells in the selection is the current cell.

Example:

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

Upvotes: 5

Matt
Matt

Reputation: 337

Workaround

function readRows() {
  var s = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = s.getActiveSheet();
  var active = sheet.getActiveCell().getA1Notation();
  var sheet1 = s.getSheetName();

  if (sheet1 == "Sheet1"){

  Logger.log(active);

  }
};

getActiveCell() does not currently work with getSheets()[i] or getSheetbyName()

Upvotes: 3

patt0
patt0

Reputation: 800

Yes you seem to be correct, the activecell in an active range is the one on the top left corner even if you selected the range from any of the other corners. What is your use case?

function getCell() {
  var actvC = SpreadsheetApp.getActiveRange();
  var txt_actvC = actvC.getA1Notation();
  Logger.log(txt_actvC);

  actvC = SpreadsheetApp.getActiveSheet().getActiveCell();
  txt_actvC = actvC.getA1Notation();
  Logger.log(txt_actvC);
}

Result : enter image description here

Upvotes: 2

Related Questions