Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get all Rows from QUERY() into one cell in a Google Spreadsheet

I have the following formula in a spreadsheet:

=IFERROR( JOIN( CHAR(10), QUERY(Schedule!$A$2:$E, "SELECT C, D, E Where A = '" & B$2 & "' AND B = timeofday '" & text($A4, "HH:MM:SS") & "' AND C = '" & $C$1 & "'", -1)),)

It works great when it returns one row. It does not work if it returns multiple rows. How can I get it to use all rows returned with a CHAR(10) included between rows as well as columns. Basically I want all the returned data separated by line breaks. So when the QUERY returns

Item1 Item2 Item3

Item1A Item2A Item3A

it is changed to

Item1

Item2

Item3

Item1A

Item2A

Item3A

My goal is to put all the returned data in 1 cell with a line break between each item. Any way I can make this happen?

Here is a copy of the file. Cells B3 to F4 are good sample cells where the formula reside.

like image 750
Karl_S Avatar asked Oct 18 '25 15:10

Karl_S


1 Answers

The solution is imperfect, it adds extra spaces before each line:

  Item1 Item2 Item3
  Item1A Item2A Item3A
^ spaces

Step1

Get origilal formula + extra column in query:

=QUERY(data, "SELECT C, D, E, '@@' Where" & ... & "' label '@@' ''", -1)

The result:

  Item1  Item2  Item3  @@
  Item1A Item2A Item3A @@

Step2

Replace "@@" with char(10)

=ARRAYFORMULA(SUBSTITUTE(step1Formula,"@@",char(10)))

  Item1  Item2   Item3 

  Item1A Item2A  Item3A
                          ^ new lines

Step3

Use concatenate:

=ArrayFormula(" "&TRIM(CONCATENATE(" "&B16:E17)))

Final formula

=ArrayFormula(" "&TRIM(CONCATENATE(" "&SUBSTITUTE(QUERY(Schedule!$A$2:$E, "SELECT C, D, E, '@@' Where A = '" & B$2 & "' AND B = timeofday '" & text($A3, "HH:MM:SS") & IF(OR($C$1 = "*",ISBLANK($C$1)), "", "' AND C = '" & $C$1) & IF(OR($F$1 = "*",ISBLANK($F$1)), "", "' AND E = '" & $F$1) & "' label '@@' ''", -1),"@@",char(10)))))

Shared file:

https://docs.google.com/spreadsheets/d/1otcqvkXb5H3WTSi_exKw9UUcRqN8MKBZ5JYCmTLAWdQ/edit?usp=sharing

like image 154
Max Makhrov Avatar answered Oct 22 '25 04:10

Max Makhrov