Thank you @thiagarajan for waiting.
In general, it is a bad idea to try accessing Handsontable instance from HyperFormula. My reasons are:
- The HyperFormula instance is created before the Handsontable instance
- In our setup, HyperFormula is employed by Handsontable. Handsontable keeps an instance of HyperFormula. It would be bad, circular architecture to try accessing the “parent” instance of Handsontable from your instance of HyperFormula.
The naive (and bad) solution would be to access the data object that you passed to Handsontable by reference to count your rows: https://jsfiddle.net/warpech/fh25rvg8/
HyperFormula, being a calculation engine, actually keeps the whole representation of your data. Meaning: It already knows how many rows you have!
Here’s a good solution - example of a custom function volatile function with 0 parameters that returns the number of rows:
export class TmpPlugin extends FunctionPlugin implements FunctionPluginTypecheck<TmpPlugin> {
public static implementedFunctions = {
'SHEET_HEIGHT': {
method: 'sheetheight',
parameters: [
],
isVolatile: true,
},
}
public sheetheight(ast: ProcedureAst, state: InterpreterState): InterpreterValue {
return this.runFunction(ast.args, state, this.metadata('SHEET_HEIGHT'),
() => this.dependencyGraph.getSheetHeight(state.formulaAddress.sheet)
)
}
}
Here’s the same presented using the Handsontable formula plugin: https://jsfiddle.net/warpech/2t5ewodx/5/
The best news for you is that you don’t really have to care about huge ranges as =SUM(A1:A999999999)
or even infinity ranges when you supply just a column range (=SUM(A:A)
), because they are automatically normalized by the engine to the size of your data graph.
You would need to dig into our implementation of this function to see how that works: https://github.com/handsontable/hyperformula/blob/cca0887f21c10e56b723d1a5efb98780cc1cd89e/src/interpreter/plugin/NumericAggregationPlugin.ts#L525-L526
Further reading: