Home » excel » excel – Formula not evaluating through Java + Apache POI

excel – Formula not evaluating through Java + Apache POI

Posted by: admin May 14, 2020 Leave a comment

Questions:

I have one excel sheet which I am reading through Apache POI, In which one cell contains the following formula.

=IFERROR(LOOKUP(2,1/(A3:T3<>"sample"),COLUMN(A3:T3)),9999)

I have tried everything which is available on the internet. Also, do the following things.

FormulaEvaluator evaluator = wb.getCreationHelper().createFormulaEvaluator();

Every time it returns me a 9999 value. Instead of actual value which is 11.

How to&Answers:

The formula

LOOKUP(2,1/(A3:T3<>"sample"),COLUMN(A3:T3))

violates the rules of the LOOKUP function. It uses the vector form with lookup_vector 1/(A3:T3<>"sample") which produces a vector having #DIV/0! if A3:T3<>"sample" is false, else 1. So lookup_vector is something like {#DIV/0!,#DIV/0!,#DIV/0!,1,#DIV/0!,#DIV/0!,...}

But the rules clearly state:

Important: The values in lookup_vector must be placed in ascending
order: …, -2, -1, 0, 1, 2, …, A-Z, FALSE, TRUE; otherwise,
LOOKUP might not return the correct value. Uppercase and lowercase text are equivalent.

There is nothing said about error values. And even if error values would be possible here, what is then the “ascending order” of those?

It is one thing that Excel itself produces results even for that wrong formula. But you cannot expect that other software also will produce results for formulas which violates the given rules.