Daveh0
Daveh0

Reputation: 990

getActiveCell() always returns 1,1

I know this has been asked before but none of the answers seem to be working for me. I need to get the values of cells that are clicked on. As a very basic prototype, I created a script tied to my spreadsheet with the following:

function getVal() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = ss.getSheets()[0];
  Logger.log(sheet.getActiveCell().getValue());
}

I then go into my spreadsheet and select a cell with a value and run the function in the script window. No value is displayed in the log.

Next I tried:

function getVal() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = ss.getSheets()[0];
  Logger.log(sheet.getActiveCell().getRow() + ',' + sheet.getActiveCell().getColumn());
}

I then go into my spreadsheet and select a cell with a value (G6) and run the function in the script window. The log displays 1,1.

And finally I tried

function onEdit(e) {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = ss.getSheets()[0];
  Browser.msgBox(sheet.getActiveCell().getRow() + ',' + sheet.getActiveCell().getColumn());
}

I then go into my spreadsheet and select a cell with a value (G6) and make a change to it. When I hit enter, the popup dialog displays 1,1.

This seems like it should be really simple but for the life of me cannot figure it out. Can anyone help?

Upvotes: 2

Views: 1799

Answers (3)

Alpesh Rathod
Alpesh Rathod

Reputation: 1

I have tried following code and works fine. getRow() method is of Range class not Sheet class.

var ActSpSheet = SpreadsheetApp.getActiveSpreadsheet();
var ActSheet  = ActSpSheet.getActiveSheet();

var GetCell = ActSheet.getCurrentCell();
var GetCurRow = GetCell.getRow();
Logger.log(GetCurRow);

Upvotes: 0

Steve
Steve

Reputation: 41

This is a bit late, but I was researching this today, and wanted to share: My issue was similar, in that sheet.getActiveRange().getRow(), sheet.getActiveCell().getRow(), etc., all returned row 1, regardless of what cell or range was active.

In this case, I was opening the script from a previous bookmark I created. Opening the script from a bookmark apparently doesn't re-establish the active relationship with the sheet.

Once I opened the script from the spreadsheet instead, my code returned the correct row reference.

Upvotes: 4

Passer-By
Passer-By

Reputation: 497

I tested this in a spreadsheet and I found that using var sheet = ss.getActiveSheet(); yielded the correct results whereas using the .getSheets()[] method did not.

Upvotes: 3

Related Questions