Skip to content

Commit

Permalink
Fix range in new cell bug (and test that caught it)
Browse files Browse the repository at this point in the history
  • Loading branch information
jstrieb committed Jan 17, 2025
1 parent 36a60de commit e1f95c6
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 5 deletions.
6 changes: 4 additions & 2 deletions src/classes.svelte.js
Original file line number Diff line number Diff line change
Expand Up @@ -395,7 +395,7 @@ export class Sheet {
.map((_, i) =>
new Array(this.widths.length)
.fill()
.map((_, j) => this.newCell(undefined, i, j)),
.map((_, j) => this.newCell(undefined, start + i, j)),
),
);
}
Expand All @@ -416,7 +416,9 @@ export class Sheet {
row.splice(
start,
0,
...new Array(n).fill().map((_, j) => this.newCell(undefined, i, j)),
...new Array(n)
.fill()
.map((_, j) => this.newCell(undefined, i, start + j)),
),
);
}
Expand Down
19 changes: 16 additions & 3 deletions test/classes.svelte.test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { State } from "../src/classes.svelte.js";
import { test, expect } from "vitest";

function createSheet(cells) {
function createSheet(cells, formulaCode = "") {
return State.load({
sheets: [
{
Expand All @@ -11,7 +11,7 @@ function createSheet(cells) {
cells: cells.map((row) => row.map((s) => ({ formula: s }))),
},
],
formulaCode: "",
formulaCode: formulaCode,
});
}

Expand All @@ -30,11 +30,24 @@ function expectSheet(sheet, cells) {
);
}

test("Simple Sheet", async () => {
test("Simple sheet with changes", async () => {
const state = createSheet([["1", "2", "=RC0 + RC1"]]);
await expectSheet(state.currentSheet, [[1, 2, 3]]);
state.currentSheet.cells[0][2].formula = "=SUM(R[0]C0:RC[-1])";
await expectSheet(state.currentSheet, [[1, 2, 3]]);
state.currentSheet.cells[0][0].formula = "3";
await expectSheet(state.currentSheet, [[3, 2, 5]]);
state.currentSheet.cells[0][2].formula = "=RC[-2] * RC[-1] + 5";
await expectSheet(state.currentSheet, [[3, 2, 11]]);
});

test("Add and remove cells", async () => {
const state = createSheet([["1", "2", "3"]]);
await expectSheet(state.currentSheet, [[1, 2, 3]]);
state.currentSheet.addCols(1);
await expectSheet(state.currentSheet, [[1, 2, 3, undefined]]);
state.currentSheet.cells[0][3].formula = "=prod(RC0:RC[-1])";
await expectSheet(state.currentSheet, [[1, 2, 3, 6]]);
state.currentSheet.cells[0][3].formula = "=RC0 * RC1 * RC2";
await expectSheet(state.currentSheet, [[1, 2, 3, 6]]);
});

0 comments on commit e1f95c6

Please sign in to comment.