@libregrid/formulas 1.3.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/NOTICE +1 -0
- package/README.md +63 -0
- package/dist/expression.d.ts +221 -0
- package/dist/expression.js +1142 -0
- package/dist/expression.js.map +1 -0
- package/dist/formulaCellEditor.d.ts +58 -0
- package/dist/formulaCellEditor.js +299 -0
- package/dist/formulaCellEditor.js.map +1 -0
- package/dist/formulaDataService.d.ts +33 -0
- package/dist/formulaDataService.js +84 -0
- package/dist/formulaDataService.js.map +1 -0
- package/dist/formulaInputManager.d.ts +19 -0
- package/dist/formulaInputManager.js +37 -0
- package/dist/formulaInputManager.js.map +1 -0
- package/dist/formulaService.d.ts +154 -0
- package/dist/formulaService.js +661 -0
- package/dist/formulaService.js.map +1 -0
- package/dist/formulasCss.d.ts +5 -0
- package/dist/formulasCss.js +91 -0
- package/dist/formulasCss.js.map +1 -0
- package/dist/formulasModule.d.ts +37 -0
- package/dist/formulasModule.js +59 -0
- package/dist/formulasModule.js.map +1 -0
- package/dist/index.d.ts +6 -0
- package/dist/index.js +7 -0
- package/dist/index.js.map +1 -0
- package/dist/version.d.ts +1 -0
- package/dist/version.js +4 -0
- package/dist/version.js.map +1 -0
- package/package.json +22 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 LibreGrid contributors
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/NOTICE
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
LibreGrid is an independent open-source project and is not affiliated with AG Grid Ltd.
|
package/README.md
ADDED
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
# @libregrid/formulas
|
|
2
|
+
|
|
3
|
+
Spreadsheet-style cell formulas: users type `=...` expressions into grid cells —
|
|
4
|
+
A1-notation references, ranges, operators and functions — and values recompute
|
|
5
|
+
automatically when referenced data changes. Includes the tokenising formula cell
|
|
6
|
+
editor, an external `formulaDataSource` store, and custom function registration.
|
|
7
|
+
|
|
8
|
+
Replaces AG Grid Enterprise's `Formula` module.
|
|
9
|
+
|
|
10
|
+
## Install
|
|
11
|
+
|
|
12
|
+
```bash
|
|
13
|
+
npm install ag-grid-community @libregrid/formulas
|
|
14
|
+
```
|
|
15
|
+
|
|
16
|
+
Requires `ag-grid-community >=36.1.0 <37` as a peer dependency.
|
|
17
|
+
|
|
18
|
+
## Usage
|
|
19
|
+
|
|
20
|
+
```ts
|
|
21
|
+
import { ModuleRegistry, AllCommunityModule, createGrid } from 'ag-grid-community';
|
|
22
|
+
import { FormulasModule } from '@libregrid/formulas';
|
|
23
|
+
|
|
24
|
+
ModuleRegistry.registerModules([AllCommunityModule, FormulasModule]);
|
|
25
|
+
|
|
26
|
+
const api = createGrid(document.querySelector('#grid')!, {
|
|
27
|
+
columnDefs: [
|
|
28
|
+
{ field: 'product' },
|
|
29
|
+
{ field: 'price' },
|
|
30
|
+
{ field: 'quantity' },
|
|
31
|
+
{ field: 'subtotal', allowFormula: true },
|
|
32
|
+
],
|
|
33
|
+
rowData: [
|
|
34
|
+
{ product: 'Bananas', price: 2.5, quantity: 6, subtotal: '=C2*B2' },
|
|
35
|
+
],
|
|
36
|
+
getRowId: (params) => String(params.data.product),
|
|
37
|
+
});
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
Formulas are strings starting with `=` stored in the cell value (or in an
|
|
41
|
+
external store via `gridOptions.formulaDataSource`). References use A1
|
|
42
|
+
notation (`B2`, `$A$1`), ranges (`A1:B10`), and the documented operator and
|
|
43
|
+
function set. `gridOptions.formulaFuncs` registers custom functions.
|
|
44
|
+
|
|
45
|
+
Columns with `allowFormula: true` use the tokenising `agFormulaCellEditor` by
|
|
46
|
+
default. Formulas require Client-Side Row Model row IDs; tree data, row
|
|
47
|
+
grouping, pivot and server-side row models are not supported (per the AG Grid
|
|
48
|
+
documentation).
|
|
49
|
+
|
|
50
|
+
## The `formula` bean and `@libregrid/calculated-columns`
|
|
51
|
+
|
|
52
|
+
This package owns the canonical `formula` bean implementation. When
|
|
53
|
+
`@libregrid/calculated-columns` is installed alongside, both modules declare the
|
|
54
|
+
same service class, so exactly one instance serves both features — calculated
|
|
55
|
+
columns keep their same-row `[colId]` expressions, per-cell formulas gain
|
|
56
|
+
cell/range references.
|
|
57
|
+
|
|
58
|
+
## Attribution
|
|
59
|
+
|
|
60
|
+
LibreGrid is an independent open-source project and is not affiliated with AG
|
|
61
|
+
Grid Ltd. "AG Grid" is a trademark of AG Grid Ltd. This package implements
|
|
62
|
+
contracts published under MIT in `ag-grid-community`; no AG Grid Enterprise
|
|
63
|
+
code is used or inspected.
|
|
@@ -0,0 +1,221 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Expression engine for calculated columns and per-cell formulas.
|
|
3
|
+
*
|
|
4
|
+
* Two parse modes share one grammar core:
|
|
5
|
+
*
|
|
6
|
+
* - **`'expression'` mode** (default — calculated columns, gap-plan A2):
|
|
7
|
+
* same-row bracket references (`[colId]`), the public operator set, and the
|
|
8
|
+
* public provided-function set (AG Grid docs 36.1, "Calculated Columns" +
|
|
9
|
+
* "Formula Reference": the same operators and functions Formulas uses).
|
|
10
|
+
* Evaluation is same-row: a reference resolves the referenced column's value
|
|
11
|
+
* in the *same* row.
|
|
12
|
+
* - **`'cell'` mode** (per-cell formulas, gap-plan A1): additionally accepts
|
|
13
|
+
* A1-notation cell references (`B2`, `AA12`), `$`-absolute refs (`$A$1`,
|
|
14
|
+
* `A$1`, `=$A1`), rectangular ranges (`A1:B2`) and the LibreGrid long-hand
|
|
15
|
+
* reference format (`[colId:rowId]`, ranges `[colIdStart:rowIdStart..colIdEnd:rowIdEnd]`).
|
|
16
|
+
* Range-taking functions (`SUMIF`, `COUNTIF`) receive ranges as iterable
|
|
17
|
+
* `RangeParam`-shaped arguments; built-ins flatten them per argument.
|
|
18
|
+
*
|
|
19
|
+
* Errors are spreadsheet-style codes returned as `FormulaError`; the formula
|
|
20
|
+
* service renders the code text in the cell (`#REF!`, `#NAME?`, `#CIRCREF!`,
|
|
21
|
+
* `#PARSE!`, `#VALUE!`, `#DIV/0!`, `#ERROR!`) and hands the error object to
|
|
22
|
+
* Community's formula-error CSS/tooltip hooks (`getFormulaError`).
|
|
23
|
+
*
|
|
24
|
+
* Grammar (precedence, low → high):
|
|
25
|
+
* or: and ('OR' and)*
|
|
26
|
+
* and: cmp (('AND') cmp)*
|
|
27
|
+
* cmp: add (( '=' | '<>' | '>' | '<' | '>=' | '<=' ) add)?
|
|
28
|
+
* add: mult (('+' | '-' | '&') mult)*
|
|
29
|
+
* mult: pow (('*' | '/') pow)*
|
|
30
|
+
* pow: unary ('^' pow)? (right-associative)
|
|
31
|
+
* unary: '-' unary | '+' unary | '!' unary | postfix
|
|
32
|
+
* postfix: primary ('%')*
|
|
33
|
+
* primary: number | string | '[ref]' | true | false | ident '(' args ')' | '(' or ')'
|
|
34
|
+
* cell mode extends primary with: A1 cell refs, `$`-absolute cell refs,
|
|
35
|
+
* cell ranges (`cell ':' cell`) and long-hand bracket refs.
|
|
36
|
+
* A bare identifier that is not a call is an unknown function (`#NAME?` at
|
|
37
|
+
* evaluation time, not parse time, so validation reports the right reason).
|
|
38
|
+
*/
|
|
39
|
+
export type FormulaErrorCode = '#REF!' | '#NAME?' | '#CIRCREF!' | '#PARSE!' | '#VALUE!' | '#DIV/0!' | '#ERROR!';
|
|
40
|
+
/** Spreadsheet-style formula error. `code` is the cell-rendered text. */
|
|
41
|
+
export declare class FormulaError extends Error {
|
|
42
|
+
readonly code: FormulaErrorCode;
|
|
43
|
+
constructor(code: FormulaErrorCode, message: string);
|
|
44
|
+
}
|
|
45
|
+
type BinOp = '+' | '-' | '*' | '/' | '^' | '&' | '=' | '<>' | '>' | '<' | '>=' | '<=' | 'or' | 'and';
|
|
46
|
+
/** A cell reference in either shorthand (A1) or long-hand (col/row ID) form. */
|
|
47
|
+
export type CellRef = {
|
|
48
|
+
shorthand: true;
|
|
49
|
+
letters: string;
|
|
50
|
+
row: number;
|
|
51
|
+
absCol: boolean;
|
|
52
|
+
absRow: boolean;
|
|
53
|
+
} | {
|
|
54
|
+
shorthand: false;
|
|
55
|
+
colId: string;
|
|
56
|
+
rowId: string;
|
|
57
|
+
};
|
|
58
|
+
export interface CellRangeBounds {
|
|
59
|
+
rowStart: number;
|
|
60
|
+
rowEnd: number;
|
|
61
|
+
/** Grid column object bounding the range start — typed `unknown` to keep this module grid-agnostic. */
|
|
62
|
+
colStart: unknown;
|
|
63
|
+
/** Grid column object bounding the range end — typed `unknown` to keep this module grid-agnostic. */
|
|
64
|
+
colEnd: unknown;
|
|
65
|
+
/** Resolved values of every cell in the range, row-major. */
|
|
66
|
+
values: unknown[];
|
|
67
|
+
}
|
|
68
|
+
/** `FormulaParam`-shaped range: bounds plus iteration over the resolved values. */
|
|
69
|
+
export interface RangeParamLike extends CellRangeBounds {
|
|
70
|
+
kind: 'range';
|
|
71
|
+
}
|
|
72
|
+
/** `FormulaParam`-shaped scalar. */
|
|
73
|
+
export interface ValueParamLike {
|
|
74
|
+
kind: 'value';
|
|
75
|
+
value: unknown;
|
|
76
|
+
}
|
|
77
|
+
export type FormulaParamLike = ValueParamLike | RangeParamLike;
|
|
78
|
+
export type ExprNode = {
|
|
79
|
+
kind: 'number';
|
|
80
|
+
value: number;
|
|
81
|
+
} | {
|
|
82
|
+
kind: 'string';
|
|
83
|
+
value: string;
|
|
84
|
+
} | {
|
|
85
|
+
kind: 'boolean';
|
|
86
|
+
value: boolean;
|
|
87
|
+
} | {
|
|
88
|
+
kind: 'ref';
|
|
89
|
+
colId: string;
|
|
90
|
+
} | {
|
|
91
|
+
kind: 'cell';
|
|
92
|
+
ref: CellRef;
|
|
93
|
+
} | {
|
|
94
|
+
kind: 'range';
|
|
95
|
+
start: CellRef;
|
|
96
|
+
end: CellRef;
|
|
97
|
+
} | {
|
|
98
|
+
kind: 'binary';
|
|
99
|
+
op: BinOp;
|
|
100
|
+
left: ExprNode;
|
|
101
|
+
right: ExprNode;
|
|
102
|
+
} | {
|
|
103
|
+
kind: 'unary';
|
|
104
|
+
op: '+' | '-' | '!';
|
|
105
|
+
operand: ExprNode;
|
|
106
|
+
} | {
|
|
107
|
+
kind: 'percent';
|
|
108
|
+
operand: ExprNode;
|
|
109
|
+
} | {
|
|
110
|
+
kind: 'call';
|
|
111
|
+
name: string;
|
|
112
|
+
args: ExprNode[];
|
|
113
|
+
};
|
|
114
|
+
export type ParseMode = 'expression' | 'cell';
|
|
115
|
+
export interface FormulaToken {
|
|
116
|
+
type: 'num' | 'str' | 'ref' | 'cell' | 'ident' | 'op';
|
|
117
|
+
value: string;
|
|
118
|
+
}
|
|
119
|
+
/** True when `text` looks like a shorthand cell reference and not a function call or name. */
|
|
120
|
+
export declare function isCellRefText(text: string): boolean;
|
|
121
|
+
export declare function tokenize(source: string, mode?: ParseMode): FormulaToken[];
|
|
122
|
+
/** Parse a tokenised A1 reference (`B2`, `$A$1`, `A$1`) into a `CellRef`. */
|
|
123
|
+
export declare function parseCellRefText(text: string): CellRef;
|
|
124
|
+
export declare function parseExpression(source: string, mode?: ParseMode): ExprNode;
|
|
125
|
+
/** Parse a per-cell formula body, stripping one leading `=` when present. */
|
|
126
|
+
export declare function parseCellFormula(source: string): ExprNode;
|
|
127
|
+
/** Whether the formula text carries the `=` value-form prefix. */
|
|
128
|
+
export declare function hasFormulaPrefix(value: string): boolean;
|
|
129
|
+
export declare function toNum(v: unknown): number;
|
|
130
|
+
export declare function toBool(v: unknown): boolean;
|
|
131
|
+
export declare function toStr(v: unknown): string;
|
|
132
|
+
export type FormulaFunc = (args: unknown[]) => unknown;
|
|
133
|
+
export declare const FORMULA_FUNCTIONS: Readonly<Record<string, FormulaFunc>>;
|
|
134
|
+
export declare const FORMULA_FUNCTION_NAMES: readonly string[];
|
|
135
|
+
export declare function getFormulaFunction(name: string): FormulaFunc | undefined;
|
|
136
|
+
export interface ExpressionEvaluator {
|
|
137
|
+
/** Parse mode driving the node set (default `'expression'` — calculated columns). */
|
|
138
|
+
mode?: ParseMode;
|
|
139
|
+
/** Same-row value of the referenced column. Throws `FormulaError('#REF!')` when the column does not exist. */
|
|
140
|
+
resolveColumn(colId: string): unknown;
|
|
141
|
+
/** Whether `colId` is already on the active resolution chain (circular-reference guard). */
|
|
142
|
+
isResolving(colId: string): boolean;
|
|
143
|
+
/** Value of one referenced cell (cell mode). Throws `FormulaError('#REF!')` for unresolvable refs. */
|
|
144
|
+
resolveCell?(ref: CellRef): unknown;
|
|
145
|
+
/** Bounds + resolved values of a rectangular range (cell mode), row-major. */
|
|
146
|
+
resolveRange?(start: CellRef, end: CellRef): CellRangeBounds;
|
|
147
|
+
/** Custom function registry (cell mode) — consulted before the built-ins. */
|
|
148
|
+
resolveCustomFunction?(name: string): ((params: {
|
|
149
|
+
row: unknown;
|
|
150
|
+
column: unknown;
|
|
151
|
+
args: Iterable<FormulaParamLike>;
|
|
152
|
+
values: Iterable<unknown>;
|
|
153
|
+
}) => unknown) | undefined;
|
|
154
|
+
/** Row/column passed through to custom functions (cell mode). */
|
|
155
|
+
functionContext?: {
|
|
156
|
+
row: unknown;
|
|
157
|
+
column: unknown;
|
|
158
|
+
};
|
|
159
|
+
}
|
|
160
|
+
export declare function evaluate(node: ExprNode, ev: ExpressionEvaluator): unknown;
|
|
161
|
+
export interface ValidateOptions {
|
|
162
|
+
/** When provided, each referenced colId must resolve or validation fails with `#REF!`. */
|
|
163
|
+
resolveReference?: (colId: string) => boolean;
|
|
164
|
+
/** Parse mode — `'cell'` enables A1/range/long-hand references. */
|
|
165
|
+
mode?: ParseMode;
|
|
166
|
+
}
|
|
167
|
+
/** Parse (and optionally reference-check) without evaluating. `null` when valid. */
|
|
168
|
+
export declare function validateExpression(source: string, options?: ValidateOptions): FormulaError | null;
|
|
169
|
+
/** Unique referenced colIds (bracket refs) in declaration order. */
|
|
170
|
+
export declare function referencedColumnIds(node: ExprNode): string[];
|
|
171
|
+
/** Every cell reference in the expression, in declaration order (cell mode). */
|
|
172
|
+
export declare function referencedCells(node: ExprNode): CellRef[];
|
|
173
|
+
/** Column letters for a 0-based column position (0 → `A`, 25 → `Z`, 26 → `AA`). */
|
|
174
|
+
export declare function columnLetters(position: number): string;
|
|
175
|
+
/** 0-based column position for column letters (`A` → 0, `AA` → 26). */
|
|
176
|
+
export declare function columnPosition(letters: string): number;
|
|
177
|
+
export interface CellFormulaFormat {
|
|
178
|
+
/** Column ID for a 0-based visible leaf column position. Throws `#REF!` when unmappable. */
|
|
179
|
+
colIdAt(position: number): string;
|
|
180
|
+
/** 0-based visible leaf column position for a column ID. Throws `#REF!` when unmappable. */
|
|
181
|
+
positionOf(colId: string): number;
|
|
182
|
+
/** Row ID for a 0-based row index. Throws `#REF!` when unmappable. */
|
|
183
|
+
rowIdAt(index: number): string;
|
|
184
|
+
/** 0-based row index for a row ID. Throws `#REF!` when unmappable. */
|
|
185
|
+
indexOf(rowId: string): number;
|
|
186
|
+
}
|
|
187
|
+
/** Serialise an AST back to shorthand (A1) text, parenthesising to preserve precedence. */
|
|
188
|
+
export declare function stringifyCellFormula(node: ExprNode, format?: CellFormulaFormat): string;
|
|
189
|
+
/** Serialise an AST to the long-hand storage format (col/row IDs). */
|
|
190
|
+
export declare function stringifyCellFormulaLong(node: ExprNode, format: CellFormulaFormat): string;
|
|
191
|
+
export interface OffsetShift {
|
|
192
|
+
value: string;
|
|
193
|
+
rowDelta?: number;
|
|
194
|
+
columnDelta?: number;
|
|
195
|
+
/**
|
|
196
|
+
* When `true`, each reference keeps its original format (long-hand stays
|
|
197
|
+
* long-hand). Default rewrites every reference to shorthand — which needs
|
|
198
|
+
* `format` when the formula contains long-hand references.
|
|
199
|
+
*/
|
|
200
|
+
useRefFormat?: boolean;
|
|
201
|
+
/** Conversion context used when rewriting long-hand references to shorthand. */
|
|
202
|
+
format?: CellFormulaFormat;
|
|
203
|
+
}
|
|
204
|
+
/**
|
|
205
|
+
* Shift a formula's relative references by row/column deltas (fill handle).
|
|
206
|
+
* Absolute (`$`) anchors never move; long-hand references are already
|
|
207
|
+
* ID-pinned and never move.
|
|
208
|
+
*/
|
|
209
|
+
export declare function shiftFormula(params: OffsetShift): string;
|
|
210
|
+
/**
|
|
211
|
+
* Convert a formula between shorthand (A1) and long-hand (col/row ID) forms.
|
|
212
|
+
* `toLong: true` → long-hand storage form; `false` → shorthand display form.
|
|
213
|
+
* The `=` value-form prefix is preserved.
|
|
214
|
+
*/
|
|
215
|
+
export declare function convertFormula(value: string, format: CellFormulaFormat, toLong: boolean): string;
|
|
216
|
+
export declare const FORMULA_FUNCTION_DESCRIPTIONS: Readonly<Record<string, string>>;
|
|
217
|
+
export declare const FORMULA_OPERATORS: ReadonlyArray<{
|
|
218
|
+
op: string;
|
|
219
|
+
description: string;
|
|
220
|
+
}>;
|
|
221
|
+
export {};
|