数式を計算する方法
紹介
Aspose.Cellsには埋め込み式計算エンジンがあります。デザイナーテンプレートからインポートされた数式を再計算するだけでなく、実行時に追加された数式の結果を計算することもできます。
数式の追加と結果の計算
Aspose.Cellsは、Microsoft Excelの一部であるほとんどの数式または関数をサポートしています。これらはAPIまたはデザイナースプレッドシートを通じて使用できます。Aspose.Cellsは、数学、文字列、ブール値、日付/時刻、統計、検索、参照の大規模な数式セットをサポートしています。
Cell.SetFormulaメソッドを使用して、セルに数式を追加します。セルに数式を適用する場合は、常にMicrosoft Excelで数式を作成するときと同様に、等号(=)で文字列を始めます。関数のパラメータを区切るには、コンマ(,)を使用します。
数式の結果を計算するには、Workbook.CalculateFormula()メソッドを呼び出します。このメソッドはExcelファイルに埋め込まれたすべての数式を処理します。このコードで追加された数式とその結果を計算した出力Excelファイルを参照してください。
サンプルコード
Aspose::Cells::Startup(); | |
//For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-C | |
//Output directory path | |
U16String outPath(u"..\\Data\\Output\\"); | |
//Path of output excel file | |
U16String outputAddingFormulasAndCalculatingResults = outPath + u"outputAddingFormulasAndCalculatingResults.xlsx"; | |
//Create workbook | |
Workbook wb; | |
//Access first worksheet in the workbook | |
Worksheet ws = wb.GetWorksheets().Get(0); | |
//Adding integer values to cells A1, A2 and A3 | |
ws.GetCells().Get(u"A1").PutValue(10); | |
ws.GetCells().Get(u"A2").PutValue(20); | |
ws.GetCells().Get(u"A3").PutValue(70); | |
//Adding a SUM formula to "A4" cell | |
ws.GetCells().Get(u"A4").SetFormula(u"=SUM(A1:A3)"); | |
//Calculating the results of formulas | |
wb.CalculateFormula(); | |
//Get the calculated value of the cell | |
U16String strVal = ws.GetCells().Get(u"A4").GetStringValue(); | |
//Print the calculated value on console | |
std::cout << "Calculated Result: " << strVal.ToUtf8() <<std::endl; | |
//Saving the workbook | |
wb.Save(outputAddingFormulasAndCalculatingResults); | |
Aspose::Cells::Cleanup(); |
一度だけ数式を計算する
Workbook.CalculateFormula()がワークブックテンプレート内の数式の値を計算すると、Aspose.Cellsは計算チェーンを作成します。数式を2回目や3回目に計算する際にパフォーマンスが向上します。
ただし、テンプレートに多数の数式が含まれている場合、最初に数式を計算するとCPU処理時間とメモリを多く消費する可能性があります。
Aspose.Cellsでは、一度だけ数式を計算したいときに便利な計算チェーンの作成をオフにすることができます。
Workbook.GetISettings().SetCreateCalcChain()メソッドをfalseパラメータとともに呼び出してください。このコードをテストするためには、提供されたExcelファイル(38109186.xlsx)を使用できます。
サンプルコード
Aspose::Cells::Startup(); | |
//For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-C | |
//Source directory path | |
U16String dirPath(u"..\\Data\\Formulas\\"); | |
//Path of input excel file | |
U16String sampleCalculatingFormulasOnceOnly = dirPath + u"sampleCalculatingFormulasOnceOnly.xlsx"; | |
//Create workbook | |
Workbook wb(sampleCalculatingFormulasOnceOnly); | |
//Set the CreateCalcChain as false | |
wb.GetSettings().GetFormulaSettings().SetEnableCalculationChain(false); | |
//Get the time before formula calculationint | |
auto startTime = std::chrono::system_clock::now(); | |
//Calculate the workbook formulas | |
wb.CalculateFormula(); | |
//Get the time after formula calculation | |
auto interval = std::chrono::system_clock::now() - startTime; | |
long long time = std::chrono::duration_cast<std::chrono::milliseconds>(interval).count(); | |
std::cout << "Workbook Formula Calculation Elapsed Time in Milliseconds: " << time << std::endl; | |
Aspose::Cells::Cleanup(); |