Skip to content
Advertisement

APACHE POI set formula by column name format

Have small problem when I want to set formula to cell by column name format. Let me show example:

In excel file I can do something like this =[Name]

enter image description here

So value from colmn B is copied to column A

But when I try to do something like this in Apache POI

JavaScript

I get exception:

Parse error near char 0 ‘[‘ in specified formula ‘[Name]’. Expected number, string, defined name, or data table”

How can I handle such situation?

Advertisement

Answer

The formula =[Name] is a structured reference to an Excel table. But it is a very short unqualified form. The fully qualified form would be =tableName[[#This Row],[Name]], And that fully qualified form also will be stored and so must be set using apache poi.

Let’s have a complete example:

JavaScript

This works using apache poi 4 or later.

Note, the additional formulaCell.getCTCell().getF().setStringValue(tableName + "[[#This Row],[Name]]"); is only needed using apache poi versions after 5.1.0. Later versions of apache poi uses a formula parser which damages the structured table formula and so it must be reset using this code line.

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement