Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DOC: discrepancies between doc and custom formulas behavior in v18 #5219

Open
dberardo-com opened this issue Nov 18, 2024 · 6 comments
Open
Labels
bug Something isn't working

Comments

@dberardo-com
Copy link

using version 18, defined custom formulas are not given single scalar as input but rather objects:

the first argument of mz custom function is now suddenly an object: {
"value": 1,
"formattedValue": "1",
"type": "number",
"isAutoSummable": true,
"defaultAlign": "right"
}

and successive arguments are also object of this shape: {
"value": 123,
}

is this breaking change by design or is there something wrong with my setup ?

ref: https://github.com/odoo/o-spreadsheet/blob/18.0/doc/add_function.md

@dberardo-com dberardo-com added the bug Something isn't working label Nov 18, 2024
@dberardo-com
Copy link
Author

also: throwing custom "LOADING error" seems not to work anymore, could this be?

#1935 (comment)

@hokolomopo
Copy link
Contributor

Hello 👋

It is an intended change, now the arguments of the functions are FunctionResultObject. This was made so we could also have errors as arguments (eg. TRANSPOSE can now have errors in its range).

You can still use the usual helpers to convert them to primitive types. Or use arg.value.

Throwing errors should still work tho, do your custom error extends EvaluationError ? We didn't change much there AFAIK.

@dberardo-com
Copy link
Author

dberardo-com commented Nov 24, 2024

i see, is the function expected to return a single scalar or should it also be an object of the new shape ?

and i had a question in past, maybe now this is covered by the new update: is it possible for a formula to get the coordinates of the cell and sheets of where it is being executed ? i need to use this unique key as a cache key for async cells.

UPDATE: i found out that there a variable: this.__originCellPosition available inside a formula, i believe i can use this one ?

Also this brings me to an important matter, in version 16 (the one i am currently using) it was possible to directly modify the "this" object inside the formulas, so as to add useful internal state variables. i have the feeling that now the context of the "this" object is always reset at every dispatch iteration, so i cant use it like before.... is this correct?

@hokolomopo
Copy link
Contributor

Hello @dberardo-com 👋

Yes __originCellPosition is how you can check the coordinates of the cell currently evaluated.

As for the this obejct inside formula, it was never intended to be used as you described so we may have removed this (unintended) behaviour in the latest versions. If you want to keep state variables i'd suggest to use plugins instead. Or there's a model confeig that is passed inside the functione val context if you want to play with that

@dberardo-com
Copy link
Author

thanks for the reply.

Yes __originCellPosition is how you can check the coordinates of the cell currently evaluated.

indeed, this is a great addition for my use case. although there have been instances where this variable was null/undefined for a couple of iterations ... i would just ignore it in that case-

As for the this obejct inside formula, it was never intended to be used as you described so we may have removed this (unintended) behaviour in the latest versions

i understand, i thought that was the case but wanted to go sure. i think i will go for the custom plugin variant. and just to be sure again: custom plugins are singleton inside a spreadsheet model, i.e. there will exist only one instance of any plugin inside the model and all formulas share it, is this correct?

also, talking about cache: is there a way for a plugin that manages a shared cache to recognize when a formula is "unmounted (deleted or so)" in order to clear the cache resources related to that specific formula instance (cell) ?

@hokolomopo
Copy link
Contributor

indeed, this is a great addition for my use case. although there have been instances where this variable was null/undefined for a couple of iterations ... i would just ignore it in that case-

That's ... strange. It seems like a bug if it does, can you provide a basic case where it happens so I could investigate ?

custom plugins are singleton inside a spreadsheet model

Plugins are indeed a singleton, they are initialized in the constructor of the mode and never again.

Is there a way for a plugin that manages a shared cache to recognize when a formula is "unmounted (deleted or so)"

On the top of my head I don't think it's easily possible unfortunately.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants