Skip to content
10 changes: 6 additions & 4 deletions src/components/spreadsheet-view/columns/utils/column-mapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,24 +24,26 @@ export const SPREADSHEET_INVALID_CELL_CLASS = 'spreadsheet-invalid-cell';

const createValueGetter =
(colDef: ColumnDefinition) =>
(params: ValueGetterParams): CustomAggridValue | undefined => {
(params: ValueGetterParams): CustomAggridValue | null => {
try {
// Skip formula processing for pinned rows and use raw value
if (isCalculationRow(params.node?.data?.rowType)) {
return params.data[colDef.id];
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Outdated
}
const scope = { ...params.data };
const colDependencies = colDef.dependencies ?? [];

//Empty values are assumed to be equal to "undefined" by users
colDependencies.forEach((dep) => {
scope[dep] = params.getValue(dep);
scope[dep] = params.getValue(dep) ?? undefined;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And why undefined here btw ?

@Meklo Meklo Sep 7, 2026

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This undefined is crucial to keep existing formulas having this type of syntax "typeOf(IT10_Or) == 'undefined' ? IT10_Ex : typeOf(IT10_Ex) == 'undefined' ? IT10_Or : max(IT10_Or, IT10_Ex)" which are largely used in production.
As of today empty values were treated as undefined. In order to keep the system working while treating empty value as null, we now do the inverse and inject undefined in the scope when the value is empty

But if it's equal to 0 it should stay 0 you're right
Edit: same as above for the nullish coalescing operator

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay thanks, maybe update the comment to make it more explicit ?

});
const result = limitedEvaluate(colDef.formula, scope);
return result == null ? undefined : validateFormulaResult(result, colDef.type);
return result != null ? validateFormulaResult(result, colDef.type) : null;
} catch (e) {
if (e instanceof MathJsValidationError) {
return { error: e.error };
}
return undefined;
return null;
}
};

Expand Down
35 changes: 30 additions & 5 deletions src/components/spreadsheet-view/columns/utils/math.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/

import { all, create, parse } from 'mathjs';
import { all, create, EvalFunction, parse } from 'mathjs';
import { unitToKiloUnit, unitToMicroUnit } from '@gridsuite/commons-ui';

const instance = create(all);
Expand All @@ -24,16 +24,41 @@ function transformExpression(expr: string): string {
return expr.replaceAll(regex, `$1.steps[string($2)]`);
}

const originalEvaluate = instance.evaluate;
const originalParse = instance.parse;
Comment thread
flomillot marked this conversation as resolved.
Outdated

const normalizeFormula = (expr: string): string => transformExpression(expr.replaceAll('\\', '\\\\'));

// runs nothing ; the instance below is what formulas are evaluated against
export const parseFormula = (expr: string) => parse(normalizeFormula(expr));

export const limitedEvaluate = (expr: string | string[], scope?: object) => {
const transformedExpression: string | string[] = typeof expr === 'string' ? normalizeFormula(expr) : expr;
const result = originalEvaluate(transformedExpression, scope);
// Compile once per distinct formula string. AG Grid runs the value getter for every row of the
// table on each filter/sort pass, and mathjs re-parses the expression on every evaluate() call
type CompiledFormula = { compiled: EvalFunction } | { error: unknown };
const compiledFormulaCache = new Map<string, CompiledFormula>();

const getCompiledFormula = (expr: string) => {
let entry = compiledFormulaCache.get(expr);
if (!entry) {
try {
const ast = originalParse(normalizeFormula(expr));
entry = { compiled: ast.compile() };
} catch (error) {
// Cache parse failures too: a syntactically broken formula would otherwise
// pay the full parse cost again on every cell of every pass
entry = { error };
}
compiledFormulaCache.set(expr, entry);
}
if ('error' in entry) {
throw entry.error;
}
return entry;
};

export const limitedEvaluate = (expr: string, scope?: object) => {
let result;
const entry = getCompiledFormula(expr);
result = entry.compiled.evaluate(scope);
if (typeof result === 'function') {
throw new MathJsValidationError('spreadsheet/formula/function-reference/disabled');
}
Expand Down
Loading