Search code examples
google-sheetsrowmultiple-columns

Google Sheets: Transform rows to columns with column breaks at 15 lines **with row data being kept together (the hard part)


I have functional code that turns rows to columns and breaks the columns at 15 lines. That code is seen below.

=WRAPCOLS(FILTER(TOCOL('Source Sheet'!A3:E30,,0),LEN(TOCOL('Source Sheet'!A3:E30,,0))),15,)

The problem is that the 15-line limit breaks row data into separate columns when it gets to the 15th line. I need row data to remain together in a column. That means I need to force an early column break if a row's data will be broken into two columns or I need to fill the rest of a column with blank-spaced cells so that the row data that would have been divided begins in the next new column and remains together.

This issue can be seen in the example sheet below. Many of the rows of data in the Source Sheet end up divided at row 15 in the Display Sheet. https://docs.google.com/spreadsheets/d/1qcRUtXKJ6y21G3leRf9EQ98xB4oyViX_Mr2sqVvGs2c/

Can you think of a way to ensure that row data is displayed completely in columns without being broken between columns?


Solution

  • Here's one approach you may test out:

    =let(Λ,index(match(,0/(A:A<>""))), Ξ,indirect("A3:D"&Λ), Σ,hstack(Ξ,wrapcols("🐠",rows(Ξ),"🐠")),
         Δ,query(reduce({0,1},sequence(rows(Σ)),lambda(a,c,let(σ,index(a,c,1)+counta(index(Σ,c)),vstack(a,if((σ-1)>15,{counta(index(Σ,c)),index(a,c,2)+1},{σ,index(a,c,2)}))))),"select Col2 offset 1"),
         reduce(torow(,1),sequence(max(Δ)),lambda(a,c,ifna(hstack(a,index(substitute(tocol(filter(Σ,Δ=c),1),"🐠",)))))))
    

    enter image description here