From 982a7391adfc3ddb1391d4fdd301e338c28df790 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/range_plugin.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 9617a676a3..1872eaf82a 100644 --- a/src/plugins/core/range.ts +++ b/src/plugins/core/range.ts @@ -291,7 +291,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 4e6d519c5a..e8ebe382b6 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(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", () => {