2

On 29 Mar 2010, Jason Hale wanted to use the spreadsheet cursor location to select an e-mail address for a Javascript app he was writing - See http://productforums.google.com/forum/#!topic/apps-script/U10q44vptPU. Respondents suggested the getActiveSelection and concomitant getRowIndex/getColumnIndex methods. Unfortunately, they didn't work for Hale and they didn't work for me two years later when I wanted to write a similar application. The cursor's location has no effect on getRowIndex and getColumnIndex. These methods always return 1, which is useless and probably a bug. Google Apps lacks a cursor service such as the one Python offers. Has anyone found anything that Google Apps returns that might be useful in this context?

JackTheLabRat
  • 31
  • 1
  • 1
  • 3
  • Do any of the posts answer your question? If so please use the "tick" option to mark that response as the answer. If not, you should edit your question and clarify it so please have an opportunity to give you a better answer. – Peter Jun 15 '12 at 01:01

2 Answers2

7

Have you tried a simple function like this one ?

function getRCposition(){
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var cell = ss.getActiveCell();
  var R = cell.getRow();
  var C = cell.getColumn();
  Browser.msgBox('Row = '+R+' and Col = '+C);
  }

or a more 'universal' function like this :

function getposition(){
   var sh = SpreadsheetApp.getActiveSheet();
   var ss = SpreadsheetApp.getActiveSpreadsheet();
  var range = ss.getActiveRange();
  var R = range.getRowIndex();
  var C = range.getColumnIndex();
  var W = range.getWidth();
  var H = range.getHeight();
  var A1not = range.getA1Notation();
  Browser.msgBox('Row = '+R+' / Col = '+C+' Width = '+W+'  / Heigth = '+H+'  A1 notation = '+A1not);
  }
Serge insas
  • 45,904
  • 7
  • 105
  • 131
7

The following code works for me. It prompts me with the row number of my current position in the spreaadsheet:

function onOpen() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var menuEntries = [ {name: "Test1", functionName: "menuItem1"}];
  ss.addMenu("Tests", menuEntries);
}

function menuItem1() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  Browser.msgBox("You have selected row " + ss.getActiveCell().getRow());
}
Community
  • 1
  • 1
Peter
  • 5,501
  • 2
  • 26
  • 42
  • Why are you creating a variable for `var range = ss.getActiveCell();`, then not using it for `Browser.msgBox("You have selected row " + ss.getActiveCell().getRow());`? You could say `range.getRow()`. – BadHorsie Feb 16 '15 at 15:45