Fix #3028: make getWorksheet case-insensitive to match addWorksheet#51
Fix #3028: make getWorksheet case-insensitive to match addWorksheet#51senoff wants to merge 1 commit into
Conversation
…sheet
Workbook.addWorksheet enforces case-insensitive name uniqueness (and
Excel itself treats sheet names case-insensitively), but
Workbook.getWorksheet was matching with a case-sensitive ===. As a
result, after addWorksheet("Sheet1"), getWorksheet("sheet1") returned
undefined even though addWorksheet("sheet1") would throw "Worksheet
name already exists".
Lowercase both sides of the string comparison in getWorksheet so any
casing of an existing sheet name resolves to that sheet. Numeric-id
and no-arg branches are unchanged.
Adds spec/integration/issues/issue-3028-getworksheet-case-insensitive.spec.js
covering: mixed casings resolve to the same sheet, unknown names still
return undefined, numeric id and no-arg shortcuts preserved, and the
addWorksheet/getWorksheet contracts now agree.
Drive-by: collapse pre-existing multi-line constructs in the same
function (two console.trace deprecation warnings and an Object.assign
+ a reduce->for loop) so prettier no longer wraps them with a trailing
comma that eslint then rejects. The repo's .prettierrc
(trailingComma: "all") conflicts with .eslintrc (comma-dangle
functions: "never"), and these were the spots prettier wanted to
rewrite around the edited region. Behavior is unchanged.
2960dc7 to
3a3af7c
Compare
|
Local test run on this branch —
No regressions vs baseline. Skipped Posted because the |
|
Ran exceljs's headless test suite against this PR: (Skipped All green. Hopefully useful evidence for review. |
|
Regenerated per AGENTS.md guidance — see new PR #67. Closing this one in favor of the regenerated version. |
Summary
Closes the API contradiction reported in exceljs/exceljs#3028:
Workbook.addWorksheet(name)enforces case-insensitive uniqueness (and Excel itself treats sheet names case-insensitively), butWorkbook.getWorksheet(name)was matching with a case-sensitive===. The two APIs disagreed:After this PR,
getWorksheet('sheet1')returns the existing sheet, matching whataddWorksheet's constraint already implies.Change
lib/doc/workbook.js— in the string branch ofgetWorksheet, lowercase both the lookup name and each candidate sheet name before comparing. The numeric-id branch and the no-arg first-sheet shortcut are unchanged.if (typeof id === 'string') { - return this._worksheets.find(worksheet => worksheet && worksheet.name === id); + // Case-insensitive to match addWorksheet's uniqueness rule (and Excel itself). + const target = id.toLowerCase(); + return this._worksheets.find(ws => ws && ws.name.toLowerCase() === target); }Behavior change
Minor and intentional: code that previously relied on a casing mismatch returning
undefinedwill now find the sheet. We consider this a bug fix because the prior behavior contradictedaddWorksheet's own case-insensitive constraint; there is no way for two sheets with names that differ only in case to coexist, so the case-sensitive lookup could never resolve a real ambiguity.Documented under "Unreleased" in
FORK.md.Test plan
spec/integration/issues/issue-3028-getworksheet-case-insensitive.spec.js(5 cases):TestSheet,testsheet,TESTSHEET,tEsTsHeEt) all resolve to the same sheetundefinedaddWorksheetandgetWorksheetcontracts now agreenpm run test:unit— 884 passingnpm run test:integration— 205 passing (including the new 5)npm run test:end-to-end— 1 passing