简体   繁体   English

如何从ExcelDna返回自定义对象或双重[,]到Excel中的单元格?

[英]How to return a custom object or double[,] to a cell in Excel from ExcelDna?

Ideally my UDF would return some double results, either in a form of double[,], double[] or as a custom object. 理想情况下,我的UDF将以double [,],double []或自定义对象的形式返回一些双重结果。 I would like it to be all stored in a single cell in Excel and then use another UDF to extract them. 我希望它全部存储在Excel中的单个单元格中,然后使用另一个UDF来提取它们。 It is similar like cache the result of a calculation and display it later on demand. 它类似于缓存计算结果,并在以后按需显示。

Is it possible? 可能吗?

One approach is to have an internal 'object store' in your Excel-DNA add-in, and then have a function that returns some kind of 'handle' to Excel, together with accessor functions that take the 'handle' and access the object as required. 一种方法是在Excel-DNA插件中有一个内部“对象存储”,然后有一个函数可以返回Excel的某种“句柄”,以及带有“句柄”和访问对象的访问器函数按要求。 Cleaning up the objects you can create is a possible issue, but can be dealt with using some of the RTD features. 清理可以创建的对象是一个可能的问题,但可以使用某些RTD功能来处理。

This discussion on the Excel-DNA group has an example of implementing this idea in F# - https://groups.google.com/group/exceldna/browse_frm/thread/138bc83923701e6d . 关于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. 这当然是可能的,但是如果不知道如何使用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. 我所知道的是,您是在vba代码中包装C#方法,还是在C#中处理Excel应用程序并直接从那里写入工作簿。 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). 包装器方法的灵活性稍差,因为你必须首先将值传递给vba UDF,然后从那里将它们写入单元格,这样你就可以限制你可以返回的数据类型(就我而言)无论如何都知道)。

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. 执行此操作的方法是将结果写入特定单元格,可能是隐藏的工作表,因此不能被篡改,然后使用其他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... 如果方法返回double[,] ,您可能必须将值写入两个不同的单元格,或者在一个单元格中将文本作为带分隔符的文本,然后在检索单元格值时从文本转换为double,类似于Double.Parse(cell.value.ToString().Split(',')[0])获取第一个值(假设您将值存储为逗号分隔的字符串)或vba中的类似代码(如果您使用)一个纯vba UDF来获取值...

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. 如果您只需要存储会话持续时间的值,那么我认为您应该将它们存储在vba模块中的全局变量中。

UPDATE 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. 您将doubledouble[,]传递给UDF中的变量并将其从那里写入单元格。 Then read it back again from that cell with any other UDF. 然后使用任何其他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). 如果在同一单元格中存储两个值,则必须以文本形式存储,因此在将它们传递给C#方法之前,必须首先拆分和解析UDF中的值(或者可以在方法中进行解析) )。

In practice there should be no problem at all with what you are trying to do. 在实践中,你应该做的事情应该没有任何问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM