[]
Extend this class and override evaluateAsync(Object[],ICalcContext) to provide the calculation logic. After the function is registered, formulas can call the function and return the result.
Parameters cannot accept cell or range references. Use calculated values as arguments instead, and do not access workbook model objects inside the asynchronous logic because it may run on another thread.
class AsyncAddFunction extends AsyncCustomFunction {
public AsyncAddFunction() {
super("ASYNCADD", "Adds two numbers asynchronously.", FunctionValueType.Number,
new Parameter[] {
new Parameter(FunctionValueType.Number),
new Parameter(FunctionValueType.Number)
});
}
protected CompletableFuture<Object> evaluateAsync(Object[] arguments, ICalcContext context) {
double left = ((Number) arguments[0]).doubleValue();
double right = ((Number) arguments[1]).doubleValue();
return CompletableFuture.completedFuture(left + right);
}
}
AsyncCustomFunction function = new AsyncAddFunction();
Workbook.AddCustomFunction(function);
worksheet.getRange("A1").setFormula("=ASYNCADD(10,20)");
Object sum = worksheet.getRange("A1").getValue();
nameprotected AsyncCustomFunction(String name,
FunctionValueType result) protected AsyncCustomFunction(String name,
FunctionValueType result,
Parameter[] parameters) protected AsyncCustomFunction(String name,
String description,
FunctionValueType result,
Parameter[] parameters) final Objectevaluate(Object[] arguments,
ICalcContext context) protected abstract CompletableFuture<Object>evaluateAsync(Object[] arguments,
ICalcContext context) getAcceptErrors, getIsVolatile, setAcceptErrors, setIsVolatilename - The name of the custom function.result - Specifies the return type for the custom function.name - The name of the custom function.result - Specifies the return type for the custom function.parameters - Specifies the parameters for the custom function.IllegalArgumentException - if any parameter accepts a reference.name - The name of the custom function. Must not be null or empty.description - The description of the custom function. Can be null.result - Specifies the return type for the custom function.parameters - Specifies the parameters for the custom function; can be null.IllegalArgumentException - if any parameter accepts a reference.Do not call this method directly. For AsyncCustomFunction, the result is produced by evaluateAsync(Object[],ICalcContext).
evaluate in class CustomFunctionarguments - the value collection of the argumentscontext - the context of the calculation.null.
Workbook workbook = new Workbook();
IWorksheet worksheet = workbook.getActiveSheet();
AsyncCustomFunction function = new AsyncCustomFunction(
"ASYNCADD_ASYNC",
"Adds two numbers asynchronously.",
FunctionValueType.Number,
new Parameter[] {
new Parameter(FunctionValueType.Number),
new Parameter(FunctionValueType.Number)
}) {
protected CompletableFuture<Object> evaluateAsync(Object[] arguments, ICalcContext context) {
return CompletableFuture.supplyAsync(() -> {
try {
Thread.sleep(50);
} catch (InterruptedException exception) {
throw new RuntimeException(exception);
}
double left = ((Number) arguments[0]).doubleValue();
double right = ((Number) arguments[1]).doubleValue();
return left + right;
});
}
};
Workbook.AddCustomFunction(function);
worksheet.getRange("A1").setFormula("=ASYNCADD_ASYNC(10,20)");
Object initialValue = worksheet.getRange("A1").getValue();
workbook.calculate();
workbook.waitForCalculationToFinish();
Object finalValue = worksheet.getRange("A1").getValue();
arguments - The value collection of the arguments.context - The context of the calculation.