数式を計算する
式の追加と結果の計算
Aspose.Cells には数式計算エンジンが組み込まれています。デザイナー テンプレートからインポートされた数式を再計算できるだけでなく、実行時に追加された数式の結果を計算することもサポートします。
Aspose.Cells は、Microsoft Excel (Read計算エンジンがサポートする関数のリスト)。これらの機能は、API またはデザイナー スプレッドシートを介して使用できます。 Aspose.Cells は、数学、文字列、ブール、日付/時刻、統計、データベース、ルックアップ、および参照式の膨大なセットをサポートしています。
使用方式プロパティまたは式の設定(…) のメソッドCellセルに数式を追加するクラス。数式を適用するときは、Microsoft Excel で数式を作成するときと同じように、文字列を常に等号 (=) で開始し、カンマ (,) を使用して関数パラメーターを区切ります。
式の結果を計算するには、ユーザーは[計算式](https://reference.aspose.com/cells/java/com.aspose.cells/workbook#calculateFormula(com.aspose.cells.CalculationOptions)の方法ワークブックExcel ファイルに埋め込まれたすべての数式を処理するクラス。または、ユーザーは[計算式](https://reference.aspose.com/cells/java/com.aspose.cells/worksheet#calculateFormula(com.aspose.cells.CalculationOptions,%20boolean)の方法ワークシートシートに埋め込まれたすべての数式を処理するクラス。または、ユーザーは[計算する](https://reference.aspose.com/cells/java/com.aspose.cells/cell#calculate(com.aspose.cells.CalculationOptions)の方法Cellつの Cell の数式を処理するクラス:
// 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.getSharedDataDir(CalculatingFormulas.class) + "formulas/"; | |
// Instantiating a Workbook object | |
Workbook workbook = new Workbook(); | |
// Adding a new worksheet to the Excel object | |
int sheetIndex = workbook.getWorksheets().add(); | |
// Obtaining the reference of the newly added worksheet by passing its sheet index | |
Worksheet worksheet = workbook.getWorksheets().get(sheetIndex); | |
// Adding a value to "A1" cell | |
worksheet.getCells().get("A1").putValue(1); | |
// Adding a value to "A2" cell | |
worksheet.getCells().get("A2").putValue(2); | |
// Adding a value to "A3" cell | |
worksheet.getCells().get("A3").putValue(3); | |
// Adding a SUM formula to "A4" cell | |
worksheet.getCells().get("A4").setFormula("=SUM(A1:A3)"); | |
// Calculating the results of formulas | |
workbook.calculateFormula(); | |
// Get the calculated value of the cell | |
String value = worksheet.getCells().get("A4").getStringValue(); | |
// Saving the Excel file | |
workbook.save(dataDir + "CalculatingFormulas_out.xls"); |
知っておくべき重要事項
数式の直接計算
Aspose.Cells には数式計算エンジンが組み込まれています。 Aspose.Cells は、デザイナー ファイルからインポートされた数式を計算するだけでなく、数式の結果を直接計算できます。
数式の結果をワークシートに追加せずに直接計算する必要がある場合があります。数式で使用されているセルの値は既にワークシートに存在しており、ワークシートに数式を追加せずに、Microsoft Excel 数式に基づいてこれらの値の結果を見つけるだけで済みます。
Aspose.Cells の数式計算エンジン API を使用できます。ワークシートに[計算する](https://reference.aspose.com/cells/java/com.aspose.cells/worksheet#calculateFormula(java.lang.String,%20com.aspose.cells.CalculationOptions)ワークシートに追加せずに、そのような数式の結果:
// 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.getSharedDataDir(DirectCalculationFormula.class) + "formulas/"; | |
// Create a workbook | |
Workbook workbook = new Workbook(); | |
// Access first worksheet | |
Worksheet worksheet = workbook.getWorksheets().get(0); | |
// Put 20 in cell A1 | |
Cell cellA1 = worksheet.getCells().get("A1"); | |
cellA1.putValue(20); | |
// Put 30 in cell A2 | |
Cell cellA2 = worksheet.getCells().get("A2"); | |
cellA2.putValue(30); | |
// Calculate the Sum of A1 and A2 | |
Object results = worksheet.calculateFormula("=Sum(A1:A2)"); | |
// Print the output | |
System.out.println("Value of A1: " + cellA1.getStringValue()); | |
System.out.println("Value of A2: " + cellA2.getStringValue()); | |
System.out.println("Result of Sum(A1:A2): " + results.toString()); |
上記のコードは、次の出力を生成します。
Value of A1: 20
Value of A2: 30
Result of Sum(A1:A2): 50.0
数式を繰り返し計算する
ワークブックに多くの数式があり、ユーザーが数式のごく一部のみを変更して繰り返し計算する必要がある場合、数式計算チェーンを有効にするとパフォーマンスが向上する場合があります。FormulaSettings.EnableCalculationChain.
// 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.getSharedDataDir(CalculatingFormulasOnce.class) + "formulas/"; | |
// Load the template workbook | |
Workbook workbook = new Workbook(dataDir + "book1.xls"); | |
// Print the time before formula calculation | |
System.out.println(DateTime.getNow()); | |
// Set the CreateCalcChain as true | |
workbook.getSettings().getFormulaSettings().setEnableCalculationChain(true); | |
// Calculate the workbook formulas | |
workbook.calculateFormula(); | |
Cells cells = workbook.getWorksheets().get("Sheet1").getCells(); | |
//with original values, the calculated result | |
System.out.println(cells.get("A11").getValue()); | |
//update one value the formula depends on | |
cells.get("A5").putValue(15); | |
// Calculate the workbook formulas again, in fact only A11 needs to be and will be calculated | |
workbook.calculateFormula(); | |
//check the re-calculated value | |
System.out.println(cells.get("A11").getValue()); | |
// Print the time after formula calculation | |
System.out.println(DateTime.getNow()); |
知っておくべき重要事項
先行トピック
- Cells を Microsoft に追加 Excel 数式ウォッチ ウィンドウ
- Aspose.Cells 数式計算エンジン
- Aspose.Cells を使用した IFNA 関数の計算
- データテーブルの配列式の計算
- Excel 2016 MINIFS および MAXIFS 関数の計算
- Cell.Calculateメソッドの計算時間を短縮
- 循環参照の検出
- カスタム関数をワークシートに書かずに直接計算
- カスタム計算エンジンを実装して、Aspose.Cells のデフォルト計算エンジンを拡張します
- ワークブックの数式計算を中断またはキャンセルする
- AbstractCalculationEngine を使用して値の範囲を返す
- ICustomFunction を使用して値の範囲を返す
- ICustomFunction 機能の使用