Non-Static Methods
Excel functions can be non-static Java methods so long as the class has a public default constructor or a public constructor that takes a single parameter of type ExcelAddIn.
The ExcelAddIn interface can be used to call back into Excel and the Jinx add-in and has, among others, the following methods:
Method | Description |
String getAddInPath() |
Returns the path to the jinx.xll file being used. |
ClassLoader getClassLoader() |
Returns the ClassLoader used to load the classes. |
List<String> getConfigSections() |
Returns a List of config sections parsed from the config file. |
List<String> getConfigSectionKeys(String section) |
Returns a list of keys for a config section. |
boolean hasConfigValue(String section, String key) |
Returns true if a config setting exists. |
String getConfigValue(String section, String key) |
Returns an item from the config. |
<T> convertArgument(Object arg, Class<T> cls) |
Converts an Object passed to an Excel function to a Java type. |
long getExcelApplication() |
Retrieves the COM Excel.Application instance for the current Excel process. |
ExcelReference getCaller() |
Get the calling cell as an ExcelReference instance. |
Future schedule(Runnable task) |
Schedule a callback on the main thread to be run as a macro. |
void log(LogRecord logRecord) |
Logs to the xll log file. |
Full documentation for the ExcelAddIn interface can be found in the javadocs.
The convertArgument method is particularly useful for functions that take a variety of different types or an array of Objects.
Below is a contrived example, but it illustrates a non-static method using a public constructor taking an ExcelAddIn.
import com.exceljava.jinx.ExcelFunction;
import com.exceljava.jinx.ExcelAddIn;
class CarUtils {
private final ExcelAddIn xl;
public CarUtils(ExcelAddIn xl) {
this.xl = xl;
}
@ExcelFunction
public String carToString(Object obj) {
Car car;
try {
car = xl.convertArgument(obj, Car.class);
} catch (Exception e) {
throw new IllegalArgumentException("Expected a car", e);
}
return car.toString();
}
}