Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/engine/arrayEvaluation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { indexToColumn } from "./parser";
import { isFormulaError, unknownError } from "./formulaError";
import { isSpreadsheetErrorValue, NA_ERROR, spreadsheetError, type SpreadsheetError } from "./spreadsheet-errors";
import { renderOperand } from "./expressionText";
import type { EvaluatorContext } from "./evaluator";
import type { EvaluatorContext } from "./evaluatorContext";

/** An array operand as the element formulas see it: its shape, and what stands in its place in the
* formula for one element — the text written there, or the error that element is. */
Expand Down
25 changes: 2 additions & 23 deletions src/engine/evaluator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* Evaluates spreadsheet formulas including functions, cell references, and arithmetic
*/

import { CALL_NAMES, FUNCTION_NAME_SOURCE, functionRegistry, isCallableName, tooFewArgumentsError, type CellCoordinates } from "./registry";
import { CALL_NAMES, FUNCTION_NAME_SOURCE, functionRegistry, isCallableName, tooFewArgumentsError } from "./registry";

const SIMPLE_CALL = new RegExp(`^(${FUNCTION_NAME_SOURCE})\\((.*)\\)$`, "i");
const CALL_START = new RegExp(`^(${FUNCTION_NAME_SOURCE})\\(`, "i");
Expand All @@ -27,28 +27,7 @@ import { booleansAsNumbers, logicalLiteralsAsBooleans, loneBoolean } from "./log
import { textOperandsAsNumbers } from "./textArithmetic";
import { endOfStringLiteral, findCellRefs, isSafeConcatExpression, parseFunctionArgs, renderOperand, substituteCellRefs } from "./expressionText";
import { evaluateOverArrays } from "./arrayEvaluation";

/**
* Evaluation context for formulas
*/
export interface EvaluatorContext {
getCellValue: (ref: string) => CellValue;
getRangeValues: (range: string) => CellValue[];
getRangeValuesRaw?: ((range: string) => CellValue[]) | undefined;
getRangeValuesKeepingBlanks?: ((range: string) => CellValue[]) | undefined;
countFilledCells?: ((range: string) => number) | undefined;
evaluateFormula: (formula: string) => CellValue;
/** A formula's value, a table included; see FunctionContext. */
evaluateValue?: ((formula: string) => FormulaValue) | undefined;
getRangeGrid?: ((range: string) => CellValue[][]) | undefined;
/** Reading order for an ambiguous slash date; see engine/date-locale.ts. */
preferDDMMYYYY?: boolean | undefined;
/** Whether the referenced cell holds nothing. `getCellValue` reads such a cell as 0, which
* is right for arithmetic and wrong for `&`, where a blank cell joins as "". */
isCellEmpty?: ((ref: string) => boolean) | undefined;
currentCell?: CellCoordinates | undefined;
currentSheet?: string | undefined;
}
import type { EvaluatorContext } from "./evaluatorContext";

/** Run `new Function` on an expression the callers have already gated with a
* character allowlist. A JS parse/eval failure becomes #ERROR! — a genuinely
Expand Down
27 changes: 27 additions & 0 deletions src/engine/evaluatorContext.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// What a formula is evaluated against: reading cells and ranges, evaluating a nested formula, and
// where the formula is.

import type { CellCoordinates } from "./registry";
import type { CellValue, FormulaValue } from "./types";

/**
* Evaluation context for formulas
*/
export interface EvaluatorContext {
getCellValue: (ref: string) => CellValue;
getRangeValues: (range: string) => CellValue[];
getRangeValuesRaw?: ((range: string) => CellValue[]) | undefined;
getRangeValuesKeepingBlanks?: ((range: string) => CellValue[]) | undefined;
countFilledCells?: ((range: string) => number) | undefined;
evaluateFormula: (formula: string) => CellValue;
/** A formula's value, a table included; see FunctionContext. */
evaluateValue?: ((formula: string) => FormulaValue) | undefined;
getRangeGrid?: ((range: string) => CellValue[][]) | undefined;
/** Reading order for an ambiguous slash date; see engine/date-locale.ts. */
preferDDMMYYYY?: boolean | undefined;
/** Whether the referenced cell holds nothing. `getCellValue` reads such a cell as 0, which
* is right for arithmetic and wrong for `&`, where a blank cell joins as "". */
isCellEmpty?: ((ref: string) => boolean) | undefined;
currentCell?: CellCoordinates | undefined;
currentSheet?: string | undefined;
}
1 change: 1 addition & 0 deletions src/engine/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export * from "./translateFormula";
export * from "./formulaError";
export * from "./spreadsheet-errors";
export * from "./evaluator";
export * from "./evaluatorContext";
export * from "./expressionText";
export * from "./calculator";
export * from "./formulaRefs";
Expand Down
2 changes: 1 addition & 1 deletion src/engine/rangeReading.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export interface RangeReadOptions {

/** Before a range is read, the spills that could reach any of its cells: those reaching its
* bottom-right corner, since a table spills right and down. */
export function ensureSpillsCovering(sheet: OpenSheet, coords: readonly CellCoordinates[]): void {
function ensureSpillsCovering(sheet: OpenSheet, coords: readonly CellCoordinates[]): void {
const corner = coords.reduce((far, { row, col }) => ({ row: Math.max(far.row, row), col: Math.max(far.col, col) }), { row: -1, col: -1 });
if (corner.row >= 0) sheet.ensureSpillsReaching(corner.row, corner.col);
}
Expand Down
5 changes: 2 additions & 3 deletions src/vue/editorSelection.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
// With the JSON editor open, a click on a table cell selects that cell's JSON in the editor.

import { findCellJsonPosition } from "../engine";
import { normalizeSheetData } from "./sheetRows";
import { findCellJsonPosition, normalizeData } from "../engine";

/** Select, in `textarea` holding `editableJson`, the JSON of the cell at `rowIndex`, `colIndex` of the
* sheet at `sheetIndex`, and scroll it into view; nothing when the cell is not in the JSON. */
Expand All @@ -10,7 +9,7 @@ export function selectCellInEditor(textarea: HTMLTextAreaElement, editableJson:
const sheets = JSON.parse(editableJson);
const currentSheet = sheets[sheetIndex];
if (!currentSheet || !currentSheet.data) return;
const normalizedData = normalizeSheetData(currentSheet.data);
const normalizedData = normalizeData(currentSheet.data);
if (!normalizedData[rowIndex] || normalizedData[rowIndex][colIndex] === undefined) {
return;
}
Expand Down
35 changes: 0 additions & 35 deletions src/vue/sheetRows.ts

This file was deleted.

7 changes: 3 additions & 4 deletions src/vue/useMiniEditor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,8 @@

import { ref, type Ref } from "vue";
import type { SpreadsheetSheet, SpreadsheetCell } from "../core/types";
import { extractCellReferences, buildCellFromInput } from "../engine";
import { extractCellReferences, buildCellFromInput, normalizeData } from "../engine";
import { errorMessage as formatErrorMessage, isRecord } from "../engine/guards";
import { normalizeSheetData } from "./sheetRows";
import { getArrowKeyOffset, isWithinSheetBounds } from "./keyboardNav";

/** What the mini editor works on: the editor's JSON, the sheet shown, and how a change is saved. */
Expand Down Expand Up @@ -45,7 +44,7 @@ export function useMiniEditor({ editableData, activeSheetIndex, persistSheets }:
}

// Normalize the data in case it's malformed
const normalizedData = normalizeSheetData(currentSheet.data);
const normalizedData = normalizeData(currentSheet.data);

if (!normalizedData[rowIndex] || normalizedData[rowIndex][colIndex] === undefined) {
return;
Expand Down Expand Up @@ -119,7 +118,7 @@ export function useMiniEditor({ editableData, activeSheetIndex, persistSheets }:
const { row, col } = miniEditorCell.value;

// Normalize the data in case it's malformed
const normalizedData = normalizeSheetData(currentSheet.data);
const normalizedData = normalizeData(currentSheet.data);

// Ensure the row exists
while (normalizedData.length <= row) {
Expand Down
3 changes: 2 additions & 1 deletion tests/engine/run-evaluator-tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
* Run with: npx tsx tests/engine/run-evaluator-tests.ts
*/

import { evaluateFormula, type EvaluatorContext } from "../../src/engine/evaluator";
import { evaluateFormula } from "../../src/engine/evaluator";
import type { EvaluatorContext } from "../../src/engine/evaluatorContext";
import { parseFunctionArgs } from "../../src/engine/expressionText";
import "../../src/engine/functions"; // Load all functions
import { expect, scriptRunner } from "./scriptRunner";
Expand Down
Loading