Passing an object into new instance of hyperformula that can be accessed from Custom Functions #808
Replies: 9 comments 8 replies
-
You can't modify the config. I have an idea but it's not tested. this.hfInstance.getConfig().ctrl = this;
Object.getPrototypeOf(this).constructor.ctrl;
this.hfInstance.getFunctionPlugin(‘HYPER’);
class HyperPlugin extends BasePlugin {
#data = new Map<string, any>()
setData(key: string, value: any): void {
this.#data.set(key, value)
}
getData(key: string): any {
return this.#data.get(key)
}
hyper(ast, state) {
let ctrl = this.getData('ctrl')
// ... rest of the function
}
} Usage: let plugin = this.hfInstance.getFunctionPlugin(‘HYPER’);
this.hfInstance.setData('ctrl', 12); I was wrong. See: #808 (reply in thread) |
Beta Was this translation helpful? Give feedback.
-
@sequba Running into this issue as well: I have some external state that I would like to use per hyperformula instance in a custom function. Specifically, I'm trying to support column headers which are stored outside of hyperformula, but would like |
Beta Was this translation helpful? Give feedback.
-
@BrianHung HyperFormula does not support storing custom data inside an instance object, but I suspect it might be possible with some "hacky" solution e.g. passing custom data as a config parameter that is intended for another purpose. Can you give us more context about this use case? We might consider adding such a feature. Why the external state cannot be passed to a custom function as an argument? Do you need to use multiple instances of HyperFormula or a single instance with multiple sheets would be enough? I'd love to see some code snippets ;) |
Beta Was this translation helpful? Give feedback.
-
+1 here. It's very hacky to rely on a global singleton to store external state, since this precludes the possibility of ever instantiating more than one instance of hyperformula. Likewise, the suggestion made elsewhere to resolve custom functions before creating a a hyperformula instance is infeasible if there is any type of non-constant dependencies within the custom function parameters. Most libraries solve this by virtue of allowing a flexible
Then, in the custom function, we can make use as follows:
I've tried various approaches to modify All that's required to solve this issue is to allow an untyped |
Beta Was this translation helpful? Give feedback.
-
I'm willing to open the ~few line PR if it can be merged. |
Beta Was this translation helpful? Give feedback.
-
@michaelbridge good idea. We discussed it with the team and we think, that HyperFormula will benefit from the proposed untyped
Feel free to open such a PR. It will certainly help us release this feature earlier. I'll be happy to review it and merge once it's finished. |
Beta Was this translation helpful? Give feedback.
-
Great, I have completed the contributor agreement. I'll open a PR once I can push to the repo. |
Beta Was this translation helpful? Give feedback.
-
@michaelbridge @sturichardson @BrianHung Do we have anything else to cover (you all have different projects and needs so I would like to confirm that) or with the merge of #1398 and #1396 in v2.7.0 we can close this issue as solved? |
Beta Was this translation helpful? Give feedback.
-
I'm closing this discussion as the feature was implemented in HyperFormula 2.7.0. @michaelbridge @sturichardson @BrianHung please, re-open it if you had further questions or comments. |
Beta Was this translation helpful? Give feedback.
-
What I am trying to do is... (within handsontable)
Create many different instances of Hyperformula.
this.hfInstance = HyperFormula.buildEmpty();
When I create these instances I want to somehow attach some data (that I want to be different for different instances) that will be accessible in the custom functions.
So far I have tried:
let plugin = this.hfInstance.getFunctionPlugin(‘HYPER’);
this.hfInstance.getConfig().ctrl = this;
plugin.ctrl = this;
In the custom function:
hyper(ast, state) {
let ctrl = Object.getPrototypeOf(this).constructor.ctrl;
console.log(ctrl.key);
return ‘Hyperformula’.length;
}
Unfortunately this will not work for me as there is only one instance of the custom formula so each instance of HyperFormula shares the same instance of the custom formula.
Is there someway to achieve what I want...? I thought I might be able to add in a new custom config property that would be accessible from custom functions but cannot see a way to achieve this?
Beta Was this translation helpful? Give feedback.
All reactions