Search code examples
google-sheets-formulaline-breaks

Google Sheet - Query - Line Break


I have this query formula:

IF(A8="","",IFNA(ARRAYFORMULA(TEXTJOIN(", ",TRUE,QUERY(Sheet6!B5:K,"SELECT F, I, J, K WHERE C = DATE'" & TEXT(A8,"yyyy-mm-dd") & "' AND G = 2",0)))))

It uses TEXTJOIN to return the values in a single cell like this:

A1, B1, C1, D1, A2, B2, C2, D2, A3, B3, C3, D3

I want this to remain in a single cell, but for the cell to show with line breaks as follows:

A1, B1, C1, D1
A2, B2, C2, D2
A3, B3, C3, D3

A1, A2 and A3 are names and contain spaces.

How can I adjust this to match the 2nd excerpt? Any help will be greatly appreciated.


Solution

  • Try using a Regular expression to replace every 4th comma with the CHAR(10) value. This is what I believe you are looking for:

    =REGEXREPLACE(TEXTJOIN(", ", TRUE, QUERY(Sheet6!B5:K, "SELECT F, I, J, K WHERE C = DATE'" & TEXT(A8, "yyyy-mm-dd") & "' AND G = 2", 0)),"((?:[^,]*,){3}[^,]*), ", "$1"&CHAR(10))

    • (?:[^,]*,){3} This part matches any sequence of characters followed by a comma, repeated exactly 3 times.
    • [^,]* This matches the characters after the 3rd comma until the next comma.
    • , This matches the 4th comma. Including a space to account for the , .
    • The $1 that we replace the comma with preserves the content after the comma.