Skip to content

Instantly share code, notes, and snippets.

@ExcelRobot
Last active October 6, 2022 19:28
Show Gist options
  • Select an option

  • Save ExcelRobot/926df8b9889b39dc92549495a3144860 to your computer and use it in GitHub Desktop.

Select an option

Save ExcelRobot/926df8b9889b39dc92549495a3144860 to your computer and use it in GitHub Desktop.
Index Wrap LAMBDA Function
/*
Name: Index Wrap (IndexWrap)
Description: Similar to INDEX but if the position is too high, it'll wrap around.
Parameters:
array - Required. A range of cells or an array constant.
index - Required. The row (or column) position to return from array. If the position is greater than the number of rows (or columns), it wraps around.
[by_column] - Optional. True for columns, false for rows. Default: False.
Source: Excel Robot (@ExcelRobot)
Gist URL: https://gist.github.com/ExcelRobot/926df8b9889b39dc92549495a3144860
Dependencies:
TOROW - https://gist.github.com/ExcelRobot/efd47694a3003a77682db0c6b3f7e7d9
*/
IndexWrap = LAMBDA(array,index,[by_column],LET(
\\LambdaName, "IndexWrap",
\\CommandName, "Index Wrap",
\\Description, "Similar to INDEX but if the position is too high, it'll wrap around.",
\\Parameters, {
"array","Required. A range of cells or an array constant. ";
"index","Required. The row (or column) position to return from array. If the position is greater than the number of rows (or columns), it wraps around.";
"[by_column]","Optional. True for columns, false for rows. Default: False."
},
\\Source, "Excel Robot (@ExcelRobot)",
\\gistURL, "https://gist.github.com/ExcelRobot/926df8b9889b39dc92549495a3144860",
\\Dependencies, {
"TOROW","https://gist.github.com/ExcelRobot/efd47694a3003a77682db0c6b3f7e7d9"
},
Flip, OR(ISOMITTED(by_column),NOT(by_column)),
RowCount, IF(Flip,ROWS(array),COLUMNS(array)),
ColumnCount, IF(Flip,COLUMNS(array),ROWS(array)),
ModIndex, MOD(TOROW(index)-1,RowCount)+1,
ModIndexArray, IF(COLUMNS(TOROW(index))<>ColumnCount,SEQUENCE(1,ColumnCount,INDEX(ModIndex,1,1),0),ModIndex),
ColSeq, SEQUENCE(1,ColumnCount),
Direction, BYCOL(ColSeq,LAMBDA(x,INDEX(IF(Flip,INDEX(array,SEQUENCE(RowCount),x),INDEX(array,x,ColSeq)),INDEX(ModIndexArray,1,x)))),
Direction
)
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment