User defined functions

Hi,
is there a way to pass Range to UDF?

Like: <#UDF(B2:B20)>

Hi,


There is no way to pass ranges or cell addresses directly. The main reason for that is that referencing cells or ranges in a report normally doesn't work as you would expect it to.

To understand why, you have to realize 2 things:
1. For performance reasons, FlexCel doesn't recalculate every time it sets a cell value (as Excel does). It recalculates once at the end, before saving. So when running the report, the range B2:B20 will have cells which aren't recalculated. Say in B2 you have =A1 and A1 is 5. And your udf sums the cells B2 to B20. When you call the UDF B2 is not recalculated, and so B2 has #N/A, not 5. So the udf sum will fail.

2. Also for performance reasons, the order in which a FlexCel report fills the cells might change. In fact, in the future we might even do it in parallel: Filling B2 in one thread, while the other is filling B3. The thing is: You can't assume that the cell with your call <#UDF(B2:B20)> will be evaluated after or before B2 is filled. It might be that B2 is not yet filled up and it still has the value <#Customer.Price>, not the actual price of the customer. 

For those 2 reasons it is not really a good idea to reference any cell or range from a <#tag> (the only exception is when using a <#formula> tag like "<#formula>=sum(b2:b20)>" since that will be replaced by a formula that will be evaluated later).

Normally what you do is to move whatever is in B2:B20 to expressions. So for example, if B2 is <#something1><#something2><#something3> you define a <#something> expression =  <#something1><#something2><#something3> and then write <#something> in B2 and in the UDF tag.

Hi,
thank you for explanation.
That's exactly what I was trying to avoid
UDF <#something1>,<#something2>,<#something3>.....

You can use an array, so you would do UDF<#someArray>


To create the array you can either set an array tag  http://www.tmssoftware.biz/flexcel/doc/vcl/guides/reports-tag-reference.html#array or when setting a property with Report.SetValue("tag", array) you can pass an array<TCellValue> as values.

What to use really depends on your use case and how the UDF works but arrays are supported in the tags, and they will allow you to avoid to pass a list with all the values of the array.