数式の計算
数式の追加と結果の計算
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-.NET | |
// The path to the documents directory. | |
string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); | |
// Create directory if it is not already present. | |
bool IsExists = System.IO.Directory.Exists(dataDir); | |
if (!IsExists) | |
System.IO.Directory.CreateDirectory(dataDir); | |
// Instantiating a Workbook object | |
Workbook workbook = new Workbook(); | |
// Adding a new worksheet to the Excel object | |
int sheetIndex = workbook.Worksheets.Add(); | |
// Obtaining the reference of the newly added worksheet by passing its sheet index | |
Worksheet worksheet = workbook.Worksheets[sheetIndex]; | |
// Adding a value to "A1" cell | |
worksheet.Cells["A1"].PutValue(1); | |
// Adding a value to "A2" cell | |
worksheet.Cells["A2"].PutValue(2); | |
// Adding a value to "A3" cell | |
worksheet.Cells["A3"].PutValue(3); | |
// Adding a SUM formula to "A4" cell | |
worksheet.Cells["A4"].Formula = "=SUM(A1:A3)"; | |
// Calculating the results of formulas | |
workbook.CalculateFormula(); | |
// Get the calculated value of the cell | |
string value = worksheet.Cells["A4"].Value.ToString(); | |
// Saving the Excel file | |
workbook.Save(dataDir + "output.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-.NET | |
// The path to the documents directory. | |
string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); | |
// Create directory if it is not already present. | |
bool IsExists = System.IO.Directory.Exists(dataDir); | |
if (!IsExists) | |
System.IO.Directory.CreateDirectory(dataDir); | |
// Create a workbook | |
Workbook workbook = new Workbook(); | |
// Access first worksheet | |
Worksheet worksheet = workbook.Worksheets[0]; | |
// Put 20 in cell A1 | |
Cell cellA1 = worksheet.Cells["A1"]; | |
cellA1.PutValue(20); | |
// Put 30 in cell A2 | |
Cell cellA2 = worksheet.Cells["A2"]; | |
cellA2.PutValue(30); | |
// Calculate the Sum of A1 and A2 | |
var results = worksheet.CalculateFormula("=Sum(A1:A2)"); | |
// Print the output | |
System.Console.WriteLine("Value of A1: " + cellA1.StringValue); | |
System.Console.WriteLine("Value of A2: " + cellA2.StringValue); | |
System.Console.WriteLine("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-.NET | |
// The path to the documents directory. | |
string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); | |
// Load the template workbook | |
Workbook workbook = new Workbook(dataDir + "book1.xls"); | |
// Print the time before formula calculation | |
Console.WriteLine(DateTime.Now); | |
// Set the CreateCalcChain as tue | |
workbook.Settings.FormulaSettings.EnableCalculationChain = true; | |
// Calculate the workbook formulas | |
workbook.CalculateFormula(); | |
// Print the time after formula calculation | |
Console.WriteLine(DateTime.Now); | |
//change the value of one cell | |
workbook.Worksheets[0].Cells["A1"].PutValue("newvalue"); | |
//re-calculate those formulas which depend on cell A1 | |
workbook.CalculateFormula(); |
重要なこと
高度なトピック
- Microsoft Excelフォーミュラ計算エンジンのAspose.Cells
- Aspose.Cellsを使用してIFNA関数を計算する
- データテーブルの配列式の計算
- Excel 2016のMINIFSおよびMAXIFS関数の計算
- Cell.Calculateメソッドの計算時間を短縮する
- 循環参照の検出
- ワークシートに書き込まずにカスタム機能を直接計算する
- Aspose.Cellsのデフォルトの計算エンジンを拡張するためにカスタム計算エンジンを実装する
- ワークブックの数式計算を中断またはキャンセルする
- AbstarctCalculationEngineを使用して値の範囲を返す
- ICustomFunctionを使用して値の範囲を返す
- ブックの数式計算モードの設定
- Aspose.CellsでのFormulaText関数の使用
- ICustomFunction機能の使用