pdro
pdro

Reputation: 317

Apps Script error: Cannot find method getRange(number,number,number,number)

I see some people have had similar issues in the past, but they seems to be just different enough for the solution to be different. So here goes:

I'm trying to return a range of known size in Google Apps Scripts for sheets like so:

var myRange = ss.getRange(2,1,14,4);

However, I'm getting an error like this:

Cannot find method getRange(number,number,number,number).

Note that using a1notation, as shown below, works just fine:

var myRange = ss.getRange("A2:D15");

The getRange function has several permutations

I've tried using all the different permutations (using hard-coded numbers), and only the a1notation one works. Any thoughts?

Upvotes: 10

Views: 20777

Answers (1)

Munkey
Munkey

Reputation: 956

As mentioned in my comment, I'd be interested to see how you've built the ss var in your code.

It could be you're pointing to a spreadsheet, and not a sheet within that spreadsheet.

I've put together a simple example of the getRange. This works and it logs how many columns and rows in the range to the Logger.

As you can see, the ss var is used for the spreadsheet and the sheet var for the sheet within the spreadsheet itself.

function getSomeRange(){

  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = ss.getActiveSheet();

  var myRange = sheet.getRange(2,1,14,4);

  Logger.log("Number of rows in range is "+myRange.getNumRows()+ " Number of columns in range is "+ myRange.getNumColumns());

}

Upvotes: 22

Related Questions