Skip to content

Add CHOOSEROWS function (HF-385) - #1827

Merged
Tobiadefami merged 29 commits into
developfrom
feature/HF-385
Nov 16, 2026
Merged

Tobiadefami merged 29 commits into
developfrom
feature/HF-385

Conversation

@Tobiadefami

@Tobiadefami Tobiadefami commented Oct 15, 2026 •

Copy link
Copy Markdown
Collaborator

Context

This PR adds the CHOOSEROWS dynamic-array function (HF-385). CHOOSEROWS(array, row_num1, [row_num2], ...) returns the requested rows of an array, in argument order.

Important

Stacked on #1734 (CHOOSECOLS). CHOOSEROWS is CHOOSECOLS on the other axis and shares its code, so this branch is based on feature/choosecols-function. Until #1734 merges, this diff also shows CHOOSECOLS's commits. The CHOOSEROWS changes are the commits feat: add CHOOSEROWS function (HF-385), docs: link the CHOOSEROWS pull request in the changelog and the license table entry. Once #1734 merges, develop will be merged into this branch and only these changes will remain in the diff.

Behavior, checked against Microsoft Excel Online (en-US):

  • Rows come back in argument order, and a row can be chosen more than once.
  • Negative indexes count from the bottom: CHOOSEROWS(A1:C3, -1) is the last row.
  • Indexes truncate toward zero. An index of 0, an index that truncates to 0 (0.5, -0.5), and an index past either end return #VALUE!.
  • Numeric text, percentages and TRUE coerce to row numbers. FALSE, "", a blank cell, an empty index and non-numeric text return #VALUE!.
  • An error in the source or in an index is the result. The source's error comes first, then the indexes are read from left to right, and the range checks come last, so CHOOSEROWS(A1:C3, 0, NA()) returns #N/A.
  • Errors, text and blank cells inside the chosen rows are kept as elements; errors in rows that are not chosen don't matter.
  • Scalars, inline arrays, ranges and computed arrays are accepted as sources. The result is a value, not a reference.

Note: Microsoft's CHOOSEROWS page describes its CHOOSEROWS(A2:B7,-1,-2) example as returning row four and then row three. On the page's six-row data, Excel returns rows 6 and 5, as the formula says, and this implementation follows Excel.

Implementation:

  • CHOOSEROWS lives in ArrayPlugin, next to CHOOSECOLS, with the same metadata.
  • CHOOSECOLS's code is refactored into shared helpers, with no change to its results. Its evaluation and spill-size prediction move into two private methods parameterised by the axis, chooseAlongAxis and chooseArraySize, and choosecols/choosecolsArraySize now delegate to them. parseChooseColsLiteralIndex is renamed parseChooseLiteralIndex; its body is unchanged. CHOOSECOLS's spec passes unchanged.
  • Spill-size prediction reserves one row per index, as wide as the source. Invalid literal indexes return their error instead of #SPILL!. Rows are read only for the chosen indexes.
  • A whole-row source spills its effective width when the formula is in column A and returns #SPILL! elsewhere. Indexes into a whole-column source count against the sheet's used rows. Both are listed in list-of-differences.md.
  • A named expression, or a function whose result size depends on cell values, passed directly as the source, has a size that can't be predicted before evaluation. CHOOSEROWS returns #VALUE! for it when the result has more than one cell.
  • The function metadata catalogue entry, the license capability table entry, the changelog, a CHOOSEROWS function section in known-limitations.md and two rows in list-of-differences.md are included.
  • Localized names were read from Excel Online in every locale by switching the session language and reading the formula bar. Each pairs with CHOOSECOLS's name in the same language (for example ZEILENWAHL/SPALTENWAHL and SCEGLI.RIGA/SCEGLI.COL). Indonesian keeps the English name, because Excel Online shows English function names in that locale.

Known limitations, documented in known-limitations.md:

  • Row indexes must be separate scalar arguments. Excel also accepts an array or a range of indexes (CHOOSEROWS(A1:C3, {1,3})); CHOOSEROWS follows CHOOSECOLS here and doesn't support it.
  • A source whose size can't be predicted (above) returns #VALUE! when the result has more than one cell.

How did you test your changes?

  • Tests PR: handsontable/hyperformula-tests#95. It adds 120 CHOOSEROWS tests: 99 assert Excel's result, and 21 pin documented differences (arrays and ranges of indexes, whole-column and whole-row sources, a missing row index, and a date before March 1900).
  • Expected values come from 129 cases measured in Microsoft Excel Online (en-US) on the same data the tests use, so any of them can be re-checked in Excel.
  • Full Jest suite with the private tests, after merging Feature/choosecols function #1734 and develop: 529 suites, 7,395 tests passed, 3 skipped. CHOOSECOLS's 54 tests pass unchanged.
  • CHOOSECOLS's results before and after the refactor were compared on 696 formulas (index spellings, error pairs, scalar, array and computed sources, whole columns and rows, nesting, named expressions): all identical.
  • Breaking the code on purpose made the tests fail: checking row indexes against the source's width failed 15 tests, and counting negative indexes from the top failed 7 of the 120 CHOOSEROWS tests (and 6 of CHOOSECOLS's, which share the code).
  • npm run verify:typings, npm run lint (0 errors), npm run docs:generate-function-docs and git diff --check pass.

AI assistance

Claude Code wrote the code, tests and docs. I reviewed it.

Compatibility with Excel and Google Sheets

Excel: Microsoft Excel Online (en-US). Google Sheets: not checked. In the examples, A1:C3 holds 1 to 9 row by row.

Formula Excel Google Sheets HyperFormula
=CHOOSEROWS(SEQUENCE(6,2),1,3,5,1) 1, 2 / 5, 6 / 9, 10 / 1, 2 – 1, 2 / 5, 6 / 9, 10 / 1, 2
=CHOOSEROWS(SEQUENCE(6,2),-1,-2) 11, 12 / 9, 10 – 11, 12 / 9, 10
=CHOOSEROWS(A1:C3,-1) 7, 8, 9 – 7, 8, 9
=CHOOSEROWS(A1:C3,200%) 4, 5, 6 – 4, 5, 6
=CHOOSEROWS(A1:C3,TRUE()) 1, 2, 3 – 1, 2, 3
=CHOOSEROWS(A1:C3,0) #VALUE! – #VALUE!
=CHOOSEROWS(A1:C3,0,NA()) #N/A – #N/A

The deliberate differences (an array or range of indexes, whole-row and whole-column sources, a source whose size can't be predicted) are listed in list-of-differences.md and known-limitations.md.

Public API changes

None. One new built-in function.

Out of scope and follow-ups

  • An array or range of indexes in one argument is not supported, as for CHOOSECOLS; it is listed in the known limitations.

Types of changes

  • Breaking change (existing functionality no longer works as before) – agreed with the maintainers
  • New feature or improvement (a non-breaking change that adds functionality)
  • Bug fix (a non-breaking change that fixes an issue)
  • Additional language file, or a change to an existing language file (translations)
  • Change to the documentation

Related issues

  1. HF-385 (CHOOSEROWS).
  2. Depends on Feature/choosecols function #1734 (CHOOSECOLS).
  3. Tests: handsontable/hyperformula-tests#95

Checklist

🤖 Generated with Claude Code


Note

Medium Risk
New dynamic-array spill and size-prediction paths touch array evaluation and worksheet-edge behavior; regressions would affect formulas using these functions but not existing callers unless shared helpers mis-handle CHOOSECOLS.

Overview
Adds Excel-style CHOOSECOLS and CHOOSEROWS dynamic-array functions that return selected columns or rows (in index order, with negative indexing and duplicates), wired through ArrayPlugin with shared chooseAlongAxis / chooseArraySize helpers, literal-index parsing for spill-size prediction, and vectorizationForbidden.

Documents spill and indexing limits (scalar indexes only, whole row/column behavior, #SPILL! vs runtime errors) in known-limitations, Excel/Sheets differences in list-of-differences, and changelog entries. Registers metadata, localized names across language packs, and lists both functions in the license ungrouped capability table.

Reviewed by Cursor Bugbot for commit 9367449. Bugbot is set up for automated code reviews on this repo. Configure here.

Tobiadefami and others added 12 commits August 13, 2026 21:52
Add CHOOSEROWS to ArrayPlugin. CHOOSECOLS's evaluation and spill-size
prediction move into private helpers parameterised by axis, which both
functions now use; CHOOSECOLS's results are unchanged.

Also add the catalogue entry, the localized names, the known
limitations, two list-of-differences rows and the changelog entry.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@cla-external-contractor-signup

Copy link
Copy Markdown

@Tobiadefami thanks for the pull request. No CLA step needed here — our records show you signed the Contributor License Agreement on 2026-07-31. That signature came from our previous signing form and has been carried over, so there is nothing for you to re-sign.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@cloudflare-workers-and-pages

cloudflare-workers-and-pages Bot commented Oct 15, 2026 •

Copy link
Copy Markdown

Deploying with  Cloudflare Workers  Cloudflare Workers

The latest updates on your project. Learn more about integrating Git with Workers.

Status Name Latest Commit Preview URL Updated (UTC)
✅ Deployment successful!
View logs
hyperformula-docs 7811bab Commit Preview URL

Branch Preview URL
Nov 16 2026, 11:49 AM

@github-actions

github-actions Bot commented Oct 15, 2026 •

Copy link
Copy Markdown

Performance comparison of head (7811bab) vs base (d31ed15)

                                     testName |    base |    head |  change
---------------------------------------------------------------------------
                                      Sheet A |  351.33 |  331.57 |  -5.62%
                                      Sheet B |  112.56 |  104.22 |  -7.41%
                                      Sheet T |   96.34 |   93.12 |  -3.34%
                                Column ranges |  482.15 |  474.72 |  -1.54%
                                Sorted lookup | 15723.9 | 15747.1 |  +0.15%
Sheet A:  change value, add/remove row/column |   10.28 |   11.87 | +15.47%
 Sheet B: change value, add/remove row/column |   87.02 |   94.41 |  +8.49%
                   Column ranges - add column |  117.33 |  123.56 |  +5.31%
                Column ranges - without batch |  350.94 |  362.79 |  +3.38%
                        Column ranges - batch |   88.35 |   83.51 |  -5.48%

Tobiadefami and others added 2 commits October 21, 2026 15:58
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Tobiadefami and others added 8 commits November 2, 2026 09:04
The evaluation returned #SPILL! for any whole-column source below row 1,
also inside SUM where nothing spills. The size prediction now keeps the
unbounded height there instead, so only a spilled result returns #SPILL!.

Correct the whole-column row in the list of differences (HyperFormula
spills the source's current height, Excel all rows) and say in the known
limitations that an array of indexes uses its first index.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Below the first row a spilled result over an empty whole column returns
#SPILL!, like any other whole-column result there.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…o feature/HF-385

# Conflicts:
#	docs/guide/list-of-differences.md
#	hyperformula/src/interpreter/plugin/ArrayPlugin.ts
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…-function

# Conflicts:
#	CHANGELOG.md
#	docs/guide/known-limitations.md
#	hyperformula/src/license/functionCapabilities.ts
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

@marcin-kordas-hoc marcin-kordas-hoc left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

blocking: the branch is 18 commits behind #1734 and conflicts with develop in CHANGELOG.md, known-limitations.md and functionCapabilities.ts, so merging #1734's current head and develop here would let CI run on the tree that would be merged.

blocking: the description needs the current template, whose AI assistance section the Definition of Done asks for when an AI tool wrote the change; all 3 commits of this PR are co-authored by Claude, and the Compatibility, Public API changes and Out of scope sections are missing too.

nit: the description still says the license capability entry comes with a later merge, but 9367449 already adds CHOOSEROWS to the table.

question: counting negative indexes from the top fails 7 of the 120 CHOOSEROWS tests when I mutate chooseAlongAxis, while the description says 12; was the description's mutant a different one?

I measured 149 cases in Excel Online through ROWS, COLUMNS and INDEX, and HyperFormula matches 143; the other six are the documented array of indexes and behaviours CHOOSECOLS has too. The full Jest suite passes at both heads (6,914 tests).

@Tobiadefami Tobiadefami mentioned this pull request Nov 12, 2026
7 of 12 tasks
…o feature/HF-385

# Conflicts:
#	docs/guide/list-of-differences.md
#	hyperformula/src/license/functionCapabilities.ts
@Tobiadefami

Copy link
Copy Markdown
Collaborator Author

Merged #1734's current head, which includes today's develop; the conflicts were the license table, where CHOOSEROWS now sits next to develop's CONCAT, and the list of differences. Updated the description to the current template and removed the license-entry sentence. On the mutant: it's the same one. Counting negative indexes from the top fails 7 of the 120 CHOOSEROWS tests, as you found, and 6 of CHOOSECOLS's, because chooseAlongAxis is shared; the description's 12 counted both specs (12 of 166 at the time). It now says 7 of 120.

@marcin-kordas-hoc marcin-kordas-hoc left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The branch now has #1734's current head, and CI passed on the tree merged with develop at f94e5f3 with 7,395 tests. The description follows the template, with the AI assistance section, and no longer says the license entry comes later. Counting negative indexes from the top fails 7 of the 120 CHOOSEROWS tests and 6 of the 54 CHOOSECOLS tests here, which answers my question and matches the description. It merges after #1734.

Tobiadefami added a commit that referenced this pull request Nov 16, 2026
### Context

This PR adds `CHOOSECOLS`, allowing columns to be selected from an array
with positive or negative indexes while preserving the requested order
and duplicate indexes.

The implementation remains local to `ArrayPlugin`; `CHOOSEROWS` is
outside this PR's scope. It includes function metadata, translations,
changelog and compatibility documentation.

Behavior covered:

- Positive, negative, mixed, duplicate, fractional, coerced, referenced,
and calculated indexes.
- Scalar, range, whole-row, whole-column, and calculated-array inputs.
- Static spill-size prediction, including validating an empty or
literal-error index before spill allocation.
- Bounds checks, error propagation, blocked spills, lazy reads, and
argument validation.
- Whole-column sources spill from row 1 and return `#SPILL!` below row
1.
- A whole-column result that outgrows the area reserved for it, for
example after another spill makes the source taller, returns `#SPILL!`
instead of throwing, including when the formula is entered with
`setCellContents`.

### How did you test your changes?

- Tests PR: handsontable/hyperformula-tests#34, 54 focused `CHOOSECOLS`
tests.
- Full Jest suite with the private tests, after merging `develop`: 528
suites, 7,275 tests passed, 3 skipped.
- `npm run verify:typings`, `npm run lint` (0 errors) and `git diff
--check` pass.

### AI assistance

Claude Code wrote the code, tests and docs. I reviewed it.

### Compatibility with Excel and Google Sheets

Excel: Microsoft Excel Online. Google Sheets: the values in the list of
differences. In the examples, A1:C3 holds 1 to 9 row by row.

| Formula | Excel | Google Sheets | HyperFormula |
|---|---|---|---|
| `=SUM(CHOOSECOLS(A:A,1))` | 12 | – | 12 |
| `=SUM(CHOOSECOLS(A:C,2))` | 15 | – | 15 |
| `=COLUMNS(CHOOSECOLS(A1:C3,{1,2}))` | 2 | 2 | 1 |

Excel also confirmed the same-sheet and cross-sheet whole-column
behavior, whole-row indexes, and that an empty column index returns
`#VALUE!` even when the predicted spill range is blocked. The deliberate
differences (whole-column and whole-row sources, an array or range of
indexes, an empty whole-column source) are listed in
`list-of-differences.md` and `known-limitations.md`.

### Public API changes

None. One new built-in function.

### Out of scope and follow-ups

- `CHOOSEROWS` is in #1827.
- An array or range of indexes in one argument is not supported; it is
listed in the known limitations.

### Types of changes

- [ ] Breaking change (existing functionality no longer works as before)
– agreed with the maintainers
- [x] New feature or improvement (a non-breaking change that adds
functionality)
- [ ] Bug fix (a non-breaking change that fixes an issue)
- [x] Additional language file, or a change to an existing language file
(translations)
- [x] Change to the documentation

### Related issues

1. HF-141 (`CHOOSECOLS`).
2. Tests: handsontable/hyperformula-tests#34

### Checklist

- [x] My pull request meets the [Definition of
Done](https://github.com/handsontable/hyperformula/blob/develop/dev-docs/DEFINITION-OF-DONE.md).
External contributors: see also [Contributing to
HyperFormula](https://handsontable.com/docs/hyperformula/guide/contributing.html).
- [ ] I have signed the [Contributor License
Agreement](https://goo.gl/forms/yuutGuN0RjsikVpM2).
- [ ] I read my own diff end to end before requesting a review.
- [x] I added a
[CHANGELOG.md](https://github.com/handsontable/hyperformula/blob/develop/CHANGELOG.md)
entry, written by [the changelog
rules](https://github.com/handsontable/hyperformula/blob/develop/dev-docs/DOC-STANDARDS.md#the-changelog)
– or none is needed (docs, tests or tooling only).
- [x] I updated the documentation and JSDoc to match the change.
- [ ] Breaking change only: I added a section to the migration guide.
- [x] I updated the `dev-docs/` pages this change affects – or none
needed it.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

<!-- CURSOR_SUMMARY -->
---

> [!NOTE]
> **Medium Risk**
> New formula engine behavior around spill reservation and whole-column
edge cases can affect existing sheets using dynamic arrays; scope is
localized to ArrayPlugin but user-visible results differ from Excel in
documented cases.
> 
> **Overview**
> Adds **`CHOOSECOLS`** as a dynamic-array lookup function in
`ArrayPlugin`, selecting one or more columns from a range by
positive/negative indexes (with duplicates allowed) and returning a
spilled 2D result.
> 
> Spill sizing is handled by **`choosecolsArraySize`**, which validates
literal column indexes before allocation, derives height from
whole-column sources (including effective height on row 1), and leaves
unbounded height below row 1 so formulas surface **`#SPILL!`** when the
result would extend past the sheet edge. Runtime evaluation mirrors
Excel-style bounds checks and maps empty sources to **`#N/A`**.
> 
> Docs and product surfacing are updated: **CHANGELOG**, **known
limitations**, a **list-of-differences** vs Excel/Sheets, **function
metadata**, **localized names** across language packs, and inclusion of
**`CHOOSECOLS`** in the **license** capability list for ungrouped
functions.
> 
> <sup>Reviewed by [Cursor Bugbot](https://cursor.com/bugbot) for commit
6452f39. Bugbot is set up for automated
code reviews on this repo. Configure
[here](https://www.cursor.com/dashboard/bugbot).</sup>
<!-- /CURSOR_SUMMARY -->

---------

Co-authored-by: Kuba Sekowski <jakub.sekowski@handsontable.com>
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
develop's tree equals 0ca675b (the CHOOSECOLS head squash-merged as #1734), which this branch already merged.
@codecov

codecov Bot commented Nov 16, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 97.46%. Comparing base (d31ed15) to head (7811bab).

Additional details and impacted files

Impacted file tree graph

@@             Coverage Diff             @@
##           develop    #1827      +/-   ##
===========================================
+ Coverage    97.44%   97.46%   +0.01%     
===========================================
  Files          209      209              
  Lines        16742    16778      +36     
  Branches      3748     3777      +29     
===========================================
+ Hits         16314    16352      +38     
+ Misses         428      426       -2     
Files with missing lines Coverage Δ
hyperformula/src/i18n/languages/csCZ.ts 100.00% <ø> (ø)
hyperformula/src/i18n/languages/daDK.ts 100.00% <ø> (ø)
hyperformula/src/i18n/languages/deDE.ts 100.00% <ø> (ø)
hyperformula/src/i18n/languages/enGB.ts 100.00% <ø> (ø)
hyperformula/src/i18n/languages/esES.ts 100.00% <ø> (ø)
hyperformula/src/i18n/languages/fiFI.ts 100.00% <ø> (ø)
hyperformula/src/i18n/languages/frFR.ts 100.00% <ø> (ø)
hyperformula/src/i18n/languages/huHU.ts 100.00% <ø> (ø)
hyperformula/src/i18n/languages/idID.ts 100.00% <ø> (ø)
hyperformula/src/i18n/languages/itIT.ts 100.00% <ø> (ø)
... and 10 more

... and 1 file with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@Tobiadefami
Tobiadefami merged commit 2e7921c into develop Nov 16, 2026
35 checks passed
@Tobiadefami
Tobiadefami deleted the feature/HF-385 branch November 16, 2026 11:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants