[]
        
(Showing Draft Content)

AsyncCustomFunction

Class AsyncCustomFunction

java.lang.Object
com.grapecity.documents.excel.CustomFunction
com.grapecity.documents.excel.AsyncCustomFunction

public abstract class AsyncCustomFunction extends CustomFunction
Represents the base type for an asynchronous custom function.

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();
 
  • Constructor Details

    • AsyncCustomFunction

      protected AsyncCustomFunction(String name, FunctionValueType result)
      Initializes an instance of an async custom function.
      Parameters:
      name - The name of the custom function.
      result - Specifies the return type for the custom function.
    • AsyncCustomFunction

      protected AsyncCustomFunction(String name, FunctionValueType result, Parameter[] parameters)
      Initializes an instance of an async custom function.
      Parameters:
      name - The name of the custom function.
      result - Specifies the return type for the custom function.
      parameters - Specifies the parameters for the custom function.
      Throws:
      IllegalArgumentException - if any parameter accepts a reference.
    • AsyncCustomFunction

      protected AsyncCustomFunction(String name, String description, FunctionValueType result, Parameter[] parameters)
      Initializes an instance of an async custom function.
      Parameters:
      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.
      Throws:
      IllegalArgumentException - if any parameter accepts a reference.
  • Method Details

    • evaluate

      public final Object evaluate(Object[] arguments, ICalcContext context)
      Calculates the function.

      Do not call this method directly. For AsyncCustomFunction, the result is produced by evaluateAsync(Object[],ICalcContext).

      Specified by:
      evaluate in class CustomFunction
      Parameters:
      arguments - the value collection of the arguments
      context - the context of the calculation.
      Returns:
      Always returns null.
    • evaluateAsync

      protected abstract CompletableFuture<Object> evaluateAsync(Object[] arguments, ICalcContext context)
      Calculates the function asynchronously.
      
       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();
       
      Parameters:
      arguments - The value collection of the arguments.
      context - The context of the calculation.
      Returns:
      The result of the function.