Skip to content

feat(xlsx): a filter by values, from a list under each header cell - #66

Open
Alex-Citeroni wants to merge 2 commits into
p10node:mainfrom
Alex-Citeroni:feat/xlsx-filter
Open

Alex-Citeroni wants to merge 2 commits into
p10node:mainfrom
Alex-Citeroni:feat/xlsx-filter

Conversation

@Alex-Citeroni

@Alex-Citeroni Alex-Citeroni commented Dec 4, 2026 •

Copy link
Copy Markdown

A workbook's <autoFilter> was kept in the file but did nothing in the editor. A sheet can now be filtered by values: turned on from the toolbar, set from a list under each header cell, and saved as Excel saves it.

What it does

  • Reading: the file's filter is read with the values chosen for its columns. A criterion of another kind (custom, top 10, dynamic, color, icon, dates by group) is kept as it is and written back until values are chosen for its column.
  • In the view: a Filter button in the toolbar (Ctrl/⌘+Shift+L, also in the context menu) and a button in each header cell, marked where the column is filtered. It opens a card with a search, (Select all), (Blanks), OK and Cancel. A read-only view shows the buttons and the marks.
  • Operations: filter (on over a range, or off) and filterBy (the values shown for a column). Each is one step of Undo, the criteria and the rows together. A protected sheet refuses them unless it allows its filter.
  • Rows it hides: a sort, a clear and a deletion of rows leave them as they are. A cut takes the whole range.
  • In a room: an operation carries the rows it hid, as a sort carries its order, so the same rows hide for everyone even where someone typed in one of them at the same moment. After another filter or a sort made at the same moment, they are worked out again from the criteria.
  • Rows and columns: the filter follows those inserted and deleted. SUBTOTAL and AGGREGATE are computed again when it hides or shows rows.
  • Saving: <autoFilter> in the order of the schema, the hidden rows, and the sheet's _xlnm._FilterDatabase name.

XlsxToolbar takes filtered and onFilter, both optional: without onFilter it has no Filter button.

Not in this PR

  • Custom, number, date and color filters set from the list, and sorting from it.
  • Filtering again when a cell changes.
  • The filters of tables. A sheet's filter is refused over the cells of a table.

Tests

Unit tests for reading, operations, undo, collaboration and the saved XML, and an e2e spec for the card. Typecheck and the unit suite (1779) pass. The xlsx e2e suite (65, run with both PRs merged) passes. I have not opened a saved file in desktop Excel.

The strings are in English and German. A second PR adds data validation lists. The two are independent, but they touch some of the same files: I will rebase whichever lands second.

🤖 Generated with Claude Code

… a list under each header cell, saved

- the file's <autoFilter> is read with the values of its columns (<filters>, blank cells included); a criterion of
  another kind (custom, top 10, dynamic, color, icon, dates by group) is kept as it is and written back, at the
  column it is at now, until values are chosen for its column
- two sheet operations: `filter` (on over a range, its first row the header; off: every row it hid shows) and
  `filterBy` (the values of a column whose rows are shown, '' for blank cells; null clears). One step of Undo each,
  the criteria and the rows together; a protected sheet refuses them unless it allows its filter (`autoFilter`)
- in a room an operation carries the rows it hid, as a sort carries its order: the same rows hide for everyone,
  also where someone typed in one of them at the same moment; it follows the rows and columns inserted meanwhile
- `xlsx.filterValues`: the distinct values a column shows in the rows the other columns let through (10,000 at
  most, saying so), and those whose rows are shown
- the filter follows the rows and columns inserted and deleted (a deleted column takes its criterion, the header
  row deleted takes the filter); SUBTOTAL and AGGREGATE are computed again when it hides or shows rows
- saved as Excel saves it: <autoFilter> from the model in the order of the schema (what else it held, its sort
  state, stays while it is over the same cells), the rows hidden, the sheet's `_xlnm._FilterDatabase` name
- the view: a Filter button in the toolbar (Ctrl/⌘+Shift+L, also in the context menu), a button in each header
  cell of the filter, marked where the column is filtered (Alt+Down on the cell), a card with a search, (Select
  all), (Blanks), OK / Cancel; a view that cannot edit shows the buttons and the marks
- `XlsxToolbar` takes `filtered` and `onFilter`, both optional: without `onFilter` it has no Filter button

Not here: custom, number, date and color filters set from the list, sorting from the list, filtering again when
a cell changes, the filters of tables.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@pierreneter

Copy link
Copy Markdown
Member

Thank you for this one too, @Alex-Citeroni. I traced these paths and they hold:

  • undo and redo restore the criteria and the rows together;
  • the range and the column ids follow rows and columns inserted and deleted;
  • <autoFilter> is written in the order of the schema, with the file's own attributes and its other criteria kept;
  • a protected sheet refuses what it should.

Three things need to change before this goes in, and two smaller ones are worth a look. I read the code. I have not run it, and I have not opened a saved file in Excel.

To fix before merge

1. A sheet filter can be put over a table.

filter() (src/core/xlsx/editor.ts:1311) never looks at wb.tables, and toggleFilter takes the region around the selection as it is. With the selection inside a table, Ctrl/⌘+Shift+L saves a sheet's <autoFilter> over cells the table's own filter already covers. I expect Excel to repair such a file. Refusing a range that meets a table of the sheet would do: the PR already leaves the filters of tables out.

2. Sort and Delete act on the rows the filter hid.

sort, moveRows and clear in src/core/xlsx/editor.ts read neither rowAttrs.hidden nor d.filter.

  • Filter by one column, then sort the range by another. The cells move and the hidden flags stay on their rows, so the rows shown no longer match the criterion.
  • Select the filtered range and press Delete. The cells of the hidden rows are cleared too. Undo brings them back, but the user never saw them go.

Excel sorts and clears only what the filter shows. At the least, a sort over the filter's range should apply the criteria again in the same step of Undo, and a clear should leave the hidden rows as they are.

3. In a room, a filterBy keeps its hidden rows after another filter operation.

  • transformX returns the operation as it is when the one before it is a filter or a filterBy (src/collab/xlsx-ops.ts:213).
  • After a sort, overSort moves only set, fmt and replace.
  • hidden is dropped only for rows inserted or deleted. filter() then takes the rows it was given over the criteria.

Two people who filter two columns at the same moment end with both criteria stored and only the second one's rows hidden: the same on every replica, but not what the criteria say. Dropping hidden when the operation before is a filter, a filterBy or a sort of the same sheet lets every replica work the rows out from the cells.

Smaller, not blocking

  • _xlnm._FilterDatabase in a workbook with a chart sheet. filterNames (src/core/xlsx/editor.ts:2033 and :2051) uses the worksheet's index as localSheetId, but that attribute counts every entry of <sheets>, chart sheets too, as the comment at src/core/xlsx/workbook.ts:736 says. With a chart sheet before the filtered sheet, the name is written for the wrong sheet and an existing one is not found.
  • Line breaks in the values. escAttr leaves a tab, LF and CR as they are, and a reader turns them into spaces in an attribute. The val of a cell with several lines then no longer matches when the file is opened again. Excel writes &#10; there.

#67 changes eight of the same files, so whichever of the two lands second needs the rebase you mentioned.

… on the rows it shows; two filters at once in a room

- a sheet's filter is refused over the cells of a table
- a sort, a clear and a deletion of rows leave the rows the filter hides as they are
- in a room, a filter set at the same moment as another one, or as a sort, hides the rows its criteria say
- the filter's hidden name counts chart sheets; line breaks in a value are kept

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@Alex-Citeroni

Copy link
Copy Markdown
Author

Thank you for the careful read. All five points are fixed in a3e8d8a.

To fix before merge

  1. Filter over a table: refused when its range meets a table of the sheet, with a message.
  2. Sort and Delete: I reproduced the sort problem. A sort and a clear now leave the rows the filter hides as they are. A cut still takes the whole range.
  3. Two filters in a room: a filterBy drops its hidden rows after a filter, a filterBy or a sort of the same sheet, as you suggested. A sort order made elsewhere also takes the hidden flags along with the cells, when it moves a column the filter reads.

Smaller

  • _xlnm._FilterDatabase counts every entry of <sheets>, chart sheets too.
  • Tabs and line breaks in a value are written as character references (&#10;).

I found one more of the same kind: deleting rows selected in a filtered sheet also deleted the hidden ones between them. Now only the rows shown go. Each block of rows is its own step of Undo, so past 100 separate blocks it is refused with a message.

There are new tests for these, and the unit suite passes. I still have not opened a saved file in desktop Excel.

I will rebase whichever of the two lands second.

This branch has not been deployed

No deployments
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.

2 participants