Skip to content

Commit

Permalink
[FIX] evaluation: add a getter to evaluate value and format
Browse files Browse the repository at this point in the history
This commit introduces a new getter `evaluateFormulaResult` which
is equivalent to the existing `evaluateFormula` but returns the
value and the format instead of just the value.

closes #4806

Task: 4102210
X-original-commit: a5da70a
Signed-off-by: Rémi Rahir (rar) <[email protected]>
Signed-off-by: Lucas Lefèvre (lul) <[email protected]>
  • Loading branch information
LucasLefevre authored and rrahir committed Aug 9, 2024
1 parent 2367612 commit 10125ff
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 21 deletions.
20 changes: 15 additions & 5 deletions src/plugins/ui_core_views/cell_evaluation/evaluation_plugin.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { isExportableToExcel } from "../../../formulas/index";
import { matrixMap } from "../../../functions/helpers";
import { getItemId, positions, toXC } from "../../../helpers/index";
import { CellErrorType } from "../../../types/errors";
import {
CellPosition,
CellValue,
Expand All @@ -12,11 +12,13 @@ import {
Format,
FormattedValue,
FormulaCell,
FunctionResultObject,
Matrix,
Range,
UID,
Zone,
invalidateDependenciesCommands,
isMatrix,
} from "../../../types/index";
import { FormulaCellWithDependencies } from "../../core";
import { UIPlugin, UIPluginConfig } from "../../ui_plugin";
Expand Down Expand Up @@ -141,6 +143,7 @@ import { Evaluator } from "./evaluator";
export class EvaluationPlugin extends UIPlugin {
static getters = [
"evaluateFormula",
"evaluateFormulaResult",
"getCorrespondingFormulaCell",
"getRangeFormattedValues",
"getRangeValues",
Expand Down Expand Up @@ -211,11 +214,18 @@ export class EvaluationPlugin extends UIPlugin {
// ---------------------------------------------------------------------------

evaluateFormula(sheetId: UID, formulaString: string): CellValue | Matrix<CellValue> {
try {
return this.evaluator.evaluateFormula(sheetId, formulaString);
} catch (error) {
return error.value || CellErrorType.GenericError;
const result = this.evaluateFormulaResult(sheetId, formulaString);
if (isMatrix(result)) {
return matrixMap(result, (cell) => cell.value);
}
return result.value;
}

evaluateFormulaResult(
sheetId: UID,
formulaString: string
): Matrix<FunctionResultObject> | FunctionResultObject {
return this.evaluator.evaluateFormulaResult(sheetId, formulaString);
}

/**
Expand Down
37 changes: 21 additions & 16 deletions src/plugins/ui_core_views/cell_evaluation/evaluator.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
import { compile } from "../../../formulas";
import { implementationErrorMessage } from "../../../functions";
import { matrixMap } from "../../../functions/helpers";
import { handleError, implementationErrorMessage } from "../../../functions";
import { lazy, positionToZone, toXC, union, unionPositionsToZone } from "../../../helpers";
import { createEvaluatedCell, evaluateLiteral } from "../../../helpers/cells";
import { ModelConfig } from "../../../model";
import { onIterationEndEvaluationRegistry } from "../../../registries/evaluation_registry";
import { _t } from "../../../translation";
import {
CellPosition,
CellValue,
CellValueType,
EvaluatedCell,
FormulaCell,
Expand Down Expand Up @@ -191,26 +189,33 @@ export class Evaluator {
console.info("evaluate all cells", performance.now() - start, "ms");
}

evaluateFormula(sheetId: UID, formulaString: string): CellValue | Matrix<CellValue> {
evaluateFormulaResult(
sheetId: UID,
formulaString: string
): FunctionResultObject | Matrix<FunctionResultObject> {
const compiledFormula = compile(formulaString);

const ranges: Range[] = compiledFormula.dependencies.map((xc) =>
this.getters.getRangeFromSheetXC(sheetId, xc)
);
this.updateCompilationParameters();
const result = updateEvalContextAndExecute(
{ ...compiledFormula, dependencies: ranges },
this.compilationParams,
sheetId,
undefined
);
if (isMatrix(result)) {
return matrixMap(result, (cell) => cell.value);
}
if (result.value === null) {
return 0;
try {
const result = updateEvalContextAndExecute(
{ ...compiledFormula, dependencies: ranges },
this.compilationParams,
sheetId,
undefined
);
if (isMatrix(result)) {
return result;
}
if (result.value === null) {
return { value: 0, format: result.format };
}
return result;
} catch (error) {
return handleError(error, "");
}
return result.value;
}

private getAllCells(): PositionSet {
Expand Down

0 comments on commit 10125ff

Please sign in to comment.