Bug 48526 - Not Yet Implemented Error for RANDBETWEEN()
Summary: Not Yet Implemented Error for RANDBETWEEN()
Status: RESOLVED FIXED
Alias: None
Product: POI
Classification: Unclassified
Component: XSSF (show other bugs)
Version: 3.5-FINAL
Hardware: PC Windows XP
: P1 major (vote)
Target Milestone: ---
Assignee: POI Developers List
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2010-01-11 20:47 UTC by Jaibabu
Modified: 2010-08-12 10:27 UTC (History)
0 users



Attachments
Adds support for RANDBETWEEN() function. The zip file contains a svn diff and a tar file which contains an Excel spreadsheet for testing (6.45 KB, patch)
2010-08-10 18:36 UTC, Brendan Nolan
Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Jaibabu 2010-01-11 20:47:57 UTC
Hai Experts,

        While i generating Excel report using POI 3.5-FINAL API it throws an error "Not Yet Implemented" for the formula RANDBETWEEN(-20,27)*0.1 i used in the
Excel template. Whether this has been inplemented in the further release or please make a note on how to do this with this API version.

Thanks
jai
Comment 1 Jaibabu 2010-01-24 20:01:30 UTC
Hai Experts,

   Is there any release with this function fix "RANDBETWEEN()".

Thanks
Jaibabu.J
Comment 2 Josh Micich 2010-01-24 23:17:05 UTC
I guess RANDBETWEEN is not a very popular function.  If you are able to write an implementation yourself and submit a patch, it is likely to get added more quickly.

Similar functions already exist in NumericFunction.java:
http://svn.apache.org/viewvc/poi/trunk/src/java/org/apache/poi/hssf/record/formula/functions/NumericFunction.java?annotate=893403


Contributions are always welcome.
Comment 3 Brendan Nolan 2010-08-10 18:36:17 UTC
Created attachment 25876 [details]
Adds support for RANDBETWEEN() function. The zip file contains a svn diff and a tar file which contains an Excel spreadsheet for testing

RANDBETWEEN is one of the Analysis Toolpak formula and I have implemented it as such rather than an entry in NumericFunctions

One strange behavior I noticed while formulating Testcases for the function was that it can't be assigned into a workbook created directly in POI. The same is true for all the currently implemented ATP functions ISEVEN(), ISODD() and YEARFRAC()

If you run the following

Workbook wb = new HSSFWorkbook();	
Sheet sheet = wb.createSheet("ATPSheet");
Row row = sheet.createRow(0);
Cell formulaCell = row.createCell(0, HSSFCell.CELL_TYPE_FORMULA);
formulaCell.setCellFormula("ISEVEN()");

You will get the following exception:

Exception in thread "main" org.apache.poi.ss.formula.FormulaParseException: Name 'ISEVEN' is completely unknown in the current workbook
	at org.apache.poi.ss.formula.FormulaParser.function(FormulaParser.java:914)
	at org.apache.poi.ss.formula.FormulaParser.parseNonRange(FormulaParser.java:552)
	at org.apache.poi.ss.formula.FormulaParser.parseRangeable(FormulaParser.java:423)
	at org.apache.poi.ss.formula.FormulaParser.parseRangeExpression(FormulaParser.java:266)
	at org.apache.poi.ss.formula.FormulaParser.parseSimpleFactor(FormulaParser.java:1113)
	at org.apache.poi.ss.formula.FormulaParser.percentFactor(FormulaParser.java:1073)
	at org.apache.poi.ss.formula.FormulaParser.powerFactor(FormulaParser.java:1060)
	at org.apache.poi.ss.formula.FormulaParser.Term(FormulaParser.java:1420)
	at org.apache.poi.ss.formula.FormulaParser.additiveExpression(FormulaParser.java:1520)
	at org.apache.poi.ss.formula.FormulaParser.concatExpression(FormulaParser.java:1504)
	at org.apache.poi.ss.formula.FormulaParser.comparisonExpression(FormulaParser.java:1461)
	at org.apache.poi.ss.formula.FormulaParser.unionExpression(FormulaParser.java:1441)
	at org.apache.poi.ss.formula.FormulaParser.parse(FormulaParser.java:1562)
	at org.apache.poi.ss.formula.FormulaParser.parse(FormulaParser.java:174)
	at org.apache.poi.hssf.model.HSSFFormulaParser.parse(HSSFFormulaParser.java:72)
	at org.apache.poi.hssf.usermodel.HSSFCell.setCellFormula(HSSFCell.java:598)

If you open a Workbook that has the ATP formula already present they you are able to assign the formula to new cells without issue.

Is this expected behavior or is there a defect with the ATP functions? Or do you need to alter the workbook after creation to support the ATP functions?
Comment 4 Yegor Kozlov 2010-08-12 10:27:42 UTC
Thanks for the patch, applied in r984796

> If you open a Workbook that has the ATP formula already present they you are
> able to assign the formula to new cells without issue.
> 
> Is this expected behavior or is there a defect with the ATP functions? Or do
> you need to alter the workbook after creation to support the ATP functions?

This is currently a limitation of POI. We can evaluate UDFs from the Analysis Toolpack but we cannot assign formulas containing these functions. It may seem an easy task, but unfortunately it is not. 

Firstly, FormulaParser needs additional information to process a user defined formula token. This additional information should describe the number of arguments and their type, type of the returned value, etc. For built-in functions this is described by org.apache.poi.hssf.record.formula.functionFunctionMetadata and functionMetadata.txt, I guess we will need something similar for the Analysis Toolpack.  

Secondly, each UDF used in a workbook must have a corresponding EXTERNALNAME record, this is how Excel resolves user-defined functions. Suppose you open a Workbook that has formulas with ATP functions. Each used ATP has a corresponding EXTERNALNAME record and is registered in the workbook-global LinkTable:

ISEVEN      --> EXTERNALNAME 
RANDBETWEEN --> EXTERNALNAME 

where all EXTERNALNAME records are aggregated in a LinkTable.

When you assign more formulas with ISEVEN and RANDBETWEEN, the FormulaParser resolves their names via the list of EXTERNALNAME and the assignment is successful. When you assign formulas with ISEVEN and RANDBETWEEN to a new workbook then the LinkTable is empty and you get a "completely unknown" exception.

Yegor