ワークブックの数式計算を中断またはキャンセルする

考えられる使用シナリオ

Aspose.Cells は、ワークブックの数式計算を中断またはキャンセルするメカニズムを提供します。AbstractCalculationMonitor.Interrupt()方法。これは、ワークブックの数式計算に時間がかかりすぎて処理をキャンセルしたい場合に便利です。

ワークブックの数式計算を中断またはキャンセルする

次のサンプル コードは、BeforeCalculate()方法AbstractCalculationMonitorクラス。このメソッド内で、行と列のインデックス パラメータを使用してセル名を見つけます。セル名が B8 の場合、関数を呼び出して計算プロセスを中断します。AbstractCalculationMonitor.Interrupt()方法。一度、具体的なクラスAbstractCalculationMonitorクラスが実装され、そのインスタンスが割り当てられますCalculationOptions.CalculationMonitor財産。ついに、Workbook.CalculateFormula()渡すことによって呼び出されます計算オプションパラメータとして。をご覧くださいサンプル Excel ファイル参照用に以下に示すコードのコンソール出力と同様に、コード内で使用されます。

サンプルコード

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET
class InterruptOrCancelFormulaCalculationOfWorkbook
{
//Implement calculation monitor class
class clsCalculationMonitor : AbstractCalculationMonitor
{
public override void BeforeCalculate(int sheetIndex, int rowIndex, int colIndex)
{
//Find the cell name
string cellName = CellsHelper.CellIndexToName(rowIndex, colIndex);
//Print the sheet, row and column index as well as cell name
System.Diagnostics.Debug.WriteLine(sheetIndex + "----" + rowIndex + "----" + colIndex + "----" + cellName);
//If cell name is B8, interrupt/cancel the formula calculation
if (cellName == "B8")
{
this.Interrupt("Interrupt/Cancel the formula calculation");
}//if
}//BeforeCalculate
}//clsCalculationMonitor
public static void Run()
{
//Load the sample Excel file
Workbook wb = new Workbook("sampleCalculationMonitor.xlsx");
//Create calculation options and assign instance of calculation monitor class
CalculationOptions opts = new CalculationOptions();
opts.CalculationMonitor = new clsCalculationMonitor();
//Calculate formula with calculation options
wb.CalculateFormula(opts);
}
}

コンソール出力

 0----1----3----D2

0----4----6----G5

0----7----1----B8