理想情况下,我的 UDF 会以 double[,]、double[] 或自定义对象的形式返回一些双精度结果。我希望它全部存储在 Excel 中的单个单元格中,然后使用另一个 UDF 来提取它们。它类似于缓存计算结果并在以后按需显示。
可能吗?
理想情况下,我的 UDF 会以 double[,]、double[] 或自定义对象的形式返回一些双精度结果。我希望它全部存储在 Excel 中的单个单元格中,然后使用另一个 UDF 来提取它们。它类似于缓存计算结果并在以后按需显示。
可能吗?
一种方法是在您的Excel-DNA加载项中有一个内部“对象存储”,然后有一个函数将某种“句柄”返回给 Excel,以及获取“句柄”并访问该对象的访问器函数按要求。清理您可以创建的对象是一个可能的问题,但可以使用一些 RTD 功能来解决。
这个关于 Excel-DNA 组的讨论有一个在 F# 中实现这个想法的例子 - https://groups.google.com/group/exceldna/browse_frm/thread/138bc83923701e6d。
It certainly is possible, but it is difficult to give you a detailed answer without knowing how you are using ExcelDNA. What I mea by that is whether you are wrapping your C# methods in vba code, or are getting a handle on the Excel application in C# and writing to the workbook directly from there. In both cases you can do what you want, but the way to do it would be slightly different. The wrapper method would also be slightly less flexible, since you have to first pass out your values to the vba UDF and then write them to the cell from there, so you will be restricted in the data type you can return (as far as I know anyway).
The way to do this would be to write the results to a specific cell, maybe on a hidden sheet, so it can't be tampered with, and then retrieve those using another UDF. You would have to hardcode the cell address, or maybe parse the sheet to find the values you want.
In case of a method returning a double[,]
, you would probably have to write the values to two different cells, or in one cell as text with a separator and then convert from text to double when you retrieve the cell value, something like Double.Parse(cell.value.ToString().Split(',')[0])
to get the first values (assuming that you are storing the values as a comma-separated string) or similar code in vba if you use a pure vba UDF to get the values...
If you want to do this, I think you should definitely use a hidden sheet with a well-defined structure to store your values. If you only need to store the values for the duration of the session, then I think you should store them in global variables in a vba module.
UPDATE
Since you are just writing functions, I don't think you will be able to pass out a custom object (unless you implement your own converter, convert it to text and then read it back in that way).
You pass back the double
or double[,]
to a variable in your UDF and write it to a cell from there. Then read it back again from that cell with any other UDF.
The rest is the same as I wrote above. If you store two values in the same cell, you will have to do that as text, so you will have to split and parse the values first in your UDF before passing them to your C# method (or you can do the parsing in the method).
In practice there should be no problem at all with what you are trying to do.