Interrupt or Cancel the Formula Calculation of Workbook
Possible Usage Scenarios
Aspose.Cells provides a mechanism to interrupt or cancel the formula calculation of the workbook using the interrupt() method of the AbstractCalculationMonitor class. This is useful when the formula calculation of workbook is taking too much time and you want to cancel its processing.
Interrupt or Cancel the Formula Calculation of Workbook
The following sample code implements the beforeCalculate() method of the AbstractCalculationMonitor class. Inside this method, it finds the cell name using row and column index parameters. If the cell name is B8, it interrupts the calculation process by calling the AbstractCalculationMonitor.interrupt() method. Once, the concrete class of AbstractCalculationMonitor class is implemented, its instance is assigned to CalculationOptions.CalculationMonitor property. Finally, Workbook.calculateFormula() is called by passing CalculationOptions as a parameter. Please see the sample Excel file used inside the code as well as the console output of the code given below for a reference.
Sample Code
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java | |
//Implement calculation monitor class | |
class clsCalculationMonitor extends AbstractCalculationMonitor | |
{ | |
public 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.out.println(sheetIndex + "----" + rowIndex + "----" + colIndex + "----" + cellName); | |
//If cell name is B8, interrupt/cancel the formula calculation | |
if (cellName.equals("B8") == true) | |
{ | |
this.interrupt("Interrupt/Cancel the formula calculation"); | |
}//if | |
}//beforeCalculate | |
}//clsCalculationMonitor | |
//--------------------------------------------------------- | |
//--------------------------------------------------------- | |
public void Run() throws Exception | |
{ | |
//Load the sample Excel file | |
Workbook wb = new Workbook(srcDir + "sampleCalculationMonitor.xlsx"); | |
//Create calculation options and assign instance of calculation monitor class | |
CalculationOptions opts = new CalculationOptions(); | |
opts.setCalculationMonitor(new clsCalculationMonitor()); | |
//Calculate formula with calculation options | |
wb.calculateFormula(opts); | |
} |
Console Output
0----1----3----D2
0----4----6----G5
0----7----1----B8