ExcelDNA 参数和 Return 类型自动转换
ExcelDNA Parameter and Return Type Auto-conversion
我想自动尝试将输入参数从 Excel 友好类型转换为在我的插件中有用的类型,反之亦然使用 [=34= 将输入参数转换回 Excel ] 值。例如,我想定义一个 Excel 函数(作为 C# 方法),例如:
public static Vector<double> MyFunction(Vector<double> inputVector)
{
// do some stuff to inputVector
return inputVector
}
我想让它转换我的输入参数和 return 值 'behind the scenes',即我定义了一些从 object
转换为 Vector<double>
的通用转换方法反之亦然,这是在我定义的方法 in/out 传递之前调用的。
这可能吗?我在 github repo 中找到了 ParameterConversionConfiguration
,但我不太确定如何使用它。是否有可用的示例或进一步的文档?我知道我可能需要以某种方式注册我的类型转换,但我不确定如何继续。
编辑:经过更多尝试后,我这样做是为了将 return 值从矩阵转换为数组:
public class ExcellAddIn : IExcelAddIn
{
public void AutoOpen()
{
var conversionConfig = GetParameterConversionConfig();
}
static ParameterConversionConfiguration GetParameterConversionConfig()
{
var paramConversionConfig = new ParameterConversionConfiguration()
.AddReturnConversion((Matrix<double> value) => value.ToArray());
return paramConversionConfig;
}
}
但是在加载 .xll 时,Excel 出现 'unsupported signature' 错误。我在正确的轨道上吗?我还需要做什么?
这里有一个使用这些 Excel-DNA 注册扩展的完整示例插件:https://github.com/Excel-DNA/Registration/tree/master/Source/Samples/Registration.Sample
与您的问题相关的一些细节:
您实际上需要获取函数注册,应用您的转换并在您的 AutoOpen
:
中执行注册
public void AutoOpen()
{
var conversionConfig = GetParameterConversionConfig();
ExcelRegistration.GetExcelFunctions()
.ProcessParameterConversions(conversionConfig)
.RegisterFunctions();
}
您可能希望通过在 .dna 文件中添加 ExplicitRegistration='true'
属性来抑制默认处理:
<DnaLibrary Name="My Add-In" RuntimeVersion="v4.0" >
<ExternalLibrary Path="XXX.dll" ExplicitRegistration="true" .... />
</DnaLibrary>
我想自动尝试将输入参数从 Excel 友好类型转换为在我的插件中有用的类型,反之亦然使用 [=34= 将输入参数转换回 Excel ] 值。例如,我想定义一个 Excel 函数(作为 C# 方法),例如:
public static Vector<double> MyFunction(Vector<double> inputVector)
{
// do some stuff to inputVector
return inputVector
}
我想让它转换我的输入参数和 return 值 'behind the scenes',即我定义了一些从 object
转换为 Vector<double>
的通用转换方法反之亦然,这是在我定义的方法 in/out 传递之前调用的。
这可能吗?我在 github repo 中找到了 ParameterConversionConfiguration
,但我不太确定如何使用它。是否有可用的示例或进一步的文档?我知道我可能需要以某种方式注册我的类型转换,但我不确定如何继续。
编辑:经过更多尝试后,我这样做是为了将 return 值从矩阵转换为数组:
public class ExcellAddIn : IExcelAddIn
{
public void AutoOpen()
{
var conversionConfig = GetParameterConversionConfig();
}
static ParameterConversionConfiguration GetParameterConversionConfig()
{
var paramConversionConfig = new ParameterConversionConfiguration()
.AddReturnConversion((Matrix<double> value) => value.ToArray());
return paramConversionConfig;
}
}
但是在加载 .xll 时,Excel 出现 'unsupported signature' 错误。我在正确的轨道上吗?我还需要做什么?
这里有一个使用这些 Excel-DNA 注册扩展的完整示例插件:https://github.com/Excel-DNA/Registration/tree/master/Source/Samples/Registration.Sample
与您的问题相关的一些细节:
您实际上需要获取函数注册,应用您的转换并在您的
中执行注册AutoOpen
:public void AutoOpen() { var conversionConfig = GetParameterConversionConfig(); ExcelRegistration.GetExcelFunctions() .ProcessParameterConversions(conversionConfig) .RegisterFunctions(); }
您可能希望通过在 .dna 文件中添加
ExplicitRegistration='true'
属性来抑制默认处理:<DnaLibrary Name="My Add-In" RuntimeVersion="v4.0" > <ExternalLibrary Path="XXX.dll" ExplicitRegistration="true" .... /> </DnaLibrary>