diff --git a/src/plugins/core/range.ts b/src/plugins/core/range.ts index aa204ec6a9..0fa3be8894 100644 --- a/src/plugins/core/range.ts +++ b/src/plugins/core/range.ts @@ -325,7 +325,7 @@ export class RangeAdapter implements CommandHandler { * @param sheetXC the string description of a range, in the form SheetName!XC:XC */ getRangeFromSheetXC(defaultSheetId: UID, sheetXC: string): RangeImpl { - if (!rangeReference.test(sheetXC)) { + if (!rangeReference.test(sheetXC) || !this.getters.tryGetSheet(defaultSheetId)) { return new RangeImpl( { sheetId: "", diff --git a/tests/range_plugin.test.ts b/tests/range_plugin.test.ts index e2656872d2..5e06fec73a 100644 --- a/tests/range_plugin.test.ts +++ b/tests/range_plugin.test.ts @@ -574,6 +574,28 @@ describe("range plugin", () => { deleteSheet(m, "s1"); expect(m.getters.getRangeString(range)).toBe(CellErrorType.InvalidReference); }); + + test("getRangeFromSheetXC on invalid sheet", () => { + let getRange = () => m.getters.getRangeFromSheetXC("NotASheet", "A1"); + expect(getRange).not.toThrow(); + expect(getRange().invalidXc).toBe("A1"); + + getRange = () => m.getters.getRangeFromSheetXC("NotASheet", "A:A"); + expect(getRange).not.toThrow(); + expect(getRange().invalidXc).toBe("A:A"); + + getRange = () => m.getters.getRangeFromSheetXC("NotASheet", "Sheet1!A1"); + expect(getRange).not.toThrow(); + expect(getRange().invalidXc).toBe("Sheet1!A1"); + + getRange = () => m.getters.getRangeFromSheetXC("", "Sheet1!A:A"); + expect(getRange).not.toThrow(); + expect(getRange().invalidXc).toBe("Sheet1!A:A"); + + getRange = () => m.getters.getRangeFromSheetXC("", "A1:A2"); + expect(getRange).not.toThrow(); + expect(getRange().invalidXc).toBe("A1:A2"); + }); }); describe("Helpers", () => {