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.

Task: 3578461
  • Loading branch information
hokolomopo committed Aug 6, 2024
1 parent f9611f7 commit 982a739
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 @@ -291,7 +291,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(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", () => {
Expand Down

0 comments on commit 982a739

Please sign in to comment.