define a range that goes to the last filled cell on the right?

275 views
Skip to first unread message

froot

unread,
Oct 18, 2022, 4:00:35 PM10/18/22
to Google Apps Script Community
how can I define a range that goes to the last filled cell inside of it?

const clientData = clientSheet.getRange(2,1,currentSheet.getLastRow()-1,7).getDisplayValues();

Code above works fine if the range is the only entry in the current sheet, but not if below or above are other rows/ranges that have filled cells further to the right.
Is it clear what I want?

Thanks for help!

cbmserv...@gmail.com

unread,
Oct 18, 2022, 4:24:29 PM10/18/22
to google-apps-sc...@googlegroups.com

When you define a range in google spreadsheets, you provide any of the following options:

 

 

geRange(row, column)

Range

Returns the range with the top left cell at the given coordinates.

geRange(row, column, numRows)

Range

Returns the range with the top left cell at the given coordinates, and with the given number of rows.

geRange(row, column, numRows, numColumns)

Range

Returns the range with the top left cell at the given coordinates with the given number of rows and columns.

geRange(a1Notation)

Range

Returns the range as specified in A1 notation or R1C1 notation.

 

 

In your case since you only care about one cell, You should use the top option which if to only specify 2 parameters to specify that one cell.

 

If you want the last row, last column cell, use this:

const clientData = clientSheet.getRange(currentSheet.getLastRow(),currentSheet.getLastRow(),).getDisplayValue();

--
You received this message because you are subscribed to the Google Groups "Google Apps Script Community" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-apps-script-c...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/google-apps-script-community/904f4052-5746-4cd2-8163-2f58c58c4c48n%40googlegroups.com.

froot

unread,
Oct 18, 2022, 4:31:02 PM10/18/22
to Google Apps Script Community
I don't think I made myself clear enough
I have a range of 2 rows but the width to the right varies.
I want the range to span to the right up to the last cell that is filled.

cbmserv...@gmail.com

unread,
Oct 18, 2022, 4:34:29 PM10/18/22
to google-apps-sc...@googlegroups.com

Ok. I understand what you are trying to do but this can not be done simply by specifying the range.

 

Only thing that google spreadsheets provides is getLastColumn() method which gives you the last column of the sheet. If some rows do not fill that far, it is up to you to figure out how to find that.

 

What you may need is a simple loop to go through the data and determine where the end of data is (first blank cell perhaps), then use that info to determine what is the more right side cell is.

froot

unread,
Oct 18, 2022, 4:59:23 PM10/18/22
to Google Apps Script Community
thanks! that helps a lot actually. I figured it out. Since I loop through the array later in the script anyway, I threw in a condition there. 
Reply all
Reply to author
Forward
0 new messages