Your task is hopeless because when only importing a single column there is no second column. However querying say Sheet1!A2:B3000
instead will still not work with COL2
because that is case sensitive, just like column b
selection does not work in a query in place of B
. Maybe try:
=QUERY(IMPORTRANGE("key","Sheet1!A2:B3000"),"SELECT * WHERE Col2='JB'")
I offer two versions, depending on whether blank cells should be kept. The basic idea is the same:
- Pick a character that is not contained in your table (e.g., an exotic Unicode character accessible via
CHAR
).
- Use it in the
JOIN
command for each of three columns.
- Concatenate the results, adding the character in between.
SPLIT
the concatenated string by the same character.
TRANSPOSE
the result.
For example:
=TRANSPOSE( SPLIT( JOIN(CHAR(57344),A1:A) &CHAR(57344)& JOIN(CHAR(57344),B1:B) &CHAR(57344)& JOIN(CHAR(57344),C1:C), CHAR(57344) ) )
where for joining/splitting, I picked the character CHAR(57344)
, which is designated for private use and therefore should not be present in any valid input.
The above formula removes blank entries, because this is what SPLIT
does. This may be convenient if your data does not have blanks: saves you the trouble of tracking where the last row of the data is. But other times, blanks should be preserved.
To preserve blanks, I follow the answer by Jacob Jan Tuinstra except that I used another private use Unicode instead of space (who knows, maybe you have some cells that contain just a space and you want to keep those). This involves two extra steps: after joining everything as before, I use SUBSTITUTE
to replace CHAR(57344)
by CHAR(57344)&CHAR(57345)
, then split as before, and replace CHAR(57345)
by empty string.
=TRANSPOSE( ARRAYFORMULA(SUBSTITUTE( SPLIT( SUBSTITUTE(...joins... , CHAR(57344),CHAR(57344)&CHAR(57345)), CHAR(57344) ), CHAR(57345), "")))
The full result looks like this (notice that now I specify where the last row of data is):
=TRANSPOSE( ARRAYFORMULA(SUBSTITUTE( SPLIT( SUBSTITUTE(JOIN(CHAR(57344),A1:A9) &CHAR(57344)& JOIN(CHAR(57344),B1:B9) &CHAR(57344)& JOIN(CHAR(57344),C1:C9), CHAR(57344),CHAR(57344)&CHAR(57345)), CHAR(57344) ), CHAR(57345), "" )))
One more caveat: strings in Google Sheets cannot exceed 50000 characters in length. If your data is too large for string-based approach to work, use a script (this one is a good place to start).
Best Answer
To add columns based on data, use the Spreadsheet Service or the Spreadsheet Advanced Service of Google Apps Script.
You should decide how the code will be called. This could be from the Apps Script editor, from custom Google Sheets UI buttons, menus, dialogs, sidebars or by using triggers.
As an alternative you could use Google Sheets add-on or the Google Sheets API.