botenvouwer
botenvouwer

Reputation: 4432

Get range of a sheet up to the last cell containing data

I want to select the range of 2 columns till the last row with a value. Now I use this code:

var test = declSheet.getRange('Potjes!A:B');

Form this sheet:

enter image description here

The result I get is this:

enter image description here

How can I select till the last row with value?

Upvotes: 1

Views: 1801

Answers (1)

Salman Arshad
Salman Arshad

Reputation: 272006

You can use Sheet.getDataRange function which:

Returns a Range corresponding to the dimensions in which data is present.

It seems like you are only interested in column A and B so:

// var spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
// var sheet = spreadsheet.getActiveSheet();
var dataRange = sheet.getDataRange();
var desiredRange = sheet.getRange("A1:B" + dataRange.getLastRow());
// desiredRange will consist of A1:B5 in OP example

Upvotes: 3

Related Questions