Forum Discussion
How to wrap table, not vector (WRAPCOLS for tables)
Zdenek_Moravec Here's a few more options to add to the mix...
METHOD 1: the MAKEARRAY / INDEX method (slowest)
=LET(arr, T_Data, wrap_count, 10, pad_with, "",
w, COLUMNS(arr),
n, CEILING.MATH(ROWS(arr)/wrap_count*w, w),
MAKEARRAY(wrap_count, n, LAMBDA(r,c, LET(
row, (ROUNDUP(c/w, 0)-1)*wrap_count+r,
col, MOD(c-1, w)+1,
IFERROR(INDEX(arr, row, col), pad_with)))))
Also, to repeat the headers the appropriate number of times, the TOROW function can be used with IF & SEQUENCE:
=LET(arr, T_Data[#All], wrap_count, 10,
w, COLUMNS(arr),
n, CEILING.MATH(ROWS(DROP(arr, 1))/wrap_count*w, w),
TOROW(IF(SEQUENCE(n/w), TAKE(arr, 1))))
To make things easier, you can also define a custom LAMBDA function in Name Manager called WRAPTABLE by combining both formulas into a single function:
=LAMBDA(array,wrap_count,[header],[pad_with], LET(
data, IF(header, DROP(array, 1), array),
w, COLUMNS(data),
n, CEILING.MATH(ROWS(data)/wrap_count*w, w),
v, MAKEARRAY(wrap_count, n, LAMBDA(r,c,
IFERROR(INDEX(data, (ROUNDUP(c/w, 0)-1)*wrap_count+r, MOD(c-1, w)+1), pad_with))),
IF(header, VSTACK(TOROW(IF(SEQUENCE(n/w), TAKE(array, 1))), v), v)))
Once defined, the WRAPTABLE function can be used as follows:
=WRAPTABLE(T_Data[#All], 10, TRUE)
Note: If the specified array includes a header row, set the optional [header] argument to 1 or TRUE.
To return the data for "Biology" or "Chemistry" wrapped by 4 records, as demonstrated on the two worksheets in your sample file, the WRAPTABLE function can be adapted as follows:
=WRAPTABLE(FILTER(DROP(T_Data,, 1), TAKE(T_Data,, 1)=A1), 4,, "")
METHOD 2: the REDUCE / HSTACK method, with WRAPCOLS, TOCOL, WRAPROWS & CHOOSECOLS (faster)
=LET(arr, T_Data, wrap_count, 10, pad_with, "",
w, COLUMNS(arr),
a, WRAPCOLS(TOCOL(arr), w*wrap_count, pad_with),
DROP(REDUCE(0, SEQUENCE(COLUMNS(a)), LAMBDA(v,n,
HSTACK(v, WRAPROWS(CHOOSECOLS(a, n), w)))),, 1))
Which can also be used to define the custom WRAPTABLE function as follows:
=LAMBDA(array,wrap_count,[header],[pad_with], LET(
data, IF(header, DROP(array, 1), array),
w, COLUMNS(data),
a, WRAPCOLS(TOCOL(data), w*wrap_count, pad_with),
b, DROP(REDUCE(0, SEQUENCE(COLUMNS(a)), LAMBDA(v,n,
HSTACK(v, WRAPROWS(CHOOSECOLS(a, n), w)))),, 1),
IF(header, VSTACK(TOROW(IF(SEQUENCE(COLUMNS(b)/w), TAKE(array, 1))), b), b)))
METHOD 3: WRAPROWS with SORTBY, TOCOL & EXPAND (fastest)
=LET(arr, T_Data, wrap_count, 10, pad_with, "",
n, ROUNDUP(ROWS(arr)/wrap_count, 0),
h, wrap_count*n,
w, COLUMNS(arr),
a, MOD(INT(SEQUENCE(h*w,, 0)/w), wrap_count)+1,
WRAPROWS(SORTBY(TOCOL(EXPAND(arr, h,, pad_with)), a), n*w))
Which can also be used to define the custom WRAPTABLE function as follows:
=LAMBDA(array,wrap_count,[header],[pad_with], LET(
data, IF(header, DROP(array, 1), array),
n, ROUNDUP(ROWS(data)/wrap_count, 0),
h, wrap_count*n,
w, COLUMNS(data),
a, MOD(INT(SEQUENCE(h*w,, 0)/w), wrap_count)+1,
b, WRAPROWS(SORTBY(TOCOL(EXPAND(data, h,, pad_with)), a), n*w),
IF(header, VSTACK(TOROW(IF(SEQUENCE(n), TAKE(array, 1))), b), b)))
Please see the attached files, which contain all of the above examples...