Skip to content

Commit 9741ab9

Browse files
committed
Skip named ranges that correspond to nonexistent sheets
1 parent c70f288 commit 9741ab9

File tree

1 file changed

+16
-2
lines changed

1 file changed

+16
-2
lines changed

src/common/xlsx.util.ts

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import {
1010
utils,
1111
type WorkSheet,
1212
} from 'xlsx';
13+
import { NotFoundException } from './exceptions';
1314
import { CalendarDate } from './temporal';
1415

1516
export class WorkBook {
@@ -57,7 +58,20 @@ export class WorkBook {
5758
const rawList = this.book.Workbook?.Names ?? [];
5859
return mapEntries(rawList, ({ Ref: ref, Name: name }, { SKIP }) => {
5960
const matched = /^'?([^']+)'?!([$\dA-Z]+(?::[$\dA-Z]+)?)$/.exec(ref);
60-
return matched ? [name, this.sheet(matched[1]).range(matched[2])] : SKIP;
61+
if (!matched) {
62+
return SKIP;
63+
}
64+
const [_, sheetName, rangeStr] = matched;
65+
try {
66+
const range = this.sheet(sheetName).range(rangeStr);
67+
return [name, range];
68+
} catch (e) {
69+
// Skip ranges that correspond to nonexistent sheets
70+
if (e instanceof NotFoundException) {
71+
return SKIP;
72+
}
73+
throw e;
74+
}
6175
}).asRecord;
6276
}
6377
}
@@ -83,7 +97,7 @@ export class Sheet {
8397
this.workbook = this.book.book;
8498
this.sheet = this.workbook.Sheets[this.name];
8599
if (!this.sheet) {
86-
throw new Error(`Cannot find ${this.name} sheet`);
100+
throw new NotFoundException(`Cannot find ${this.name} sheet`);
87101
}
88102
}
89103
nonEnumerable(this, 'workbook' as any, 'sheet' as any);

0 commit comments

Comments
 (0)