Page Index Toggle Pages: 1 Send TopicPrint
Normal Topic How to read cell value in a custom function? (Read 660 times)
Ale
YaBB Newbies
*
Offline


I Love MindFusion!

Posts: 14
Joined: May 26th, 2023
How to read cell value in a custom function?
May 26th, 2023 at 4:06pm
Print Post  
Hello,

I'm creating some custom functions using the code (C#):

Code
Select All
public class CustomFunctions : IExpressionEvaluator {
        int a = 0;

        public object EvaluateIdentifier(IExpressionEvaluatorContext context, string identifier) {
            return null;
        }

public object InvokeFunction(IExpressionEvaluatorContext context, string name, IList parameters) {
            if(name == "FUNC_A") {

                // HERE: how to read the current cell value?

                return "Value:" + (new Random()).Next().ToString();
           }

// BLA BLA BLA .... CODE

 



As you can see in the code, this piece of program is executed everytime something is changed in the sheet (automatic recalculation).

I need, "// HERE:" (see the code) to read the value that I previously returned in the previuos calculation (see the code: "return "Value:" + (new Random()).Next().ToString();"). This values is shown in the cell (normally in the sheet we can see the cell value not the formula).
In the formula recalculation I need to read this value.

How can I achieve this in this custom defined formula?

Thank you





  
Back to top
 
IP Logged
 
Slavcho
God Member
*****
Offline


tech.support

Posts: 3153
Joined: Oct 19th, 2005
Re: How to read cell value in a custom function?
Reply #1 - May 29th, 2023 at 7:22am
Print Post  
Hi,

This build adds context.OldValue property -
https://mindfusion.eu/_beta/spreadwin_eval.zip

It will just return the formula string if never evaluated before.

Regards,
Slavcho
Mindfusion
  
Back to top
 
IP Logged
 
Ale
YaBB Newbies
*
Offline


I Love MindFusion!

Posts: 14
Joined: May 26th, 2023
Re: How to read cell value in a custom function?
Reply #2 - May 29th, 2023 at 9:01am
Print Post  
Simply amazing! Thank you!
  
Back to top
 
IP Logged
 
Ale
YaBB Newbies
*
Offline


I Love MindFusion!

Posts: 14
Joined: May 26th, 2023
Re: How to read cell value in a custom function?
Reply #3 - May 29th, 2023 at 2:11pm
Print Post  
UPDATE: I think the behaviour of "OldValue" is not correct Cry since, for example, when I insert a formula in a cell for the first time, OldValue should be NULL since the cell was initially empty, instead it contains the formula inserted! Shocked

See the attached screenshot (I used the custom formula "CAREA(2)" just for testing).
It means that the first time I insert a formula OldValue contains the formula self, instead when a recalculation is performed (because I changed something in another place of the sheet), OldValue contains the previously calculated value.

I think, in order to let OldValue have a coherent behaviour, when a new formula is inserted then OldValue should be "" or NULL (maybe NULL is more logical).

Instead if the cell already contains a formula, OldValue should ALWAYS contain the old formula result (this is not happening, in fact if I modify the formula in Old Value I get the formula I just inserted).

Thank you
  

oldvalue.jpg ( 51 KB | 32 Downloads )
oldvalue.jpg
Back to top
 
IP Logged
 
Slavcho
God Member
*****
Offline


tech.support

Posts: 3153
Joined: Oct 19th, 2005
Re: How to read cell value in a custom function?
Reply #4 - May 30th, 2023 at 7:58am
Print Post  
Hi,

New build here returns null if current formula never evaluated -

https://mindfusion.eu/_beta/spreadwin_eval.zip

Regards,
Slavcho
Mindfusion
  
Back to top
 
IP Logged
 
Ale
YaBB Newbies
*
Offline


I Love MindFusion!

Posts: 14
Joined: May 26th, 2023
Re: How to read cell value in a custom function?
Reply #5 - May 30th, 2023 at 8:21am
Print Post  
Thank you  Cheesy
I hope this modification could be useful to other people also
  
Back to top
 
IP Logged
 
Page Index Toggle Pages: 1
Send TopicPrint