addFormula()
Description
Registers a custom formula function that can be used in cell formulas
Once registered, the formula is available in any cell by its uppercase name (e.g. =MYFUNC(A1, B2)).
Usage
type cellValue = string | number | boolean
type mathArgument = cellValue | cellValue[];
type mathFunction = (...x: mathArgument[]) => cellValue;
addFormula: (name: string, handler: mathFunction) => void;
Parameters
name- (string) required, the formula name (case-insensitive, stored as uppercase)handler- (function) required, a callback function that processes the input arguments (strings, numbers, booleans, or arrays of these) and returns a single value
note
The handler callback function must be synchronous. Using Promise or fetch inside the function is not allowed.
Example
const spreadsheet = new dhx.Spreadsheet("spreadsheet_container", {});
// adds a custom formula that doubles a value
spreadsheet.addFormula("DOUBLE", (value) => {
return value * 2;
});
// now use in cells: =DOUBLE(A1)
spreadsheet.parse([
{ cell: "A1", value: 4, format: "number" },
{ cell: "B1", value: "=DOUBLE(A1)", format: "number" }
]);
Change log: Added in v6.0
Related sample: Spreadsheet. Add custom formula
Related articles: Formulas and functions