Skip to content

Commit 285e036

Browse files
committed
Fix handling ranges in updateCellValue
1 parent a3d6ffe commit 285e036

2 files changed

Lines changed: 44 additions & 6 deletions

File tree

src/engine.spec.ts

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import * as Formula from "./formula";
2-
import { getFormulaComputedValue } from "./engine";
2+
import { getFormulaComputedValue, updateCellValue, Model } from "./engine";
3+
import { CellBase } from "./types";
34
import FormulaParser from "fast-formula-parser";
4-
import { ORIGIN } from "./point";
5+
import { ORIGIN, Point } from "./point";
56

67
const MOCK_PARSE = jest.fn();
78
const MOCK_FORMULA_PARSER = {
@@ -41,3 +42,36 @@ describe("getFormulaComputedValue()", () => {
4142
);
4243
});
4344
});
45+
46+
describe("updateCellValue", () => {
47+
test("update simple cell", () => {
48+
const model = new Model([]);
49+
const cell: CellBase = { value: "1" };
50+
const point: Point = { row: 0, column: 0 };
51+
const nextModel = updateCellValue(model, point, cell);
52+
expect(nextModel.data).toEqual([[cell]]);
53+
expect(nextModel.evaluatedData).toEqual([[cell]]);
54+
});
55+
test("update simple formula cell", () => {
56+
const model = new Model([[{ value: 1 }], [{ value: 2 }]]);
57+
const cell: CellBase = { value: "=A1" };
58+
const point: Point = { row: 0, column: 1 };
59+
const nextModel = updateCellValue(model, point, cell);
60+
expect(nextModel.data).toEqual([[{ value: 1 }, cell], [{ value: 2 }]]);
61+
expect(nextModel.evaluatedData).toEqual([
62+
[{ value: 1 }, { value: 1 }],
63+
[{ value: 2 }],
64+
]);
65+
});
66+
test("update range formula cell", () => {
67+
const model = new Model([[{ value: 1 }], [{ value: 2 }]]);
68+
const cell: CellBase = { value: "=SUM(A:A)" };
69+
const point: Point = { row: 0, column: 1 };
70+
const nextModel = updateCellValue(model, point, cell);
71+
expect(nextModel.data).toEqual([[{ value: 1 }, cell], [{ value: 2 }]]);
72+
expect(nextModel.evaluatedData).toEqual([
73+
[{ value: 1 }, { value: 3 }],
74+
[{ value: 2 }],
75+
]);
76+
});
77+
});

src/formula.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,15 +40,18 @@ export function createBoundFormulaParser(
4040
return cell?.value;
4141
},
4242
onRange: (ref) => {
43+
const data = getData();
44+
const size = matrix.getSize(data);
4345
const start: Point = {
4446
row: ref.from.row - 1,
4547
column: ref.from.col - 1,
4648
};
4749
const end: Point = {
48-
row: ref.to.row - 1,
49-
column: ref.to.col - 1,
50+
row: Math.min(ref.to.row - 1, size.rows - 1),
51+
column: Math.min(ref.to.col - 1, size.columns - 1),
5052
};
51-
return matrix.toArray(matrix.slice(start, end, getData()), (cell) => {
53+
const dataSlice = matrix.slice(start, end, data);
54+
return matrix.toArray(dataSlice, (cell) => {
5255
if (!isNaN(cell?.value as number)) return Number(cell?.value);
5356
return cell?.value;
5457
});
@@ -112,7 +115,8 @@ export function evaluate(
112115
formulaParser: FormulaParser
113116
): Value {
114117
try {
115-
const returned = formulaParser.parse(formula, convertPointToCellRef(point));
118+
const position = convertPointToCellRef(point);
119+
const returned = formulaParser.parse(formula, position);
116120
return returned instanceof FormulaError ? returned.toString() : returned;
117121
} catch (error) {
118122
if (error instanceof FormulaError) {

0 commit comments

Comments
 (0)