Recently there have been a few forum posts that got me looking at the version of the java Apache POI which ships in KNIME AP 4.7, and as a result set myself the challenge of writing a component that could insert an Excel formula into a cell in an existing spreadsheet.
Well, having had a play, and then finding it could do much more besides, I have the following workflow as a proof of concept. I won’t go into the full details here, but instead I’ll just post the workflow, plus some screenshots which if you use KNIME 4.7 and you are interested in writing ever-more fancy spreadsheets directly out of KNIME, I hope will make you as excited as it made me
OK, this is the demo workflow, with a couple of components that currently reside in my private area on the hub, so they currently won’t update, but hopefully will still work for you here. As it is still work in progress, I don’t want to put the components publicly on the HUB just yet!
The sample data is a data table:
Note that the value column has been intentionally left blank. This data is written to Excel, and is pretty boring:
Moving on…
The lower table creator looks like this:
And this is going to ultimately tell the “Write Excel Formula” component to do some stuff. But it is written using a syntax (for want of a better term) that is going to be interpreted by an intermediate component called the “Excel Cell Formula Expander”, because this needs to be generic, and we don’t know how many rows of data and such like our data is going to have.
The Excel Cell Formula Expander takes the lower control table, and works out, based on the data table that was written to Excel, what the actual Excel cell references will be for all the things we want to do, and it generates this table that will be passed to the Write Excel Cell Formula"
Hopefully you can get an idea of what this all might mean but as a quick explanation.
It says "Into cell D2 write the formula B2*C2, and format it using the Excel format mask 0,000.00
Do the same for cells D3 to D5
Into Cell D6 write the words “TOTAL SALES”, make it right justified with a font size of 20, and yellow writing on black background.
Make the headings A1:D1 black on yellow
Make the cells A2 to A5 (the data rows for product) Green
Put the formula SUM(D2:D5) into cell D7 and make that 14pt, right justified, with the format mask 0,000 and red on black.
Into Cell A9 write the label “–=== end of report ===–”
oh, and in a couple of places it sets a column width too.
In summary, yes it is going to look a right mess! but it will demonstrate a point, … if it works…
Maybe it will look like this!
Revealing the cell formulas, you can see that it really has written the formulas to the cells. No data calculations have been performed in KNIME:
Proof of concept - Excel Cell Formula writing -takbb.knwf (188.2 KB)
I would be interested in hearing your views, and of course further suggestions. As this is built using components (with a heavy reliance on java snippets!) rather than bespoke nodes, there are limitations on what can be achieved, but I think this has potential for being useful.
I have a technical question though. The components use Java snippet/Java Edit Variable to call the specific Apache POI packages that are bundled in KNIME 4.7. What will happen with future KNIME AP releases? If a different version of the Apache POI packages are bundled, will this component cease to work, or will it continue to have access to the jar files that were available when it was created?
Well hopefully a few of you might give this a try… let me know what you think.
Is this going to change your life?
Maybe this might give some ideas for having similar functionality baked into KNIME.
btw, I’d like to thank my best virtual friend chatGPT for massively accelerating the creation of the java code to make this work.