Fix silent drop of named LAMBDA / non-range defined names on round-trip#59
Closed
senoff wants to merge 1 commit into
Closed
Fix silent drop of named LAMBDA / non-range defined names on round-trip#59senoff wants to merge 1 commit into
senoff wants to merge 1 commit into
Conversation
DefinedNamesXform.extractRanges() split definedName values on commas and
passed each token through colCache.decodeEx(). Tokens that looked like valid
addresses (e.g. fragments of LAMBDA(x,x*2)) were silently kept; the rest
were silently dropped. Any definedName whose value is not a pure range list
(LAMBDA, LET, OFFSET wrappers, etc.) was therefore irreversibly lost on the
first ExcelJS write.
Fix: add a fast-path in extractRanges() — if the text contains '(' it cannot
be a range address list and is returned as empty immediately. parseClose()
then stores the raw text in a `formula` field instead. render() writes that
field verbatim. workbook.js keeps formula-type defined names in a separate
_formulaDefinedNames array (not in the CellMatrix-backed DefinedNames class)
and round-trips them through model get/set.
Also carry the .prettierrc trailingComma "all" → "es5" fix (same as PR protobi#57).
Tests: add unit cases for Named LAMBDA expression and Named LAMBDA with
multiple parameters (render + renderIn + parse), and an integration test
that builds a synthetic workbook via JSZip, injects two LAMBDA definedNames
plus one normal range name, writes/re-reads through ExcelJS, and asserts
all three survive verbatim.
Author
|
Local test run on this branch —
No regressions vs baseline. Skipped Posted because the |
This was referenced May 7, 2026
Author
|
Regenerated per AGENTS.md guidance — see new PR #71. Closing this one in favor of the regenerated version. |
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.
Problem
Modern Excel (2024+) lets users define reusable workbook-level formulas using LAMBDA:
Cells can then call
=MyDouble(5)directly. These named LAMBDAs live as<definedName>entries inxl/workbook.xml:ExcelJS currently silently drops every
<definedName>whose value is not a valid range address. After one read/write cycle, all named LAMBDAs — and any other formula-type defined names — are gone with no error or warning. The workbook is quietly broken.Root cause
DefinedNamesXform.extractRanges()splits the raw text on commas and passes each token throughcolCache.decodeEx(). ForLAMBDA(x,x*2)the split produces['LAMBDA(x', 'x*2)'].LAMBDA(xthrows (correctly rejected), butx*2)does not throw —decodeExis permissive enough to return a row-only object for it. The result is arangesarray with garbage, non-empty, so the formula text is never preserved and is lost on write.Fix
Two targeted changes:
lib/xlsx/xform/book/defined-name-xform.jsextractRanges(): add a fast-path at the top — if the text contains(, it cannot be a range address list (parentheses are never valid in range addresses), so return[]immediately without attempting to split.parseClose(): whenextractRangesreturns empty and the trimmed text is non-empty, store the raw text in aformulafield on the model object.render(): if the model has aformulafield, write it verbatim; otherwise join ranges as before.lib/doc/workbook.js_formulaDefinedNames = []alongside the existing_definedNames(CellMatrix-backed).modelgetter: concatenate both arrays so formula-type names are included in the output fed toWorkbookXform.render().modelsetter: split incomingdefinedNames— entries with aformulafield go to_formulaDefinedNames, entries without go to the existing_definedNamespath unchanged.Normal range-based defined names, print areas, and print titles are completely unaffected.
Also includes the
.prettierrctrailingComma: "all"→"es5"config correction (same fix as pending PR #57) to keep lint-staged hooks passing.Tests
Unit (
spec/unit/xlsx/xform/book/defined-name-xform.spec.js): two new cases —Named LAMBDA expressionandNamed LAMBDA with multiple parameters— covering render, renderIn, and parse. The existingString with something that looks like a rangeparse expectation is updated to include the now-correctly-preservedformulafield.Integration (
spec/integration/pr/lambda-defined-name.spec.js): builds a synthetic workbook via JSZip with two LAMBDA defined names (MyDouble = LAMBDA(x,x*2),MyAdd = LAMBDA(x,y,x+y)) and one normal range name (NormalRange = Sheet1!$A$1:$B$2), writes through ExcelJS, reads back, and asserts all three survive verbatim. Three assertions: LAMBDA names present, range name present, exact LAMBDA body text unchanged.Test counts after patch: 890 unit (was 884, +6), 203 integration (was 200, +3), 1 e2e — all passing.