使用ICustomFunction功能
本文详细介绍如何使用ICustomFunction功能使用Aspose.Cells API实现自定义函数。
ICustomFunction接口允许添加自定义公式计算函数以扩展Aspose.Cells的核心计算引擎,以满足特定需求。此功能可用于在模板文件或代码中定义自定义(用户定义)函数,可使用Aspose.Cells API像任何其他默认Microsoft Excel函数一样实现和评估。
请注意,此接口已被AbstractCalculationEngine取代,并将在未来删除。关于新API的一些技术文章/示例:这里和这里
创建和评估用户定义的函数
本文演示了使用ICustomFunction接口实现自定义函数,并在电子表格中使用它获取结果。我们将按名称MyFunc定义一个自定义函数,其接受以下参数。
- 第1个参数指代单个单元格
- 第2个参数指代一系列的单元格
自定义函数将添加指定为第2个参数的单元格范围中的所有值,并将结果除以第1个参数中的值。
这里是我们如何实现CalculateCustomFunction方法的方式。
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
public class CustomFunction : ICustomFunction | |
{ | |
public object CalculateCustomFunction(string functionName, System.Collections.ArrayList paramsList, System.Collections.ArrayList contextObjects) | |
{ | |
decimal total = 0M; | |
try | |
{ | |
// Get value of first parameter | |
decimal firstParamB1 = System.Convert.ToDecimal(paramsList[0]); | |
// Get value of second parameter | |
Array secondParamC1C5 = (Array)(paramsList[1]); | |
// get every item value of second parameter | |
foreach (object[] value in secondParamC1C5) | |
{ | |
total += System.Convert.ToDecimal(value[0]); | |
} | |
total = total / firstParamB1; | |
} | |
catch | |
{ | |
} | |
// Return result of the function | |
return total; | |
} | |
} |
以下是如何在电子表格中使用新定义的函数
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
// The path to the documents directory. | |
string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); | |
// Open the workbook | |
Workbook workbook = new Workbook(); | |
// Obtaining the reference of the first worksheet | |
Worksheet worksheet = workbook.Worksheets[0]; | |
// Adding sample values to cells | |
worksheet.Cells["B1"].PutValue(5); | |
worksheet.Cells["C1"].PutValue(100); | |
worksheet.Cells["C2"].PutValue(150); | |
worksheet.Cells["C3"].PutValue(60); | |
worksheet.Cells["C4"].PutValue(32); | |
worksheet.Cells["C5"].PutValue(62); | |
// Adding custom formula to Cell A1 | |
workbook.Worksheets[0].Cells["A1"].Formula = "=MyFunc(B1,C1:C5)"; | |
// Calcualting Formulas | |
workbook.CalculateFormula(false, new CustomFunction()); | |
// Assign resultant value to Cell A1 | |
workbook.Worksheets[0].Cells["A1"].PutValue(workbook.Worksheets[0].Cells["A1"].Value); | |
// Save the file | |
workbook.Save(dataDir + "UsingICustomFunction_out.xls"); |
概述
Aspose.Cells的API在对应参数为引用或其计算结果为引用时,将ReferredArea对象放入"paramsList"中。如果您需要引用本身,可以直接使用ReferredArea。如果需要获取与公式位置对应的引用中单元格的值,可以使用ReferredArea.GetValue(rowOffset, int colOffset)方法。如果需要整个区域的单元格值数组,则可以使用ReferredArea.GetValues方法。
由于Aspose.Cells API在"paramsList"中提供了ReferredArea,因此"contextObjects"中的ReferredAreaCollection将不再需要(在旧版本中,它并不总是能够给出与自定义函数参数一一对应的映射),因此它已从"contextObjects"中移除。
public object CalculateCustomFunction(string functionName, ArrayList paramsList, ArrayList contextObjects)
{
...
object o = paramsList[i];
if(o is ReferredArea) //fetch data from reference
{
ReferredArea ra = (ReferredArea)o;
if(ra.IsArea)
{
o = ra.GetValues();
}
else
{
o = ra.GetValue(0, 0);
}
}
if (o is Array)
{
...
}
else if...
...
}