Skip to content

Fix PnP named range crashes #3430

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
May 14, 2025
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 18 additions & 3 deletions src/common/xlsx.util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
utils,
type WorkSheet,
} from 'xlsx';
import { NotFoundException } from './exceptions';
import { CalendarDate } from './temporal';

export class WorkBook {
Expand Down Expand Up @@ -56,8 +57,22 @@ export class WorkBook {
@Once() private get namedRanges(): Record<string, Range> {
const rawList = this.book.Workbook?.Names ?? [];
return mapEntries(rawList, ({ Ref: ref, Name: name }, { SKIP }) => {
const matched = /^'?([^']+)'?!([$\dA-Z]+(?::[$\dA-Z]+)?)$/.exec(ref);
return matched ? [name, this.sheet(matched[1]).range(matched[2])] : SKIP;
const matched =
/^(?:\[\d+])?'?([^']+)'?!([$\dA-Z]+(?::[$\dA-Z]+)?)$/.exec(ref);
if (!matched) {
return SKIP;
}
const [_, sheetName, rangeStr] = matched;
try {
const range = this.sheet(sheetName).range(rangeStr);
return [name, range];
} catch (e) {
// Skip ranges that correspond to nonexistent sheets
if (e instanceof NotFoundException) {
return SKIP;
}
throw e;
}
}).asRecord;
}
}
Expand All @@ -83,7 +98,7 @@ export class Sheet {
this.workbook = this.book.book;
this.sheet = this.workbook.Sheets[this.name];
if (!this.sheet) {
throw new Error(`Cannot find ${this.name} sheet`);
throw new NotFoundException(`Cannot find ${this.name} sheet`);
}
}
nonEnumerable(this, 'workbook' as any, 'sheet' as any);
Expand Down