Interface TypedUserFunction

Functional Interface:
This is a functional interface and can therefore be used as the assignment target for a lambda expression or method reference.

@FunctionalInterface public interface TypedUserFunction
A user-defined formula function that receives typed values, including ranges. Register it with Workbook.registerTypedFunction(name, fn); the engine calls it when a formula uses that name, ignoring case. For a function that takes and returns plain numbers, UserFunction is simpler.

 workbook.registerTypedFunction("CONCATALL", args -> {
     StringBuilder sb = new StringBuilder();
     for (CellValue a : args) {
         if (a instanceof CellValue.Text t) sb.append(t.value());
     }
     return new CellValue.Text(sb.toString());
 });
 

Argument shapes. Each argument arrives as the CellValue the engine evaluated for it:

  • a number, text, boolean, error, or blank operand arrives as CellValue.Number, CellValue.Text, CellValue.Bool, CellValue.Error, or CellValue.Blank respectively — including a single-cell reference, which arrives as that cell's scalar value;
  • a range (area) argument such as A1:B2 arrives as CellValue.Array: the engine evaluates an area reference to a row-major two-dimensional array of its cell values, and that shape is mirrored across the bridge as a rectangular CellValue[][] (rows of columns). Constant array literals like {1,2;3,4} arrive the same way.

Return. Return any scalar CellValue — number, text, boolean, error (an error return becomes the formula's error value, e.g. CellValue.error(7) yields #N/A), or blank. Returning a CellValue.Array produces an array result, which the engine treats like any other array-valued function result. Returning null makes the formula evaluate to #VALUE!.

Failures. If the function throws, the native bridge clears the exception and the formula evaluates to #VALUE!. The exception does not reach the code that triggered the recalculation. A formula that uses a name that is not registered evaluates to #NAME?.

A UDF must not call back into the same workbook while it runs; the engine holds the workbook during evaluation.

  • Method Summary

    Modifier and Type
    Method
    Description
    apply(CellValue[] args)
    Computes the function result.
  • Method Details

    • apply

      CellValue apply(CellValue[] args)
      Computes the function result.
      Parameters:
      args - one value per formula argument, in order; a range argument is a CellValue.Array
      Returns:
      the result; null makes the formula evaluate to #VALUE!