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

可能な使用シナリオ

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

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

次のサンプルコードは、AbstractCalculationMonitor クラスの BeforeCalculate() メソッドを実装しています。このメソッド内で、行および列のインデックスパラメータを使用してセルの名前を見つけます。セルの名前がB8の場合、AbstractCalculationMonitor.Interrupt() メソッドを呼び出すことで計算プロセスを中断します。一度、AbstractCalculationMonitor クラスの具象クラスが実装されると、そのインスタンスは CalculationOptions.CalculationMonitor プロパティに割り当てられます。最後に、CalculationOptions をパラメータとして渡して 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