数式の計算

数式の追加と結果の計算

Aspose.Cellsには埋め込み数式計算エンジンがあります。デザイナーテンプレートからインポートされた数式を再計算するだけでなく、実行時に追加された数式の結果を計算することもサポートしています。

Aspose.Cellsは、Microsoft Excelの一部である多くの関数や式をサポートしています(計算エンジンでサポートされている関数のリスト)。これらの関数はAPIまたはデザイナースプレッドシートを通じて使用できます。Aspose.Cellsは、数学、文字列、ブール、日付/時刻、統計、データベース、検索、参照関数などの大規模なセットの式をサポートしています。

FormulaプロパティまたはSetFormula(…)メソッドのCellクラスを使用して、セルに数式を追加します。数式を適用するときには常に等号(=)で始め、Microsoft Excelで数式を作成するときと同様に関数パラメータを区切るためにコンマ(,)を使用します。

数式の結果を計算するには、ユーザーはExcelファイルに埋め込まれたすべての数式を処理するCalculateFormulaメソッドを呼び出すか、シートに埋め込まれたすべての数式を処理するWorkbookメソッドを呼び出すか、またはセルの数式を処理するCalculateFormulaメソッドを呼び出すかを選択できます。

// 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には、埋め込みファイルからインポートされた数式を計算するだけでなく、直接数式の結果を計算する機能があります。

時々、ワークシートに追加することなく、Microsoft Excelの数式に基づいてワークシート内にすでに存在するセルの値の結果を見つける必要があります。

Aspose.Cellsの数式計算エンジンAPIを使用して、ワークシートに追加せずにそのような数式の結果をWorksheetからcalculateまで計算できます。

// 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());

重要なこと

高度なトピック