Graeme Muir
Graeme Muir

Reputation: 29

How do I select a range of columns from a sheet with a gap in the middle

I am writing a Google Apps Script for a Google Sheet and I want to select from a range of columns but exclude several columns in the middle.

I have a sheet resembling

+-------+-----+--------+--------+-------+
| Name  | Age | Height | Income | State |
+-------+-----+--------+--------+-------+
| Bob   |  23 |    185 |  50000 | OH    |
| Jim   |  35 |    160 |  23000 | HI    |
| Frank |  24 |    142 |  44565 | CA    |
| Tom   |  24 |    155 |  23758 | NY    |
+-------+-----+--------+--------+-------+

And I want to select the first two columns and the last column like so

+-------+-----+-------+
| Name  | Age | State |
+-------+-----+-------+
| Bob   |  23 | OH    |
| Jim   |  35 | HI    |
| Frank |  24 | CA    |
| Tom   |  24 | NY    |
+-------+-----+-------+

sheet.getRange() works well enough getting a continuous range but what is the most compact way to combine two ranges with the same number of rows?

Upvotes: 1

Views: 6045

Answers (2)

player0
player0

Reputation: 1

the most simple (and faster than QUERY) is to use FILTER or ARRAYFORMULA like:

=ARRAYFORMULA({A:B, E:E})

=FILTER({A:B, E:E}, A:A<>"")

0

Upvotes: 1

pnuts
pnuts

Reputation: 59485

If Name is in A1, I suggest:

=query(A:E,"select A,B,E")

or copy/paste the entire table and delete the surplus columns.

Upvotes: 1

Related Questions