Skip to content

Instantly share code, notes, and snippets.

@Medohh2120
Last active May 2, 2026 23:40
Show Gist options
  • Select an option

  • Save Medohh2120/ff8512cb0d5e72024b8b3aeeda46aa91 to your computer and use it in GitHub Desktop.

Select an option

Save Medohh2120/ff8512cb0d5e72024b8b3aeeda46aa91 to your computer and use it in GitHub Desktop.
Wraps a column into rows at every occurrence of a trigger value. Use case: each “record” is a vertical block cells, and blocks are separated a given separator. The blocks are not a fixed size.
/*
Name: WRAPROWS_AT
Description: Wraps a column into rows at every occurrence of a trigger value.
Supports multiple triggers via array constant {"start", "end", ...}.
⚠ Triggers are not case-sensitive "end" and "END" are treated the same.
Consecutive triggers in data are automatically treated as one -> no empty groups produced.
Groups are internally joined via CHAR(1) as a safe delimiter, never colliding with real data.
Optional "pad_with" pads shorter rows to uniform width.
⚠ Note: TEXTJOIN cap applies -> max ~32,000 chars per group.
Made By: Medohh2120
*/
WRAPROWS_AT = LAMBDA(column, triggers, [pad_with],
LET(
pad_with, IF(ISOMITTED(pad_with), "", pad_with),
is_trigger, MAP(column, LAMBDA(r, OR(r = triggers))), // We didn't use XMATCH -> can't deal with "" cases.
group_iDs, SCAN(1, column, LAMBDA(acc, cell, acc + OR(cell = triggers))), // Counts triggers seen so far -> group ID per cell
Joined_groups, DROP(
GROUPBY(group_iDs, column,
LAMBDA(x, TEXTJOIN(CHAR(1), , x)), 0, 0, , NOT(is_trigger) //safe join: use a rare char -- guaranteed not to appear in data.
)
,,1
),
TEXTSPLIT_COLUMN(Joined_groups, CHAR(1), , , pad_with)
)
);
/*
Name: TEXTSPLIT_COLUMN
Description: This is called by WRAPROWS_AT to split results.
Splits a 1D column of text by a delimiter and expands it horizontally.
Optional "pad_with" replaces errors in the final result.
Performance: High
Made By: Medohh2120
*/
TEXTSPLIT_COLUMN = LAMBDA(column, delimiter, [ignore_empty], [match_mode], [pad_with],
LET(
Width, MAX(MAP(column, LAMBDA(r, COLUMNS(TEXTSPLIT(r, delimiter, , ignore_empty, match_mode))))),
/* If omitted, Excel treats pad_with as 0. ISOMITTED forces it to an error. */
safe_pad, IF(ISOMITTED(pad_with), #N/A, pad_with),
result, DROP(
REDUCE("",SEQUENCE(Width),
LAMBDA(acc, word_idx,
LET(
/* Create a single column for this word index */
NewCol, MAP(
column,
LAMBDA(cell,
INDEX(TEXTSPLIT(cell, delimiter, , ignore_empty, match_mode), word_idx)
)
),
HSTACK(acc, NewCol)
)
)
),,1
),
IFERROR(result,safe_pad)
)
);
@Medohh2120

Copy link
Copy Markdown
Author
image

You can find a file with an example Here

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment