Cell.Calculateメソッドの計算時間を短縮

考えられる使用シナリオ

通常、ユーザーは電話することをお勧めしますWorkbook.CalculateFormula()メソッドを 1 回実行してから、個々のセルの計算値を取得します。しかし、ユーザーがワークブック全体を計算したくない場合があります。単一のセルを計算したいだけです。 Aspose.Cells提供CalculationOptions.Recursive設定できるプロパティ間違い個々のセルの計算時間を大幅に短縮します。 recursive プロパティが真実、その後、セルのすべての従属が各呼び出しで再計算されます。しかし、再帰プロパティが間違いの場合、依存セルは 1 回だけ計算され、その後の呼び出しでは再度計算されません。

Cell.Calculate()メソッドの計算時間を短縮

次のサンプル コードは、CalculationOptions.Recursive財産。指定されたコードでこのコードを実行してくださいサンプルエクセルファイルコンソール出力を確認します。再帰プロパティを間違い計算時間を大幅に短縮しました。このプロパティをよりよく理解するために、コメントもお読みください。

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET
// Test calculation time after setting recursive true
TestCalcTimeRecursive(true);
// Test calculation time after setting recursive false
TestCalcTimeRecursive(false);
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET
static void TestCalcTimeRecursive(bool rec)
{
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
// Load your sample workbook
Workbook wb = new Workbook(dataDir + "sample.xlsx");
// Access first worksheet
Worksheet ws = wb.Worksheets[0];
// Set the calculation option, set recursive true or false as per parameter
CalculationOptions opts = new CalculationOptions();
opts.Recursive = rec;
// Start stop watch
Stopwatch sw = new Stopwatch();
sw.Start();
// Calculate cell A1 one million times
for (int i = 0; i < 1000000; i++)
{
ws.Cells["A1"].Calculate(opts);
}
// Stop the watch
sw.Stop();
// Calculate elapsed time in seconds
long second = 1000;
long estimatedTime = sw.ElapsedMilliseconds / second;
// Print the elapsed time in seconds
Console.WriteLine("Recursive " + rec + ": " + estimatedTime + " seconds");
}

コンソール出力

これは、指定されたコマンドで実行したときの上記のサンプル コードのコンソール出力です。サンプルエクセルファイル私たちのマシンで。出力は異なる場合がありますが、再帰プロパティをに設定した後の経過時間に注意してください間違いに設定するよりも常に少なくなります。真実.

Recursive True: 96 seconds

Recursive False: 42 seconds