// Create a new workbook Workbook workbook = new Workbook(); IWorksheet worksheet = workbook.getActiveSheet(); // public class FetchData { // public volatile int FetchPercentage = 0; // } // public class FetchService { // public static FetchData FetchData = new FetchData(); // } // public class FetchPercentageFunction extends CustomFunction { // // public FetchPercentageFunction() { // super("FetchPercentage", FunctionValueType.Text, new Parameter[]{}); // } // // @Override // public Object evaluate(Object[] objects, ICalcContext iCalcContext) { // int fetchPercentage = FetchService.FetchData.FetchPercentage; // return fetchPercentage; // } // } // public class FetchDataFunction extends AsyncCustomFunction { // public FetchDataFunction() { // super("FetchData", FunctionValueType.Text, new Parameter[]{}); // } // // @Override // public CompletableFuture evaluateAsync(Object[] arguments, ICalcContext context) { // return CompletableFuture.supplyAsync(() -> { // FetchData file = FetchService.FetchData; // for (int i = 0; i < 100; i++) { // // Simulate fetching data, fetch 1% every 0.1 second. // try { // Thread.sleep(100); // } catch (InterruptedException e) { // throw new RuntimeException(e); // } // file.FetchPercentage++; // } // return "Fetch complete"; // }); // } // } Workbook.AddCustomFunction(new FetchDataFunction()); Workbook.AddCustomFunction(new FetchPercentageFunction()); worksheet.getRange("A1").setFormula("=FetchData()"); worksheet.getRange("B1").setFormula("=FetchPercentage()"); workbook.calculate(); while (worksheet.getRange("A1").getValue() instanceof CalcError) { // Retrieve the current progress every 1 second. try { Thread.sleep(1000); } catch (InterruptedException e) { throw new RuntimeException(e); } // Mark cell B1 as Dirty to trigger a recalculation of the B1 cell. worksheet.getRange("B1").dirty(); Object FetchState = worksheet.getRange("A1").getValue(); Object FetchPercentage = worksheet.getRange("B1").getValue(); if (FetchState instanceof CalcError) { FetchState = "Fetching"; } else { // If the FetchState is not CalcError.Busy, then update the FetchPercentage. worksheet.getRange("B1").dirty(); FetchPercentage = worksheet.getRange("B1").getValue(); } System.out.println(FetchState + "\t" + FetchPercentage + "%"); }