Excel is one of the most used tools so its natural that scripting and Excel files would nicely go together. In this article I would like to outline a few options and provide an easy to use library for medini import scripts.
Option 1: use external libraries as Apache POI
As described in a separate article in the "Solution" titled "Using additional libraries", it is possible to load and use arbitrary Java libraries. That way it would be also possible to use the libraries of the Apache POI project which supports almost any Office file format out there since years, that includes XLS as well as XLSX. However, the POI project came a long way and the API is somewhat awkward to some extend and the developer has to get used to it. Furthermore that approach forces the script developer to mainly write Java code in Java Script which is not everyone's favorite.
Option 2: use the medini lightweight Excel API
Well, now the good news: medini has its own much simpler Excel API. That API has just a few classes as ExcelDocument, Worksheet, Row, Cell and a few more. Still the API is a Java API and just disclosing it 1:1 to Java Script would technically work but is maybe not the ideal way to do it. For that reason I would like to offer a third way.
Option 3: use a simple Java Script wrapper library
Similar to the articles about Factory, Trashbin and ASIL Comparison, we have developed a slim but powerful Excel library that (1) reduces the overhead to start a simple Excel import to just a few lines but (2) still offers a flexible API to cover most use cases, at least for Excel import and (3) sits atop the Java Excel API mentioned in option 2. The library comes as a single JS file named "excel.js" and can be used to write scripts that require user interaction or scripts that do not. In the first case a second library named "ui.js" is required which offers a few methods to interact with the user.
Lets directly dive into code and have a look at the following simple script:
// the callback object which we pass to the Excel API
var callback = {
/*
* Called by the Excel for each processed row. The function may do
* arbitrary stuff here. This function is MANDATORY.
*/
handleRow : function(index, row) {
console.log("Handle row {0}...", index);
}
};
var importer = new ExcelImporter(callback);
if (!importer.run()) {
console.log("Import was canceled");
}
As you can see, the script defines a mandatory "callback" object. Depending on the use case, this callback object can define a set of functions to drive the excel import. The minimum is a function that is named "handleRow" which is called for each row in an Excel worksheet. Starting this script will result in
- a dialog to ask the user for an input Excel
- a dialog to ask the user for a sheet to pick (if there are multiple)
- calling the "handleRow" function for each row found
As you see, the importer is limited to a single worksheet but for most use cases that should be sufficient.
There are now many more optional callback functions that can be provided to change the importer behavior which I do not explain in text but in two sample scripts that I will attach later. Just to mention the two groups of callbacks:
- callbacks that drive the selection of file, worksheet and rows
- callbacks that are called before or after certain steps in the import process, e.g. after file was opened, after worksheet was opened etc.
I have added documentation into the sample scripts attached to this article to explain everything. Hope that scripts are useful and I am happy to get your feedback.
There will be the following files attached:
- excel.js - the ExcelImporter API
- ui.js - required if importer is run in interactive mode (default)
- Test Excel Headless - sample importer showing first group of API calls
- Test Excel.js - sample importer showing second group of API calls
Happy Scripting
Jan