Introduction

How does it work?

Jinx is an Excel Add-In that runs Java, Scala or Kotlin code in Excel.

It has a config file where you configure the Java classpath and what classes to load. When Jinx starts up it loads those classes and looks for annotated methods, which are exposed to Excel.

For example, an Excel user defined function (UDF) to compute the nth Fibonacci number can be written in Java as follows:

import com.exceljava.jinx.ExcelFunction;

class FibonacciExample {
    /**
     * Returns the n-th Fibonacci number where:
     *   fib(0) = 1
     *   fib(1) = 1
     *   fib(n) = fib(n-1) + fib(n-2)
     */
    @ExcelFunction
    public static int fib(int n) {
        if (n < 2) {
            return 1;
        }

        int fib_n1 = 1;
        int fib_n2 = 1;
        for (int i = 2; i <= n; i++) {
            int fib_n = fib_n1 + fib_n2;
            fib_n2 = fib_n1;
            fib_n1 = fib_n;
        }
        return fib_n1;
    }
}

The @ExcelFunction annotation is detected by Jinx and the method is exposed to Excel:

fib

Excel types are automatically converted to Java types based on the signature of the method. Where there is no simple conversion (e.g. when returning an arbitrary class instance from a method) Jinx caches the returned object and returns a handle to it to Excel. When another function is called with that handle Jinx retrieves the object from its cache and passes it to the method. Jinx keeps track of cells referencing objects so that once an object is no longer referenced it can be garbage collected.