数式を計算する方法
序章
Aspose.Cells には数式計算エンジンが組み込まれています。デザイナー テンプレートからインポートされた数式を再計算できるだけでなく、実行時に追加された数式の結果の計算もサポートします。
式の追加と結果の計算
Aspose.Cells は、Microsoft Excel の一部である数式または関数のほとんどをサポートしています。 API またはデザイナー スプレッドシートを使用して使用できます。 Aspose.Cells は、数学、文字列、ブール、日付/時刻、統計、検索、および参照式の膨大なセットをサポートしています。
セルに数式を追加するには、Cell.Formula メソッドを使用します。数式をセルに適用するときは、Microsoft Excel で数式を作成するときと同じように、文字列を常に等号 (=) で始めます。関数のパラメーターを区切るには、コンマ (,) を使用します。
数式の結果を計算するには、Excel ファイルに埋め込まれたすべての数式を処理する Workbook.CalculateFormula() メソッドを呼び出します。数式を追加してその結果を計算する次のサンプル コードを参照してください。を確認してください出力エクセルファイルこのコードで生成されます。
サンプルコード
//For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-C | |
//Output directory path | |
StringPtr outPath = new String("..\\Data\\Output\\"); | |
//Path of output excel file | |
StringPtr outputAddingFormulasAndCalculatingResults = outPath->StringAppend(new String("outputAddingFormulasAndCalculatingResults.xlsx")); | |
//Create workbook | |
intrusive_ptr<IWorkbook> wb = Factory::CreateIWorkbook(); | |
//Access first worksheet in the workbook | |
intrusive_ptr<IWorksheet> ws = wb->GetIWorksheets()->GetObjectByIndex(0); | |
//Adding integer values to cells A1, A2 and A3 | |
ws->GetICells()->GetObjectByIndex(new String("A1"))->PutValue(10); | |
ws->GetICells()->GetObjectByIndex(new String("A2"))->PutValue(20); | |
ws->GetICells()->GetObjectByIndex(new String("A3"))->PutValue(70); | |
//Adding a SUM formula to "A4" cell | |
ws->GetICells()->GetObjectByIndex(new String("A4"))->SetFormula(new String("=SUM(A1:A3)")); | |
//Calculating the results of formulas | |
wb->CalculateFormula(); | |
//Get the calculated value of the cell | |
intrusive_ptr<String> strVal = ws->GetICells()->GetObjectByIndex(new String("A4"))->GetStringValue(); | |
//Print the calculated value on console | |
StringPtr str1 = new String("Calculated Result: "); | |
Console::WriteLine(str1->StringAppend(strVal)); | |
//Saving the workbook | |
wb->Save(outputAddingFormulasAndCalculatingResults); |
数式の直接計算
数式の結果をワークシートに追加せずに直接計算する必要がある場合があります。数式で使用されているセルの値は既にワークシートに存在しており、ワークシートに数式を追加せずに、Microsoft Excel 数式に基づいてこれらの値の結果を見つけるだけで済みます。
Worksheet.CalculateFormula(String formula) メソッドを使用して、ワークシートに追加せずにそのような数式の結果を計算できます。
以下のコードは、次の出力を生成します。
Value of A1: 20
Value of A2: 30
Result of Sum(A1:A2): 50
サンプルコード
//For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-C | |
//Create workbook | |
intrusive_ptr<IWorkbook> wb = Factory::CreateIWorkbook(); | |
//Access first worksheet in the workbook | |
intrusive_ptr<IWorksheet> ws = wb->GetIWorksheets()->GetObjectByIndex(0); | |
//Put 20 in cell A1 | |
intrusive_ptr<ICell> cellA1 = ws->GetICells()->GetObjectByIndex(new String("A1")); | |
cellA1->PutValue(20); | |
//Put 30 in cell A2 | |
intrusive_ptr<ICell> cellA2 = ws->GetICells()->GetObjectByIndex(new String("A2")); | |
cellA2->PutValue(30); | |
//Calculate the Sum of A1 and A2 | |
intrusive_ptr<Aspose::Cells::System::Object> results = ws->CalculateFormula(new String("=Sum(A1:A2)")); | |
//Print the output | |
StringPtr str1 = new String("Value of A1: "); | |
Console::WriteLine(str1->StringAppend(cellA1->GetStringValue())); | |
StringPtr str2 = new String("Value of A2: "); | |
Console::WriteLine(str2->StringAppend(cellA2->GetStringValue())); | |
StringPtr str3 = new String("Result of Sum(A1:A2): "); | |
Console::WriteLine(str3->StringAppend(results->ToString())); |
数式を一度だけ計算する
ブック テンプレート内の数式の値を計算するために Workbook.CalculateFormula() が呼び出されると、Aspose.Cells によって計算チェーンが作成されます。数式が 2 回目または 3 回目に計算されるときのパフォーマンスが向上します。
ただし、テンプレートに多数の式が含まれている場合、最初に式が計算されるときに、多くの CPU 処理時間とメモリが消費される可能性があります。
Aspose.Cells を使用すると、数式を 1 回だけ計算する場合に役立つ計算チェーンの作成をオフにすることができます。
Workbook.GetISettings().SetCreateCalcChain() を false パラメータで呼び出してください。を使用できます。提供されたエクセルファイルこのコードをテストします。
サンプルコード
//For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-C | |
//Source directory path | |
StringPtr dirPath = new String("..\\Data\\Formulas\\"); | |
//Path of input excel file | |
StringPtr sampleCalculatingFormulasOnceOnly = dirPath->StringAppend(new String("sampleCalculatingFormulasOnceOnly.xlsx")); | |
//Create workbook | |
intrusive_ptr<IWorkbook> wb = Factory::CreateIWorkbook(sampleCalculatingFormulasOnceOnly); | |
//Set the CreateCalcChain as false | |
wb->GetISettings()->SetCreateCalcChain(false); | |
//Get the time in milliseconds before formula calculation | |
int before_miliseconds = Aspose::Cells::System::DateTime::GetNow()->GetMillisecond(); | |
//Calculate the workbook formulas | |
wb->CalculateFormula(); | |
//Get the time in milliseconds after formula calculation | |
int after_miliseconds = Aspose::Cells::System::DateTime::GetNow()->GetMillisecond(); | |
//Print the difference in milliseconds | |
StringPtr str1 = new String("Workbook Formula Calculation Elapsed Time in Milliseconds: "); | |
Console::WriteLine(str1->StringAppend(Int32Helper::ToString(after_miliseconds - before_miliseconds))); |