Repository navigation
JSE-62: bounded shared formulas (scan once, Excel's limit for masters, copies under maxCellTextLength), importDataset without shifting - #138
Merged
Conversation
- A shared formula's master is scanned once into the text between its references and the references; each cell that shares it costs its references plus its own length, built once as a flat string, and whether its references stay on the sheet is known in constant time. 2.3.0 rescanned the master per cell and appended character by character. - A master longer than 8,192 characters (Excel's formula limit) is not expanded: the cells that share it keep their cached values with SHARED_FORMULA_UNRESOLVED naming the master cell and its length. - Each shifted copy counts against maxCellTextLength and is refused before it is built (details.ref names the cell). - importDataset reads with an internal option that gives the cells sharing a formula the master's text unshifted: whether a cell has a formula, and whether it is SUBTOTAL(...), is the same, so results are unchanged. - Docs: 3.4.2, 5.2 limit and text tables, 5.6, warning table, 8 security table and server guidance, Q6. README limits line. Changeset: minor.
… limit, faster scan
- importDataset ("master" mode) skips the 8,192-character guard: children of
a long master keep the master's text as in 2.3.0 (totals rows and rows
without cached values are unchanged). Only the bounds of the master are
scanned there, once, to keep off-sheet children unresolved.
- read: a shifted copy longer than maxCellTextLength is no longer refused;
the cell keeps its cached value with SHARED_FORMULA_UNRESOLVED saying the
copy's length, and the read goes on. A master's own <f> text is still
refused by the limit.
- The scanner uses character codes and hand-written reference scanners
instead of a regex per character and per word; a test compares it with the
2.3.0 shifter on random formulas and on hand-picked edge tokens.
- Docs (3.4.2, 5.2, 5.6, 7.3, 8 with the measured worst case, Q6) and the
changeset say so.
This was referenced Dec 1, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Gap
Two security reviews (on 2.2.0 and 2.3.0) measured:
shiftFormula) and built its text without +=, a rope of millions of pieces. A master<f t="shared" si="0">of 1,048,576 characters shared by N cells: 10 cells 2.2 s / 314 MB, 100 cells 26.7 s / 2.9 GB, 1,000 cells out of memory at 4 GB, from a 3-4 KB file. Each copy is about 1.7 times the master, pastmaxCellTextLength, which did not count copies.maxCellTextLength: 1000. 1,000 rows × 100 cells sharing one 999-character formula (428 KB) took 48.6 s and 3.8 GB over five reads in the consumer's page; 10,000 × 90 gave no result in 10 minutes.importDatasetalways read withformulas: trueand shifted every shared formula, though the dataset only uses values; callers could not opt out.readwithformulas: falsereads the same file in about 0.1 s.Design
The smallest change that makes the measured files fast, in three parts:
Scan once, build once.
compileFormulascans a master once into the text between its references and the references (cell, column range, row range; anchors and numbers parsed). A reference with only$parts is the same in every cell and is folded into the text; equal references share one entry. A cell that shares the formula then costs its distinct references plus its own length:canShifttells in constant time (min/max of the relative rows and columns) whether every reference stays on the sheet, andshiftCompiledcomputes the copy's length first and builds it once as a flat string (join). The master is compiled when the first cell needs it and is replaced when a new master takes the samesi. The scanner is the 2.3.0 one, split into a parse step that does not depend on the offset and a format step, so results are the same (a seeded test compares it with the 2.3.0 shifter on 20,000 random formulas and offsets, and the fuzz below).Excel's formula limit for masters. A master longer than 8,192 characters (
MAX_SHARED_FORMULA_LENGTH, Excel's own limit) is not expanded: the cells that share it keep their cached values with the existing warningSHARED_FORMULA_UNRESOLVED(data loss), whose message names the master cell and its length:The shared formula of cell A2 could not be resolved: its master formula in cell A1 has 1048576 characters, more than Excel's limit of 8192; only its cached value was read.No new code, soExcelWarningCodedoes not widen; the master keeps its own text, as any formula withinmaxCellTextLength.Copies are cell text. A shifted copy's length is known before it is built; one longer than
maxCellTextLengthis not built, and the cell keeps its cached value withSHARED_FORMULA_UNRESOLVED(soft since the review round; see below).importDatasetdoes not shift. It reads through an internal read option (InternalReadOptions.sharedFormulaText: 'master', not public) that gives a cell sharing a formula the master's text unshifted. What the pipeline uses formulas for:toDataset's empty-row test,detectHeader's cell kind), and is kept as a cell rather than dropped;skipTotals: 'auto', whether every formula in the last data row isSUBTOTAL(…)(/^\s*=?\s*SUBTOTAL\s*\(/i).Both are the same for the master's text as for each shifted copy: a copy exists exactly when the shift succeeds (the reader still runs
canShift, constant time, so unresolvable cells keep warning and lose their formula as before), shifting never touches the leading=, spaces or the function name, and the master is never empty. Nothing else in the result carries formula text. The master is compiled once forcanShift, at most 8,192 characters.Lazy translation was considered and left out. Keeping
formulaas an accessor that shifts on access would keep the keys and values but not plain data: property descriptors change, libraries that copy descriptors or turn getters into computed values (immer in strict mode, MobX) behave differently, a frozen cell could not cache, and a worker transfer orJSON.stringifybuilds every copy anyway. Soreadwith formulas still returns one string per cell (see "What remains").Measurements
Node 26.13.1, Apple-silicon laptop, one process per row,
--max-old-space-size=4096, files built by a probe script (same shapes as the tests). RSS is the process peak. Two rounds were taken; the second ran while other jobs loaded the machine, so its times are higher, and the slower files were run only there.readreadreadimportDatasetimportDataset,maxCellTextLength: 1000read,maxCellTextLength: 1000read,formulas: falseimportDataset,maxCellTextLength: 1000read,formulas: falseread,maxCellTextLength: 1000The 999-character formula is
IF(AA10000>0,AB10000*2,$C$1)+…(70 relative references), whose copies keep their length. With a formula whose copies grow past the limit,readis refused at the first such cell.The 10,000 × 90
importDatasettime is the cost of 900,000 cells (theformulas: falseread alone is 1.4 s); no formula is shifted.What remains
readwith formulas returns one string per cell that shares a formula, so its output grows with the number of such cells times their length: 900 million characters for the 10,000 × 90 file, about 11 s and 1.2 GB. That is now proportional to what it returns, and bounded per cell by min(maxCellTextLength, about 5 × 8,192), but not in total except throughmaxCells. The docs (§8) say so, and the server guidance now recommendsformulas: falsewhen formula text is not needed. A total budget for built formula text would need a new limit; not part of this change.Tests
Written first. The red run on the unchanged source (with only the two new exports stubbed so the files load; the 10,000 × 90 import and the 1,000-child cases left out because 2.3.0 does not finish them): 6 failed (the 1,048,576-character masters with 10 and 100 children, the 8,192/8,193 boundary, the refusal of a copy over the limit, the 1,000 × 100 work counts in
readand inimportDataset, which took 35 s there), 13 passed (the regression guards: ordinary shifting, edges, totals rows,formulas: false). Then green.test/read/shared-formulas.test.ts: masters of 1,048,576 characters with 10, 100 and 1,000 children under default limits (files under 16 KB; children keep cached values; one warning each, message pinned; no compile and no shift:shiftWorkall zero; time bound 5 s); 8,192 expands, 8,193 does not;formulas: falsewarns nothing; a copy at the limit reads, one over is refused withdetails{ value: max + 1, max, ref }and nothing built; 1,000 × 100 withmaxCellTextLength: 1000: exact copies at A2, B3 and CV1001, 100,000 formulas, 1 compile of 999 characters, 99,999 shifts building exactly 99,999 × 999 characters (time bound 20 s, measured 1.1 to 1.7 s in vitest); 10,000 × 90 with formulas off (no work) and with a formula whose copies grow (refused at AA2 after 25 shifts); relative, absolute, mixed, ranges, other-sheet and quoted-sheet references, strings with reference-like text, names, structured references; the last row and column shift, one further is unresolved; a master redefined with the samesi.test/read/shift-formula.test.ts: the compiled shifter equals the 2.3.0 shifter (copied into the test as the reference) on 20,000 seeded random formulas and offsets, includingcanShiftand the length-before-building result; normalisation as before (a1+$a$01→B2+$A$1); equal references formatted once; length known before building.test/import/shared-formulas.test.ts: 1,000 × 100 and 10,000 × 90 withmaxCellTextLength: 1000throughimportDataset: values, no issues or warnings,shiftWork= 1 compile of 999 characters and 0 shifts (time bounds 10 s and 60 s; measured 0.4 s and 3.4 s in vitest); the 1,000-child master;skipTotals: 'auto'with plain SUBTOTAL formulas, a shared SUBTOTAL master and child, shared children of a master elsewhere, a shared non-SUBTOTAL formula, SUBTOTAL next to a shared child that is not, and next to a shared child whose references leave the sheet (no formula, warning), each compared withtoDataseton the full read and with the full read's warnings; formula cells without cached values still count.Local:
npm run typecheck;npm -w packages/javascript-excel run test: 1,312 passed, 51 skipped, 1 failed:test/format/libreoffice.test.ts(number-format probes against the local LibreOffice, unrelated to formulas; it is not in the required job and was reported failing the same way locally in #132);npm run build,npm run lint,npm run smoke, dependency guard,npm run docs:check.Differential fuzz against 2.3.0
A seeded generator of workbooks with one or two sheets, at the top-left and near the last row and column: shared masters (with
ref,si0 to 3, redefined), shared children (includingsiwithout a master and children of masters over 8,192 characters, about 3% of masters), plain, array and empty<f>, with and without cached values (n,str,e,b,s, inline), styles; formula text from every reference kind (A1,$A1,A$1,$A$1, lowercase, leading zeros, rows 0 and 9,999,999, columns past XFD, ranges,A:C,$1:2,Sheet2!,'My Sheet'!,'It''s'!,[1]Sheet1!), strings with reference-like text and"", names (TaxRate,ABCD1,LOG10(,_xlfn.XLOOKUP(,A1B2,R1C1), structured references,SUBTOTAL(prefixes with spaces,=and lowercase, and references at 9/99/999 and Z/ZZ so copies grow. Each workbook runsreadtwice (randomformulas,richText,rawText,styles,maxRows,dates, smallmaxCellTextLengthandmaxWarnings; in 30% of the second runsmaxCellTextLengthis exactly the longest formula in the file) andimportDatasetonce (randomheader,skipTotals,range,types,preview,errorCells,maxCellTextLength,sheet,maxRows), against the published 2.3.0 tarball. Results and errors must be deep-equal, except:<f>removed;maxCellTextLengthnaming a cell, and 2.3.0 with the limit lifted holds a formula longer than the limit in that cell (2.3.0 either read the file or refused a later, longer plain text).The first full run reported 75 divergences, all of one kind: both versions refused with
maxCellTextLength, this branch at an earlier cell, a shifted copy over the limit, before 2.3.0 reached the longer plain text it refused. The classifier was widened to check that case against 2.3.0 with the limit lifted (above); the rerun has none.Compatibility
No public API change: no new option, export or warning code.
importDatasetresults are identical to 2.3.0 for every input (fuzz in the review section).readdiffers only for those masters and for cells whose shifted copy would be longer thanmaxCellTextLength(default 1,048,576, which no copy of a master within 8,192 characters can reach): those keep their cached values with a warning. The change stays informula.ts, the shared-formula branch ofsheet.ts, one internal read option inreader.tsand the read call in the import pipeline.Changeset: minor. No new API, but a new refusal (copies count against
maxCellTextLength) and a new case of a data-loss warning, which the repo has released as minor (maxXmlTokenLengthin 2.1,maxCellTextLengthin 2.3). Docs: §3.4.2, themaxCellTextLengthrow and the text table in §5.2, the values bullet in §5.2, the xlsx step in §5.6, the warning table in §7.3, the "Huge text" row of the §8 security table (shared formulas move out of the not-bounded list, with what remains) and the server guidance (formulas: false), §12 Q6 (the design, and why not lazy). README: the untrusted-files line.Review fixes (second round)
importDatasetequal to 2.3.0 for long masters. The 8,192-character guard ran before theimportDataset("master") branch, so the children of a long master lost their formula there too. A SUBTOTAL totals row whose only formula was such a child was no longer skipped, and a child without a cached value no longer counted its row. In "master" mode the guard is now skipped.canShiftstill runs, so off-sheet children stay unresolved as in 2.3.0. Only the master's bounds are scanned, once (compileFormula(f, true): no segments, no copies). Tests use both reproducers, with 2.3.0's results as the expected values: (a)totalsRowSkipped: true, 2 rows, rangeA1:B4, no warning; (b) rangeA1:B3, 2 rows, no warning. The 1,048,576-character master with 1,000 children now imports with no warnings, as 2.3.0 would.maxCellTextLengthis no longer refused. The cell keeps its cached value withSHARED_FORMULA_UNRESOLVED:…could not be resolved: shifted, it would have 13 characters, more than maxCellTextLength (12); only its cached value was read.The read goes on and no text longer than the limit is built. A master's own<f>text is still refused, as any cell text (test). The 10,000 × 90 file whose copies grow past 1,000 now reads: 233 copies are built, and the other 899,766 cells keep their cached values with the warning (kept plus dropped counted).importDataset(machine shared with other jobs, two runs each):importDatasetread,formulas: false(floor)What is left is about 0.35 µs per reference (3.2 million references), mostly the reference scanner.
ExcelWarningcarriescode,message,part,sheet,ref, and only code-specificpartKind/dropped; there is no details field. A cause field would widen the public type, so the cause stays in the message (three wordings: off-sheet or missing master, master too long, copy over the limit).maxCellTextLengthrow and values bullet, §5.6, §7.3, §8 (soft copy limit; the reviewer's worst case: 273 KB, 100,000 children of an 8,192-character master from the bottom-right corner give 3.0 billion characters, 22 s and 3.4 GB;formulas: falseavoids it; a total budget is a separate ticket), and Q6. The changeset is reworded to match.readand twoimportDatasetcases per workbook.importDatasetmust now be deep-equal with no exception, long masters included.readmay differ only by long-master and over-limit cells: each over-limit cell must hold a longer formula in 2.3.0 with the limit lifted, and the result without those warnings must equal 2.3.0 on the file without those cells'<f>.origin/main(JSE-69: 'day' takes the day as written for ISO text with an offset #137, JSE-69). No conflicts. Local after the merge: typecheck and build pass. The suite has 1,338 passed, 51 skipped, and 1 failed:test/format/libreoffice.test.ts, the local LibreOffice probe, as before. Lint, docs check and smoke pass.