Avbryt eller avbryt formelberäkningen i arbetsbok
Möjliga användningsscenario
Aspose.Cells tillhandahåller en mekanism för att avbryta eller avbryta formelberäkningen av arbetsboken med hjälp av AbstractCalculationMonitor.Interrupt()-metoden. Detta är användbart när arbetsbokens formelberäkning tar för lång tid och du vill avbryta dess bearbetning.
Avbryt eller avbryt formelberäkningen i arbetsbok
Följande exempelkod implementerar BeforeCalculate()-metoden i AbstractCalculationMonitor-klassen. Inne i denna metod hittar den cellnamnet med hjälp av rad- och kolumnindexparametrarna. Om cellnamnet är B8, avbryter den beräkningsprocessen genom att anropa AbstractCalculationMonitor.Interrupt()-metoden. När AbstractCalculationMonitor-klassens konkreta klass väl har implementerats, tilldelas dess instans till CalculationOptions.CalculationMonitor-egenskapen. Slutligen anropas Workbook.CalculateFormula() genom att skicka CalculationOptions som en parameter. Se även den exempel på excelfil som används i koden samt konsolresultatet av koden nedan för en referens.
Exempelkod
// 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); | |
} | |
} |
Konsoloutput
0----1----3----D2
0----4----6----G5
0----7----1----B8