Skip to content
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export function createComputeFunction(
acceptToVectorize.push(!argDefinition.acceptMatrix);
}

return applyVectorization(errorHandlingCompute.bind(this), args, acceptToVectorize);
return applyVectorization(this, errorHandlingCompute, args, acceptToVectorize);
}

function errorHandlingCompute(
Expand Down
16 changes: 13 additions & 3 deletions packages/o-spreadsheet-engine/src/functions/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {
NotAvailableError,
errorTypes,
} from "../types/errors";
import { LookupCaches } from "../types/functions";
import { EvalContext, LookupCaches } from "../types/functions";
import { Locale } from "../types/locale";
import {
Arg,
Expand Down Expand Up @@ -499,6 +499,7 @@ type VectorArgType = "horizontal" | "vertical" | "matrix";
* as ranges and invoke this helper directly within your `compute` implementation.
*/
export function applyVectorization(
evalCtx: EvalContext,
formula: (...args: Arg[]) => Matrix<FunctionResultObject> | FunctionResultObject,
args: Arg[],
acceptToVectorize: boolean[] | undefined = undefined
Expand Down Expand Up @@ -541,7 +542,7 @@ export function applyVectorization(

if (countVectorizedCol === 1 && countVectorizedRow === 1) {
// either this function is not vectorized or it ends up with a 1x1 dimension
return formula(...args);
return formula.call(evalCtx, ...args);
}

const getArgOffset: (i: number, j: number) => Arg[] = (i, j) =>
Expand All @@ -564,7 +565,16 @@ export function applyVectorization(
_t("Array arguments to [[FUNCTION_NAME]] are of different size.")
);
}
const singleCellComputeResult = formula(...getArgOffset(col, row));
const basePosition = evalCtx.__originCellPosition;
const ctx = { ...evalCtx };
if (basePosition) {
ctx.__originCellPosition = {
col: basePosition.col + col,
row: basePosition.row + row,
sheetId: basePosition.sheetId,
};
}
const singleCellComputeResult = formula.call(ctx, ...getArgOffset(col, row));
// In the case where the user tries to vectorize arguments of an array formula, we will get an
// array for every combination of the vectorized arguments, which will lead to a 3D matrix and
// we won't be able to return the values.
Expand Down
8 changes: 4 additions & 4 deletions packages/o-spreadsheet-engine/src/functions/module_logical.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ export const IF = {
],
compute: function (logicalExpression: Arg, valueIfTrue: Arg, valueIfFalse: Arg) {
if (isMultipleElementMatrix(logicalExpression)) {
return applyVectorization(IF.compute, [logicalExpression, valueIfTrue, valueIfFalse]);
return applyVectorization(this, IF.compute, [logicalExpression, valueIfTrue, valueIfFalse]);
}
const result = toBoolean(toScalar(logicalExpression)) ? valueIfTrue : valueIfFalse;
return result ?? { value: 0 };
Expand All @@ -94,7 +94,7 @@ export const IFERROR = {
],
compute: function (value: Arg, valueIfError: Arg) {
if (isMultipleElementMatrix(value)) {
return applyVectorization(IFERROR.compute, [value, valueIfError]);
return applyVectorization(this, IFERROR.compute, [value, valueIfError]);
}
const result = isEvaluationError(toScalar(value)?.value) ? valueIfError : value;
return result ?? { value: 0 };
Expand All @@ -116,7 +116,7 @@ export const IFNA = {
],
compute: function (value: Arg, valueIfError: Arg) {
if (isMultipleElementMatrix(value)) {
return applyVectorization(IFNA.compute, [value, valueIfError]);
return applyVectorization(this, IFNA.compute, [value, valueIfError]);
}
const result = toScalar(value)?.value === CellErrorType.NotAvailable ? valueIfError : value;
return result ?? { value: 0 };
Expand Down Expand Up @@ -149,7 +149,7 @@ export const IFS = {
}
while (values.length > 0) {
if (isMultipleElementMatrix(values[0])) {
return applyVectorization(IFS.compute, values);
return applyVectorization(this, IFS.compute, values);
}
const condition = toBoolean(toScalar(values.shift()));
const valueIfTrue = values.shift();
Expand Down
63 changes: 57 additions & 6 deletions packages/o-spreadsheet-engine/src/functions/module_lookup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ import {
import { toZone } from "../helpers/zones";
import { _t } from "../translation";
import { CellErrorType, EvaluationError, InvalidReferenceError } from "../types/errors";
import { AddFunctionDescription } from "../types/functions";
import { Arg, FunctionResultObject, Matrix, Maybe, Zone } from "../types/misc";
import { AddFunctionDescription, EvalContext } from "../types/functions";
import { Arg, FunctionResultObject, Matrix, Maybe, PivotCacheItem, Zone } from "../types/misc";
import { arg } from "./arguments";
import { expectNumberGreaterThanOrEqualToOne } from "./helper_assert";
import {
Expand Down Expand Up @@ -786,6 +786,13 @@ export const XLOOKUP = {
// Pivot functions
//--------------------------------------------------------------------------

function addPivotMetaDataToContext(context: EvalContext, item: PivotCacheItem) {
const { __originCellPosition: position } = context;
if (position) {
context.sendEvaluationMessage({ type: "addPivotToPosition", position, item });
}
}

// PIVOT.VALUE

export const PIVOT_VALUE = {
Expand All @@ -804,8 +811,13 @@ export const PIVOT_VALUE = {
const _pivotFormulaId = toString(formulaId);
const _measure = toString(measureName);
const pivotId = getPivotId(_pivotFormulaId, this.getters);
assertMeasureExist(pivotId, _measure, this.getters);
assertDomainLength(domainArgs);
try {
assertMeasureExist(pivotId, _measure, this.getters);
assertDomainLength(domainArgs);
} catch (e) {
addPivotMetaDataToContext(this, { type: "error", pivotId });
return e;
}
const pivot = this.getters.getPivot(pivotId);
const coreDefinition = this.getters.getPivotCoreDefinition(pivotId);

Expand All @@ -817,6 +829,7 @@ export const PIVOT_VALUE = {
pivot.init({ reload: pivot.needsReevaluation });
const error = pivot.assertIsValid({ throwOnError: false });
if (error) {
addPivotMetaDataToContext(this, { type: "error", pivotId });
return error;
}

Expand All @@ -825,6 +838,7 @@ export const PIVOT_VALUE = {
"Consider using a dynamic pivot formula: %s. Or re-insert the static pivot from the Data menu.",
`=PIVOT(${_pivotFormulaId})`
);
addPivotMetaDataToContext(this, { type: "error", pivotId });
return {
value: CellErrorType.GenericError,
message: _t("Dimensions don't match the pivot definition") + ". " + suggestion,
Expand All @@ -834,6 +848,12 @@ export const PIVOT_VALUE = {
if (this.getters.getActiveSheetId() === this.__originSheetId) {
this.getters.getPivotPresenceTracker(pivotId)?.trackValue(_measure, domain);
}
addPivotMetaDataToContext(this, {
type: "static",
pivotId,
pivotCell: { type: "VALUE", measure: _measure, domain },
});

return pivot.getPivotCellValueAndFormat(_measure, domain);
},
} satisfies AddFunctionDescription;
Expand All @@ -853,20 +873,27 @@ export const PIVOT_HEADER = {
) {
const _pivotFormulaId = toString(pivotId);
const _pivotId = getPivotId(_pivotFormulaId, this.getters);
assertDomainLength(domainArgs);
try {
assertDomainLength(domainArgs);
} catch (e) {
addPivotMetaDataToContext(this, { type: "error", pivotId: _pivotId });
return e;
}
const pivot = this.getters.getPivot(_pivotId);
const coreDefinition = this.getters.getPivotCoreDefinition(_pivotId);
addPivotDependencies(this, coreDefinition, []);
pivot.init({ reload: pivot.needsReevaluation });
const error = pivot.assertIsValid({ throwOnError: false });
if (error) {
addPivotMetaDataToContext(this, { type: "error", pivotId: _pivotId });
return error;
}
if (!pivot.areDomainArgsFieldsValid(domainArgs)) {
const suggestion = _t(
"Consider using a dynamic pivot formula: %s. Or re-insert the static pivot from the Data menu.",
`=PIVOT(${_pivotFormulaId})`
);
addPivotMetaDataToContext(this, { type: "error", pivotId: _pivotId });
return {
value: CellErrorType.GenericError,
message: _t("Dimensions don't match the pivot definition") + ". " + suggestion,
Expand All @@ -876,11 +903,32 @@ export const PIVOT_HEADER = {
if (this.getters.getActiveSheetId() === this.__originSheetId) {
this.getters.getPivotPresenceTracker(_pivotId)?.trackHeader(domain);
}

const lastNode = domain.at(-1);
if (lastNode?.field === "measure") {
return pivot.getPivotMeasureValue(toString(lastNode.value), domain);
const measure = toString(lastNode.value);
addPivotMetaDataToContext(this, {
type: "static",
pivotId: _pivotId,
pivotCell: { type: "MEASURE_HEADER", measure, domain: domain.slice(0, -1) },
});

return pivot.getPivotMeasureValue(measure, domain);
}
const { value, format } = pivot.getPivotHeaderValueAndFormat(domain);

const columns = pivot.definition.columns;
const isColumnHeader = columns.some((col) => col.nameWithGranularity === domain[0]?.field);
addPivotMetaDataToContext(this, {
type: "static",
pivotId: _pivotId,
pivotCell: {
type: "HEADER",
domain,
dimension: isColumnHeader ? "COL" : "ROW",
},
});

return {
value,
format:
Expand Down Expand Up @@ -941,13 +989,16 @@ export const PIVOT = {
pivot.init({ reload: pivot.needsReevaluation });
const error = pivot.assertIsValid({ throwOnError: false });
if (error) {
addPivotMetaDataToContext(this, { type: "error", pivotId });
return error;
}
const table = pivot.getCollapsedTableStructure();
if (table.numberOfCells > PIVOT_MAX_NUMBER_OF_CELLS) {
addPivotMetaDataToContext(this, { type: "error", pivotId });
return new EvaluationError(getPivotTooBigErrorMessage(table.numberOfCells, this.locale));
}
const cells = table.getPivotCells(pivotStyle);
addPivotMetaDataToContext(this, { type: "dynamic", pivotStyle, pivotId });

let headerRows = 0;
if (pivotStyle.displayColumnHeaders) {
Expand Down
9 changes: 7 additions & 2 deletions packages/o-spreadsheet-engine/src/functions/module_math.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { splitReference } from "../helpers";
import { toZone } from "../helpers/zones";
import { isSubtotalCell } from "../plugins/ui_feature/subtotal_evaluation";
import { _t } from "../translation";
import { EvaluatedCell } from "../types/cells";
import { DivisionByZeroError, EvaluationError } from "../types/errors";
Expand Down Expand Up @@ -1386,6 +1385,12 @@ export const SUBTOTAL = {
code -= 100;
acceptHiddenCells = false;
}

const { __originCellPosition: position } = this;
if (position) {
this.sendEvaluationMessage({ type: "addSubTotalToPosition", position });
}

if (code < 1 || code > 11) {
return new EvaluationError(
_t("The function code (%s) must be between 1 to 11 or 101 to 111.", code)
Expand All @@ -1410,7 +1415,7 @@ export const SUBTOTAL = {

for (let col = left; col <= right; col++) {
const cell = this.getters.getCell({ sheetId, col, row });
if (!cell || !isSubtotalCell(cell)) {
if (!cell || !this.getters.isSubtotalCell({ sheetId, col, row })) {
evaluatedCellToKeep.push(this.getters.getEvaluatedCell({ sheetId, col, row }));
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { CellPosition } from "../..";
import { Registry } from "../../registry";

export type EvaluationMessage =
| { type: "invalidateCell"; position: CellPosition }
| { type: "invalidateAllCells" }
| { type: string; [key: string]: any };

export interface EvaluationListener {
handleEvaluationMessage(message: EvaluationMessage): void;
}

export const evaluationListenerRegistry = new Registry<EvaluationListener>();
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ import { matrixMap } from "../../../functions/helpers";
import { PositionMap } from "../../../helpers/cells/position_map";
import { toXC } from "../../../helpers/coordinates";
import { lazy } from "../../../helpers/misc";
import {
evaluationListenerRegistry,
EvaluationMessage,
} from "../../../helpers/pivot/evaluation_listener_registry";
import { excludeTopLeft, positionToZone, union } from "../../../helpers/zones";
import { onIterationEndEvaluationRegistry } from "../../../registries/evaluation_registry";
import { _t } from "../../../translation";
Expand Down Expand Up @@ -139,6 +143,14 @@ export class Evaluator {
forwardSearch: new Map(),
reverseSearch: new Map(),
};
this.compilationParams.evalContext.sendEvaluationMessage = (message: EvaluationMessage) =>
this.sendToListeners(message);
}

private sendToListeners(message: EvaluationMessage) {
for (const listener of evaluationListenerRegistry.getAll()) {
listener.handleEvaluationMessage(message);
}
}

private createEmptyPositionSet() {
Expand Down Expand Up @@ -218,6 +230,7 @@ export class Evaluator {
evaluateAllCells() {
const start = performance.now();
this.evaluatedCells = new PositionMap();
this.sendToListeners({ type: "invalidateAllCells" });
const ranges: BoundedRange[] = [];
for (const sheetId of this.getters.getSheetIds()) {
const zone = this.getters.getSheetZone(sheetId);
Expand Down Expand Up @@ -325,7 +338,9 @@ export class Evaluator {
const { left, bottom, right, top } = range.zone;
for (let col = left; col <= right; col++) {
for (let row = top; row <= bottom; row++) {
this.evaluatedCells.delete({ sheetId: range.sheetId, col, row });
const position = { sheetId: range.sheetId, col, row };
this.sendToListeners({ type: "invalidateCell", position });
this.evaluatedCells.delete(position);
}
}
}
Expand Down Expand Up @@ -545,6 +560,7 @@ export class Evaluator {
continue;
}
this.evaluatedCells.delete(resultPosition);
this.sendToListeners({ type: "invalidateCell", position: resultPosition });
}
}
const sheetId = position.sheetId;
Expand Down
Loading