ICustomFunction を使用して値の範囲を返す
Contents
[
Hide
]
のカスタム関数Aspose.Cells for Java 20.8 のリリース以降は非推奨です。をご利用くださいAbstractCalculationEngineクラス。の使用AbstractCalculationEngineクラスについては以下の記事で説明しています。
Aspose.Cells提供カスタム関数Microsoft Excel で組み込み関数としてサポートされていないユーザー定義関数またはカスタム関数を実装するために使用されるインターフェイス。
ほとんどの場合、カスタム関数インターフェイス メソッドでは、単一のセル値を返す必要があります。ただし、値の範囲を返す必要がある場合もあります。この記事では、値の範囲を返す方法について説明しますカスタム関数.
次のコードは実装しますカスタム関数メソッドを介して値の範囲を返します。
関数を持つクラスを作成するCalculateCustomFunction.このクラスは実装しますカスタム関数.
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-.NET | |
public class CustomFunctionStaticValue : ICustomFunction | |
{ | |
public object CalculateCustomFunction(string functionName, ArrayList paramsList, ArrayList contextObjects) | |
{ | |
return 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-.NET | |
// The path to the documents directory. | |
string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); | |
// Create workbook | |
Workbook wb = new Workbook(); | |
Cells cells = wb.Worksheets[0].Cells; | |
// Set formula | |
Cell cell = cells[0, 0]; | |
cell.SetArrayFormula("=MYFUNC()", 2, 2); | |
Style style = cell.GetStyle(); | |
style.Number = 14; | |
cell.SetStyle(style); | |
// Set calculation options for formula | |
CalculationOptions copt = new CalculationOptions(); | |
copt.CustomEngine = new CustomFunctionStaticValue(); | |
wb.CalculateFormula(copt); | |
// Save to xlsx by setting the calc mode to manual | |
wb.Settings.FormulaSettings.CalculationMode = CalcModeType.Manual; | |
wb.Save(dataDir + "output_out.xlsx"); | |
// Save to pdf | |
wb.Save(dataDir + "output_out.pdf"); |