From e1f95c61c6eb1407f40ff3a3c554577653ab8d92 Mon Sep 17 00:00:00 2001 From: Jacob Strieb Date: Fri, 17 Jan 2025 03:01:15 -0500 Subject: [PATCH] Fix range in new cell bug (and test that caught it) --- src/classes.svelte.js | 6 ++++-- test/classes.svelte.test.js | 19 ++++++++++++++++--- 2 files changed, 20 insertions(+), 5 deletions(-) diff --git a/src/classes.svelte.js b/src/classes.svelte.js index e60c8dd..ae3e193 100644 --- a/src/classes.svelte.js +++ b/src/classes.svelte.js @@ -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)), ), ); } @@ -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)), ), ); } diff --git a/test/classes.svelte.test.js b/test/classes.svelte.test.js index b910d53..76c3fca 100644 --- a/test/classes.svelte.test.js +++ b/test/classes.svelte.test.js @@ -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: [ { @@ -11,7 +11,7 @@ function createSheet(cells) { cells: cells.map((row) => row.map((s) => ({ formula: s }))), }, ], - formulaCode: "", + formulaCode: formulaCode, }); } @@ -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]]); +});