Skip to content

Instantly share code, notes, and snippets.

@Medohh2120
Last active March 16, 2026 05:07
Show Gist options
  • Select an option

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

Select an option

Save Medohh2120/57784c259a3763024e885cb636d28226 to your computer and use it in GitHub Desktop.
Dynamic array transformation toolkit for Excel LAMBDA functions
/*
Name: REPLACE_VECTOR
Description: Replaces targeted rows and/or columns in an array with given "new_arrays".
If multiple indices are supplied per axis, the same "new_array" is placed at each target.
preserves the original order of the remaining rows/columns.
Optional "pad_with" replaces dimension-mismatch errors in the final result.
Made By: Medohh2120
*/
REPLACE_VECTOR = LAMBDA(array, [row_idxs], [new_row_array], [col_idxs], [new_col_array], [pad_with],
LET(
/* row processing block */
AfterRows, IF(ISOMITTED(row_idxs), array,
REDUCE(array, SORT(TOCOL(row_idxs), , -1),
LAMBDA(arry, RowIdx, // Iterate backwards to preserve row indices.
LET(
TotalRows, ROWS(arry),
Top, IF(RowIdx = 1, "", TAKE(arry, RowIdx - 1)), //if replace first row -> no top piece.
Bottom, IF(RowIdx = TotalRows, "", DROP(arry, RowIdx)), //if replace last row-> no bottom piece.
IF(
RowIdx = 1,
VSTACK(new_row_array, Bottom),
IF(RowIdx = TotalRows, VSTACK(Top, new_row_array), VSTACK(Top, new_row_array, Bottom))
)
)
)
)
),
/* column processing block */
AfterCols, IF(ISOMITTED(col_idxs), AfterRows,
REDUCE(AfterRows, SORT(TOCOL(col_idxs), , -1),
LAMBDA(arry, ColIdx, // Iterate backwards to preserve column indices.
LET(
TotalCols, COLUMNS(arry),
Left, IF(ColIdx = 1, "", TAKE(arry, , ColIdx - 1)), //if replace 1st column -> no Left piece.
Right, IF(ColIdx = TotalCols, "", DROP(arry, , ColIdx)), //if replace last column -> no right piece.
IF(
ColIdx = 1,
HSTACK(new_col_array, Right),
IF(ColIdx = TotalCols, HSTACK(Left, new_col_array), HSTACK(Left, new_col_array, Right))
)
)
)
)
),
/* final error padding block */
IF(ISOMITTED(pad_with), AfterCols, IFERROR(AfterCols, pad_with))
)
);
/*
Name: DROP_VECTOR
Description: Drops targeted rows and/or columns from an array by index,
while preserving the original order of the remaining rows/columns.
Made By: Medohh2120
*/
DROP_VECTOR = LAMBDA(array, [row_idxs], [col_idxs],
LET(
/* defaults & base arrays block */
RowIndices, SEQUENCE(ROWS(array)),
ColIndices, SEQUENCE(, COLUMNS(array)),
// When omitted, provide an empty string "" so XMATCH finds nothing
sRowIdxs, IF(ISOMITTED(row_idxs), "", SORT(TOCOL(row_idxs))),
sColIdxs, IF(ISOMITTED(col_idxs), "", SORT(TOROW(col_idxs),,,TRUE)),
/* row processing (binary search 0, 2) */
filterdRowIndices, FILTER(RowIndices, ISNA(XMATCH(RowIndices, sRowIdxs, 0, 2))),
/* column processing (binary search 0, 2) */
filterdColIndices, FILTER(ColIndices, ISNA(XMATCH(ColIndices, sColIdxs, 0, 2))),
/* final extraction */
INDEX(array, filterdRowIndices, filterdColIndices)
)
);
/*
Name: REPLACECELLS
Description: Replace array values by rowcol(cell) index.
Supports 1:1 or 1:M replacement (Multiple replacements).
Made By: Medohh2120
*/
REPLACECELLS = LAMBDA(array, rowcol_codes, replacement_values,
LET(
// broadcasting to enable 1:1 or 1:M
rep_aligned, IF(rowcol_codes, replacement_values),
// computed once, reused per iteration
cellmap, MAKEARRAY(ROWS(array), COLUMNS(array), LAMBDA(r, c, --(r & c))),
REDUCE(array,SEQUENCE(COUNTA(rowcol_codes)),
LAMBDA(arry, idx,
IF(cellmap = INDEX(rowcol_codes,idx), index(rep_aligned,idx), arry)
)
)
)
);
@Medohh2120
Copy link
Author

Untitled_deيsign

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