Skip to content
Open
Show file tree
Hide file tree
Changes from 4 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
14 changes: 8 additions & 6 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];
return params.data[colDef.id] ?? null;
Comment thread
flomillot marked this conversation as resolved.
}
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);
const result = limitedEvaluate(colDef.formula, scope, params.context?.compiledFormulaCache);
Comment thread
coderabbitai[bot] marked this conversation as resolved.
return result != null ? validateFormulaResult(result, colDef.type) : null;
} catch (e) {
if (e instanceof MathJsValidationError) {
return { error: e.error };
}
return undefined;
return null;
}
};

Expand Down
32 changes: 27 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,38 @@ 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 };
export type CompiledFormulaCache = Map<string, CompiledFormula>;
export const createCompiledFormulaCache = (): CompiledFormulaCache => new Map();

const getCompiledFormula = (expr: string, cache?: CompiledFormulaCache): CompiledFormula => {
let entry = cache?.get(expr);
if (!entry) {
try {
entry = { compiled: originalParse(normalizeFormula(expr)).compile() };
} catch (error) {
entry = { error };
}
cache?.set(expr, entry);
}
return entry;
};

export const limitedEvaluate = (expr: string, scope?: object, cache?: CompiledFormulaCache) => {
const entry = getCompiledFormula(expr, cache);
if ('error' in entry) {
throw entry.error;
}
const result = entry.compiled.evaluate(scope);
if (typeof result === 'function') {
throw new MathJsValidationError('spreadsheet/formula/function-reference/disabled');
}
Expand Down
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 { FunctionComponent, useCallback, useMemo } from 'react';
import { FunctionComponent, useCallback, useMemo, useRef } from 'react';
import { ListItemIcon, ListItemText, Menu, MenuItem, useTheme } from '@mui/material';
import { useIntl } from 'react-intl';
import { CustomAGGrid } from '@gridsuite/commons-ui';
Expand All @@ -21,6 +21,7 @@ import { isCalculationRow } from '../../utils/calculation-utils';
import { AGGRID_LOCALES } from '../../../../translations/not-intl/aggrid-locales';
import { refreshSpreadsheetAfterFilterChanged } from './hooks/use-spreadsheet-gs-filter';
import { useEquipmentContextMenu } from './hooks/useEquipmentContextMenu';
import { createCompiledFormulaCache } from '../../columns/utils/math';

const DEFAULT_ROW_HEIGHT = 28;

Expand Down Expand Up @@ -134,7 +135,13 @@ export const EquipmentTable: FunctionComponent<EquipmentTableProps> = ({
[currentNode?.type, theme, isDataEditable]
);

const gridContext = useMemo(() => ({ theme, currentNode, studyUuid }), [currentNode, studyUuid, theme]);
// The Map lives in a ref, NOT in the memo below: gridContext is rebuilt on every currentNode
// change, and creating the cache there would silently flush all compiled formulas per rebuild.
const compiledFormulaCache = useRef(createCompiledFormulaCache()).current;
Comment thread
flomillot marked this conversation as resolved.
Outdated
const gridContext = useMemo(
() => ({ theme, currentNode, studyUuid, compiledFormulaCache }),
[currentNode, studyUuid, theme, compiledFormulaCache]
);

return (
<>
Expand Down
Loading