使用 ICustomFunction 返回值范围
Contents
[
Hide
]
Aspose.Cells提供自定义函数用于实现 Microsoft Excel 不支持的用户定义或自定义函数的接口作为内置函数。
大多数情况下,当您实施自定义函数接口方法,需要返回单个单元格值。但有时,您需要返回一系列值。本文将解释如何从返回值的范围自定义函数.
使用 ICustomFunction 返回值范围
下面代码实现自定义函数并通过其方法返回值的范围。请检查输出excel文件和PDF格式使用代码生成供您参考。
创建一个带有函数的类计算自定义函数.这个类实现自定义函数.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java | |
import java.util.ArrayList; | |
import com.aspose.cells.AbstractCalculationEngine; | |
import com.aspose.cells.CalculationData; | |
import com.aspose.cells.DateTime; | |
public class CustomFunctionStaticValue extends AbstractCalculationEngine { | |
@Override | |
public void calculate(CalculationData calculationData) { | |
calculationData.setCalculatedValue(new Object[][] { new Object[] { new DateTime(2015, 6, 12, 10, 6, 30), 2 }, | |
new Object[] { 3.0, "Test" } }); | |
} | |
} |
现在在你的程序中使用上面的函数。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java | |
// The path to the documents directory. | |
String dataDir = Utils.getDataDir(ReturningRangeOfValues.class); | |
Workbook wb = new Workbook(); | |
Cells cells = wb.getWorksheets().get(0).getCells(); | |
Cell cell = cells.get(0, 0); | |
cell.setArrayFormula("=MYFUNC()", 2, 2); | |
Style style = cell.getStyle(); | |
style.setNumber(14); | |
cell.setStyle(style); | |
CalculationOptions copt = new CalculationOptions(); | |
copt.setCustomFunction(new CustomFunctionStaticValue()); | |
wb.calculateFormula(copt); | |
// Save to xlsx by setting the calc mode to manual | |
wb.getSettings().getFormulaSettings().setCalculationMode(CalcModeType.MANUAL); | |
wb.save(dataDir + "output.xlsx"); | |
// Save to pdf | |
wb.save(dataDir + "output.pdf"); | |