Excel API Library for Java - Sample Browser | Document Solutions | Async Calculation
[
フレーム]
src="bundle.js">
Refer to the example code below for adding and utilizing custom Async formulas.
// 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 + "%"); }
// Create a new workbook
var workbook = Workbook()
val worksheet = workbook.activeSheet
// class FetchData {
// @Volatile
// var FetchPercentage = 0
// }
// object FetchService {
// var FetchData: features.formulas.FetchData = features.formulas.FetchData()
// }
// class FetchPercentageFunction : CustomFunction("FetchPercentage", FunctionValueType.Text, arrayOf
()) {
// override fun evaluate(objects: Array, iCalcContext: ICalcContext): Any {
// return features.formulas.FetchService.FetchData.FetchPercentage
// }
// }
// class FetchDataFunction : AsyncCustomFunction("FetchData", FunctionValueType.Text, arrayOf()) {
// override fun evaluateAsync(arguments: Array, context: ICalcContext): CompletableFuture {
// return CompletableFuture.supplyAsync {
// val file: features.formulas.FetchData? = features.formulas.FetchService.FetchData
// for (i in 0..99) {
// // Simulate fetching data, fetch 1% every 0.1 second.
// try {
// Thread.sleep(100)
// } catch (e: InterruptedException) {
// throw RuntimeException(e)
// }
// file!!.FetchPercentage++
// }
// "Fetch complete"
// }
// }
// }
Workbook.AddCustomFunction(FetchDataFunction())
Workbook.AddCustomFunction(FetchPercentageFunction())
worksheet.getRange("A1").formula = "=FetchData()"
worksheet.getRange("B1").formula = "=FetchPercentage()"
workbook.calculate()
while (worksheet.getRange("A1").value is CalcError) {
// Retrieve the current progress every 1 second.
try {
Thread.sleep(1000)
} catch (e: InterruptedException) {
throw RuntimeException(e)
}
// Mark cell B1 as Dirty to trigger a recalculation of the B1 cell.
worksheet.getRange("B1").dirty()
var FetchState = worksheet.getRange("A1").value
var FetchPercentage = worksheet.getRange("B1").value
if (FetchState is CalcError) {
FetchState = "Fetching"
} else {
// If the FetchState is not CalcError.Busy, then update the FetchPercentage.
worksheet.getRange("B1").dirty()
FetchPercentage = worksheet.getRange("B1").value
}
println("$FetchState\t$FetchPercentage%")
}