Skip to content

Commit

Permalink
[FIX] ranges: getRangeFromSheetXC with invalid sheetId
Browse files Browse the repository at this point in the history
When `getRangeFromSheetXC` is called, it either crashes or returns an
corrupted range. The getter should handle this case, and return an
invalid range.

closes #4779

Task: 3578461
X-original-commit: d6bc2b7
Signed-off-by: Adrien Minne (adrm) <[email protected]>
Signed-off-by: Rémi Rahir (rar) <[email protected]>
  • Loading branch information
hokolomopo committed Aug 6, 2024
1 parent 5bac40b commit 974bfd7
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/plugins/core/range.ts
Original file line number Diff line number Diff line change
Expand Up @@ -325,7 +325,7 @@ export class RangeAdapter implements CommandHandler<CoreCommand> {
* @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: "",
Expand Down
22 changes: 22 additions & 0 deletions tests/range_plugin.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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", () => {
Expand Down

0 comments on commit 974bfd7

Please sign in to comment.