Unterbrechen oder Abbrechen der Formelberechnung des Arbeitsblatts

Mögliche Verwendungsszenarien

Aspose.Cells bietet einen Mechanismus, um die Formelberechnung der Arbeitsmappe mit der Methode AbstractCalculationMonitor.Interrupt() zu unterbrechen oder abzubrechen. Dies ist nützlich, wenn die Formelberechnung der Arbeitsmappe zu viel Zeit in Anspruch nimmt und Sie deren Verarbeitung abbrechen möchten.

Unterbrechen oder Abbrechen der Formelberechnung des Arbeitsbuchs

Der folgende Beispielcode implementiert die Methode BeforeCalculate() der Klasse AbstractCalculationMonitor. In dieser Methode findet sie den Zellnamen unter Verwendung der Zeilen- und Spaltenindexparameter. Wenn der Zellname B8 ist, unterbricht sie den Berechnungsprozess durch Aufrufen der Methode AbstractCalculationMonitor.Interrupt(). Sobald die konkrete Klasse der Klasse AbstractCalculationMonitor implementiert ist, wird ihre Instanz der Eigenschaft CalculationOptions.CalculationMonitor zugewiesen. Schließlich wird Workbook.CalculateFormula() aufgerufen, indem CalculationOptions als Parameter übergeben wird. Bitte beachten Sie die Beispiel-Excel-Datei, die innerhalb des Codes verwendet wird, sowie die Konsolenausgabe des folgenden Codes zur Referenz.

Beispielcode

// 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);
}
}

Konsolenausgabe

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

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

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