From 566b5f5ae84653801146036b60ee02a82c6c9ac6 Mon Sep 17 00:00:00 2001 From: "Adrien Minne (adrm)" Date: Mon, 27 May 2024 10:40:33 +0200 Subject: [PATCH] [FIX] ranges: getRangeFromSheetXC with invalid sheetId When `getRangeFromSheetXC` is called, it either crashes or returns an corrupted range. The getter should handle this case, and return an invalid range. Task: 3578461 --- src/plugins/core/range.ts | 2 +- tests/plugins/range.test.ts | 22 ++++++++++++++++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/src/plugins/core/range.ts b/src/plugins/core/range.ts index 280d980e6e..c1b16c55ef 100644 --- a/src/plugins/core/range.ts +++ b/src/plugins/core/range.ts @@ -292,7 +292,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/plugins/range.test.ts b/tests/plugins/range.test.ts index 64bf6d1c8a..49613a7ede 100644 --- a/tests/plugins/range.test.ts +++ b/tests/plugins/range.test.ts @@ -571,6 +571,28 @@ describe("range plugin", () => { deleteSheet(m, "s1"); expect(m.getters.getRangeString(range)).toBe(INCORRECT_RANGE_STRING); }); + + 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", () => {