Skip to content

JSE-F2: detect two-row headers without merges (CSV) - #117

Merged
securedeveloper merged 1 commit into
mainfrom
jse-f2-two-row-headers
Nov 17, 2026
Merged

securedeveloper merged 1 commit into
mainfrom
jse-f2-two-row-headers

Conversation

@securedeveloper

Copy link
Copy Markdown
Owner

header: 'auto' read a two-row header without merges as a one-row header, with confidence: 'high', and turned the second header row into data. CSV never has merges, so this hit every CSV with a two-row header. header: { rows: 2 } already worked.

The rule (src/internal/import/header.ts)

  1. A stacked second row. After a single high-confidence header row, the row directly below joins the header when it is all text and every text cell sits on a column whose next five non-empty values are all typed (number, boolean, date). A data row can't look like that: it would make each of those columns mixed.
  2. Two rows that only fill the table together. When no single row fills the table width, two adjacent text rows that cover it together are a two-row header, provided the lower one passes rule 1. That is what toCsv writes for header groups: Name,Q1, over ,Jan,Feb.
  3. Uncertain. If the row under the header is all text but only some of its text cells sit on typed columns, it stays data and the confidence drops to 'low', so a UI asks the user.

It is still deterministic, with no scores or weights.

input (CSV) before after
Region,Sales,Sales / ,Q1,Q2 / numbers [0] high; Sales: 'Q1' as the first data row [0, 1] high; Region, Sales / Q1, Sales / Q2
Name,Q1, / ,Jan,Feb / numbers [0] low; keys Name, Q1, C [0, 1] high; keys Name, Q1 / Jan, Feb*
Temp,Pressure / °C,hPa / numbers [0] high; units as data [0, 1] high; Temp / °C, Pressure / hPa
Name,Amount / Ada,n/a / Bob,10 [0] high [0] low
Name,City,Amount / Bob,Paris, / Ada,Oslo,10 [0] high [0] high (unchanged)

* Without merges, the flattening can't know that Q1 also covers Feb. Filling a group title to the right over empty cells is a separate decision for the flattening step and is not part of this PR.

Fixtures

react-data-import's header fixtures are copied unchanged into test/fixtures/import/ (4 CSV, 6 xlsx; README there) and checked in test/import/fixtures.test.ts. Only two-header-rows.csv changes, from [0] to [0, 1]. Every other fixture keeps its rows and 'high' confidence. Separately, react-data-import's own suite was run against this build: 94 tests pass, and 1 fails, the test that pins today's behaviour for two-header-rows.csv. That test (and its docs line) needs updating in react-data-import once this ships.

Checks

  • New unit tests in test/import/header.test.ts (stacked rows, units row, group row, a text data row kept, the low-confidence cases)
  • Full suite: 940 pass. Typecheck, lint, docs:check clean.
  • docs/api.md (Header detection) describes the new rules; patch changeset.

detectHeader took the first of two stacked header rows as the header
and the second as data, with high confidence, whenever there were no
merges to tie them together (always the case in CSV).

- The row directly below a single high-confidence header joins it when
  it is all text and every text cell sits on a column whose next five
  non-empty values are all typed. A data row can't look like that: it
  would make each of those columns mixed.
- When no single row fills the table, two adjacent text rows that fill
  it together, with the lower one passing the same test, are a two-row
  header ("Name,Q1," over ",Jan,Feb", toCsv of header groups).
- When the row under the header is all text but only partly fits, it
  stays data and the confidence drops to 'low'.

react-data-import's header fixtures are copied into
test/fixtures/import and checked here; only two-header-rows.csv
changes (rows [0] -> [0, 1]). docs/api.md describes the new rules.
@securedeveloper
securedeveloper merged commit 1664819 into main Nov 17, 2026
9 checks passed
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.

1 participant