bug# 45973: added factory method for FormulaEvaluator, also fixed unpaired tags in javadocs
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@713021 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
32de644e51
commit
aa8ee7d78c
@ -37,6 +37,7 @@
|
|||||||
|
|
||||||
<!-- Don't forget to update status.xml too! -->
|
<!-- Don't forget to update status.xml too! -->
|
||||||
<release version="3.5-beta4" date="2008-??-??">
|
<release version="3.5-beta4" date="2008-??-??">
|
||||||
|
<action dev="POI-DEVELOPERS" type="add">45973 - added CreationHelper.createFormulaEvaluator(), implemeted both for HSSF and XSSF</action>
|
||||||
<action dev="POI-DEVELOPERS" type="fix">46182 - fixed Slideshow.readPictures() to skip pictures with invalid headers</action>
|
<action dev="POI-DEVELOPERS" type="fix">46182 - fixed Slideshow.readPictures() to skip pictures with invalid headers</action>
|
||||||
<action dev="POI-DEVELOPERS" type="fix">46137 - Handle odd files with a ContinueRecord after EOFRecord</action>
|
<action dev="POI-DEVELOPERS" type="fix">46137 - Handle odd files with a ContinueRecord after EOFRecord</action>
|
||||||
<action dev="POI-DEVELOPERS" type="fix">Fixed problem with linking shared formulas when ranges overlap</action>
|
<action dev="POI-DEVELOPERS" type="fix">Fixed problem with linking shared formulas when ranges overlap</action>
|
||||||
|
@ -32,10 +32,7 @@
|
|||||||
formulas in Excels sheets read-in, or created in POI. This document explains
|
formulas in Excels sheets read-in, or created in POI. This document explains
|
||||||
how to use the API to evaluate your formulas.
|
how to use the API to evaluate your formulas.
|
||||||
</p>
|
</p>
|
||||||
<note>In versions of POI before 3.0.3, this code lived in the
|
<note>.xlsx format is suported since POI 3.5, make sure yoy upgraded to that version before experimenting with this
|
||||||
scratchpad area of the POI SVN repository. If using an such an older
|
|
||||||
version of POI, ensure that you have the scratchpad jar or the
|
|
||||||
scratchpad build area in your classpath before experimenting with this
|
|
||||||
code. Users of all versions of POI may wish to make use of a recent
|
code. Users of all versions of POI may wish to make use of a recent
|
||||||
SVN checkout, as new functions are currently being added fairly frequently.
|
SVN checkout, as new functions are currently being added fairly frequently.
|
||||||
</note>
|
</note>
|
||||||
@ -47,7 +44,8 @@
|
|||||||
It also provides implementations for approx. 100 built in
|
It also provides implementations for approx. 100 built in
|
||||||
functions in Excel. The framework however makes is easy to add
|
functions in Excel. The framework however makes is easy to add
|
||||||
implementation of new functions. See the <link href="eval-devguide.html"> Formula
|
implementation of new functions. See the <link href="eval-devguide.html"> Formula
|
||||||
evaluation development guide</link> for details. </p>
|
evaluation development guide</link> and <link href="../apidocs/org/apache/poi/hssf/record/formula/functions/package-summary.html">javadocs</link>
|
||||||
|
for details. </p>
|
||||||
<p> Both HSSFWorkbook and XSSFWorkbook are supported, so you can
|
<p> Both HSSFWorkbook and XSSFWorkbook are supported, so you can
|
||||||
evaluate formulas on both .xls and .xlsx files.</p>
|
evaluate formulas on both .xls and .xlsx files.</p>
|
||||||
<p> Note that user-defined functions are not supported, and is not likely to done
|
<p> Note that user-defined functions are not supported, and is not likely to done
|
||||||
@ -66,38 +64,37 @@
|
|||||||
without affecting the cell</p>
|
without affecting the cell</p>
|
||||||
<source>
|
<source>
|
||||||
FileInputStream fis = new FileInputStream("c:/temp/test.xls");
|
FileInputStream fis = new FileInputStream("c:/temp/test.xls");
|
||||||
Workbook wb = new HSSFWorkbook(fis);
|
Workbook wb = new HSSFWorkbook(fis); //or new XSSFWorkbook("c:/temp/test.xls")
|
||||||
Sheet sheet = wb.getSheetAt(0);
|
Sheet sheet = wb.getSheetAt(0);
|
||||||
FormulaEvaluator evaluator = new FormulaEvaluator(sheet, wb);
|
FormulaEvaluator evaluator = wb.getCreationHelper().createFormulaEvaluator();
|
||||||
|
|
||||||
// suppose your formula is in B3
|
// suppose your formula is in B3
|
||||||
CellReference cellReference = new CellReference("B3");
|
CellReference cellReference = new CellReference("B3");
|
||||||
Row row = sheet.getRow(cellReference.getRow());
|
Row row = sheet.getRow(cellReference.getRow());
|
||||||
Cell cell = row.getCell(cellReference.getCol());
|
Cell cell = row.getCell(cellReference.getCol());
|
||||||
|
|
||||||
evaluator.setCurrentRow(row);
|
CellValue cellValue = evaluator.evaluate(cell);
|
||||||
FormulaEvaluator.CellValue cellValue = evaluator.evaluate(cell);
|
|
||||||
|
|
||||||
switch (cellValue.getCellType()) {
|
switch (cellValue.getCellType()) {
|
||||||
case Cell.CELL_TYPE_BOOLEAN:
|
case Cell.CELL_TYPE_BOOLEAN:
|
||||||
System.out.println(cellValue.getBooleanValue());
|
System.out.println(cellValue.getBooleanValue());
|
||||||
break;
|
break;
|
||||||
case Cell.CELL_TYPE_NUMERIC:
|
case Cell.CELL_TYPE_NUMERIC:
|
||||||
System.out.println(cellValue.getNumberValue());
|
System.out.println(cellValue.getNumberValue());
|
||||||
break;
|
break;
|
||||||
case Cell.CELL_TYPE_STRING:
|
case Cell.CELL_TYPE_STRING:
|
||||||
System.out.println(cellValue.getStringValue());
|
System.out.println(cellValue.getStringValue());
|
||||||
break;
|
break;
|
||||||
case Cell.CELL_TYPE_BLANK:
|
case Cell.CELL_TYPE_BLANK:
|
||||||
break;
|
break;
|
||||||
case Cell.CELL_TYPE_ERROR:
|
case Cell.CELL_TYPE_ERROR:
|
||||||
break;
|
break;
|
||||||
|
|
||||||
// CELL_TYPE_FORMULA will never happen
|
// CELL_TYPE_FORMULA will never happen
|
||||||
case Cell.CELL_TYPE_FORMULA:
|
case Cell.CELL_TYPE_FORMULA:
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
</source>
|
</source>
|
||||||
<p>Thus using the retrieved value (of type
|
<p>Thus using the retrieved value (of type
|
||||||
FormulaEvaluator.CellValue - a nested class) returned
|
FormulaEvaluator.CellValue - a nested class) returned
|
||||||
by FormulaEvaluator is similar to using a Cell object
|
by FormulaEvaluator is similar to using a Cell object
|
||||||
@ -117,39 +114,38 @@ switch (cellValue.getCellType()) {
|
|||||||
formula remains in the cell, just with a new value</p>
|
formula remains in the cell, just with a new value</p>
|
||||||
<p>The return of the function is the type of the
|
<p>The return of the function is the type of the
|
||||||
formula result, such as Cell.CELL_TYPE_BOOLEAN</p>
|
formula result, such as Cell.CELL_TYPE_BOOLEAN</p>
|
||||||
<source>
|
<source>
|
||||||
FileInputStream fis = new FileInputStream("/somepath/test.xls");
|
FileInputStream fis = new FileInputStream("/somepath/test.xls");
|
||||||
Workbook wb = new HSSFWorkbook(fis);
|
Workbook wb = new HSSFWorkbook(fis); //or new XSSFWorkbook("/somepath/test.xls")
|
||||||
Sheet sheet = wb.getSheetAt(0);
|
Sheet sheet = wb.getSheetAt(0);
|
||||||
FormulaEvaluator evaluator = new FormulaEvaluator(sheet, wb);
|
FormulaEvaluator evaluator = wb.getCreationHelper().createFormulaEvaluator();
|
||||||
|
|
||||||
// suppose your formula is in B3
|
// suppose your formula is in B3
|
||||||
CellReference cellReference = new CellReference("B3");
|
CellReference cellReference = new CellReference("B3");
|
||||||
Row row = sheet.getRow(cellReference.getRow());
|
Row row = sheet.getRow(cellReference.getRow());
|
||||||
Cell cell = row.getCell(cellReference.getCol());
|
Cell cell = row.getCell(cellReference.getCol());
|
||||||
evaluator.setCurrentRow(row);
|
|
||||||
|
|
||||||
if (cell!=null) {
|
if (cell!=null) {
|
||||||
switch (<strong>evaluator.evaluateFormulaCell</strong>(cell)) {
|
switch (evaluator.evaluateFormulaCell(cell)) {
|
||||||
case Cell.CELL_TYPE_BOOLEAN:
|
case Cell.CELL_TYPE_BOOLEAN:
|
||||||
System.out.println(cell.getBooleanCellValue());
|
System.out.println(cell.getBooleanCellValue());
|
||||||
break;
|
break;
|
||||||
case Cell.CELL_TYPE_NUMERIC:
|
case Cell.CELL_TYPE_NUMERIC:
|
||||||
System.out.println(cell.getNumberCellValue());
|
System.out.println(cell.getNumericCellValue());
|
||||||
break;
|
break;
|
||||||
case Cell.CELL_TYPE_STRING:
|
case Cell.CELL_TYPE_STRING:
|
||||||
System.out.println(cell.getStringCellValue());
|
System.out.println(cell.getStringCellValue());
|
||||||
break;
|
break;
|
||||||
case Cell.CELL_TYPE_BLANK:
|
case Cell.CELL_TYPE_BLANK:
|
||||||
break;
|
break;
|
||||||
case Cell.CELL_TYPE_ERROR:
|
case Cell.CELL_TYPE_ERROR:
|
||||||
System.out.println(cell.getErrorCellValue());
|
System.out.println(cell.getErrorCellValue());
|
||||||
break;
|
break;
|
||||||
|
|
||||||
// CELL_TYPE_FORMULA will never occur
|
// CELL_TYPE_FORMULA will never occur
|
||||||
case Cell.CELL_TYPE_FORMULA:
|
case Cell.CELL_TYPE_FORMULA:
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
</source>
|
</source>
|
||||||
</section>
|
</section>
|
||||||
@ -163,64 +159,59 @@ if (cell!=null) {
|
|||||||
in place of the old formula.</p>
|
in place of the old formula.</p>
|
||||||
<source>
|
<source>
|
||||||
FileInputStream fis = new FileInputStream("/somepath/test.xls");
|
FileInputStream fis = new FileInputStream("/somepath/test.xls");
|
||||||
Workbook wb = new HSSFWorkbook(fis);
|
Workbook wb = new HSSFWorkbook(fis); //or new XSSFWorkbook("/somepath/test.xls")
|
||||||
Sheet sheet = wb.getSheetAt(0);
|
Sheet sheet = wb.getSheetAt(0);
|
||||||
FormulaEvaluator evaluator = new FormulaEvaluator(sheet, wb);
|
FormulaEvaluator evaluator = wb.getCreationHelper().createFormulaEvaluator();
|
||||||
|
|
||||||
// suppose your formula is in B3
|
// suppose your formula is in B3
|
||||||
CellReference cellReference = new CellReference("B3");
|
CellReference cellReference = new CellReference("B3");
|
||||||
Row row = sheet.getRow(cellReference.getRow());
|
Row row = sheet.getRow(cellReference.getRow());
|
||||||
Cell cell = row.getCell(cellReference.getCol());
|
Cell cell = row.getCell(cellReference.getCol());
|
||||||
evaluator.setCurrentRow(row);
|
|
||||||
|
|
||||||
if (cell!=null) {
|
if (cell!=null) {
|
||||||
switch (<strong>evaluator.evaluateInCell</strong>(cell).getCellType()) {
|
switch (evaluator.<strong>evaluateInCell</strong>(cell).getCellType()) {
|
||||||
case Cell.CELL_TYPE_BOOLEAN:
|
case Cell.CELL_TYPE_BOOLEAN:
|
||||||
System.out.println(cell.getBooleanCellValue());
|
System.out.println(cell.getBooleanCellValue());
|
||||||
break;
|
break;
|
||||||
case Cell.CELL_TYPE_NUMERIC:
|
case Cell.CELL_TYPE_NUMERIC:
|
||||||
System.out.println(cell.getNumberCellValue());
|
System.out.println(cell.getNumericCellValue());
|
||||||
break;
|
break;
|
||||||
case Cell.CELL_TYPE_STRING:
|
case Cell.CELL_TYPE_STRING:
|
||||||
System.out.println(cell.getStringCellValue());
|
System.out.println(cell.getStringCellValue());
|
||||||
break;
|
break;
|
||||||
case Cell.CELL_TYPE_BLANK:
|
case Cell.CELL_TYPE_BLANK:
|
||||||
break;
|
break;
|
||||||
case Cell.CELL_TYPE_ERROR:
|
case Cell.CELL_TYPE_ERROR:
|
||||||
System.out.println(cell.getErrorCellValue());
|
System.out.println(cell.getErrorCellValue());
|
||||||
break;
|
break;
|
||||||
|
|
||||||
// CELL_TYPE_FORMULA will never occur
|
// CELL_TYPE_FORMULA will never occur
|
||||||
case Cell.CELL_TYPE_FORMULA:
|
case Cell.CELL_TYPE_FORMULA:
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
</source>
|
|
||||||
|
</source>
|
||||||
</section>
|
</section>
|
||||||
|
|
||||||
<anchor id="EvaluateAll"/>
|
<anchor id="EvaluateAll"/>
|
||||||
<section><title>Re-calculating all formulas in a Workbook</title>
|
<section><title>Re-calculating all formulas in a Workbook</title>
|
||||||
<source>
|
<source>
|
||||||
|
|
||||||
FileInputStream fis = new FileInputStream("/somepath/test.xls");
|
FileInputStream fis = new FileInputStream("/somepath/test.xls");
|
||||||
Workbook wb = new HSSFWorkbook(fis);
|
Workbook wb = new HSSFWorkbook(fis); //or new XSSFWorkbook("/somepath/test.xls")
|
||||||
|
FormulaEvaluator evaluator = wb.getCreationHelper().createFormulaEvaluator();
|
||||||
for(int sheetNum = 0; sheetNum < wb.getNumberOfSheets(); sheetNum++) {
|
for(int sheetNum = 0; sheetNum < wb.getNumberOfSheets(); sheetNum++) {
|
||||||
Sheet sheet = wb.getSheetAt(sheetNum);
|
Sheet sheet = wb.getSheetAt(sheetNum);
|
||||||
FormulaEvaluator evaluator = new FormulaEvaluator(sheet, wb);
|
for(Row r : sheet) {
|
||||||
|
for(Cell c : r) {
|
||||||
for(Iterator rit = sheet.rowIterator(); rit.hasNext();) {
|
if(c.getCellType() == Cell.CELL_TYPE_FORMULA) {
|
||||||
Row r = (Row)rit.next();
|
evaluator.evaluateFormulaCell(c);
|
||||||
evaluator.setCurrentRow(r);
|
}
|
||||||
|
}
|
||||||
for(Iterator cit = r.cellIterator(); cit.hasNext();) {
|
}
|
||||||
Cell c = (Cell)cit.next();
|
|
||||||
if(c.getCellType() == Cell.CELL_TYPE_FORMULA) {
|
|
||||||
evaluator.evaluateFormulaCell(c);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
wb.write(new FileOutputStream("/somepath/changed.xls"));
|
</source>
|
||||||
</source>
|
|
||||||
</section>
|
</section>
|
||||||
</section>
|
</section>
|
||||||
|
|
||||||
|
@ -34,6 +34,7 @@
|
|||||||
<!-- Don't forget to update changes.xml too! -->
|
<!-- Don't forget to update changes.xml too! -->
|
||||||
<changes>
|
<changes>
|
||||||
<release version="3.5-beta4" date="2008-??-??">
|
<release version="3.5-beta4" date="2008-??-??">
|
||||||
|
<action dev="POI-DEVELOPERS" type="add">45973 - added CreationHelper.createFormulaEvaluator(), implemeted both for HSSF and XSSF</action>
|
||||||
<action dev="POI-DEVELOPERS" type="fix">46182 - fixed Slideshow.readPictures() to skip pictures with invalid headers</action>
|
<action dev="POI-DEVELOPERS" type="fix">46182 - fixed Slideshow.readPictures() to skip pictures with invalid headers</action>
|
||||||
<action dev="POI-DEVELOPERS" type="fix">46137 - Handle odd files with a ContinueRecord after EOFRecord</action>
|
<action dev="POI-DEVELOPERS" type="fix">46137 - Handle odd files with a ContinueRecord after EOFRecord</action>
|
||||||
<action dev="POI-DEVELOPERS" type="fix">Fixed problem with linking shared formulas when ranges overlap</action>
|
<action dev="POI-DEVELOPERS" type="fix">Fixed problem with linking shared formulas when ranges overlap</action>
|
||||||
|
@ -31,14 +31,14 @@ import org.apache.poi.hssf.record.formula.eval.StringEval;
|
|||||||
import org.apache.poi.hssf.record.formula.functions.CountUtils.I_MatchPredicate;
|
import org.apache.poi.hssf.record.formula.functions.CountUtils.I_MatchPredicate;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Implementation for the function COUNTIF<p/>
|
* Implementation for the function COUNTIF
|
||||||
*
|
* <p>
|
||||||
* Syntax: COUNTIF ( range, criteria )
|
* Syntax: COUNTIF ( range, criteria )
|
||||||
* <table border="0" cellpadding="1" cellspacing="0" summary="Parameter descriptions">
|
* <table border="0" cellpadding="1" cellspacing="0" summary="Parameter descriptions">
|
||||||
* <tr><th>range </th><td>is the range of cells to be counted based on the criteria</td></tr>
|
* <tr><th>range </th><td>is the range of cells to be counted based on the criteria</td></tr>
|
||||||
* <tr><th>criteria</th><td>is used to determine which cells to count</td></tr>
|
* <tr><th>criteria</th><td>is used to determine which cells to count</td></tr>
|
||||||
* </table>
|
* </table>
|
||||||
* <p/>
|
* </p>
|
||||||
*
|
*
|
||||||
* @author Josh Micich
|
* @author Josh Micich
|
||||||
*/
|
*/
|
||||||
|
@ -25,13 +25,13 @@ import org.apache.poi.hssf.record.formula.eval.OperandResolver;
|
|||||||
import org.apache.poi.hssf.usermodel.HSSFErrorConstants;
|
import org.apache.poi.hssf.usermodel.HSSFErrorConstants;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Implementation for the ERROR.TYPE() Excel function.<p/>
|
* Implementation for the ERROR.TYPE() Excel function.
|
||||||
*
|
* <p>
|
||||||
* <b>Syntax:</b><br/>
|
* <b>Syntax:</b><br/>
|
||||||
* <b>ERROR.TYPE</b>(<b>errorValue</b>)<p/>
|
* <b>ERROR.TYPE</b>(<b>errorValue</b>)</p>
|
||||||
*
|
* <p>
|
||||||
* Returns a number corresponding to the error type of the supplied argument.<p/>
|
* Returns a number corresponding to the error type of the supplied argument.<p/>
|
||||||
*
|
* <p>
|
||||||
* <table border="1" cellpadding="1" cellspacing="1" summary="Return values for ERROR.TYPE()">
|
* <table border="1" cellpadding="1" cellspacing="1" summary="Return values for ERROR.TYPE()">
|
||||||
* <tr><td>errorValue</td><td>Return Value</td></tr>
|
* <tr><td>errorValue</td><td>Return Value</td></tr>
|
||||||
* <tr><td>#NULL!</td><td>1</td></tr>
|
* <tr><td>#NULL!</td><td>1</td></tr>
|
||||||
@ -46,7 +46,8 @@ import org.apache.poi.hssf.usermodel.HSSFErrorConstants;
|
|||||||
*
|
*
|
||||||
* Note - the results of ERROR.TYPE() are different to the constants defined in
|
* Note - the results of ERROR.TYPE() are different to the constants defined in
|
||||||
* <tt>HSSFErrorConstants</tt>.
|
* <tt>HSSFErrorConstants</tt>.
|
||||||
*
|
* </p>
|
||||||
|
*
|
||||||
* @author Josh Micich
|
* @author Josh Micich
|
||||||
*/
|
*/
|
||||||
public final class Errortype implements Function {
|
public final class Errortype implements Function {
|
||||||
|
@ -26,7 +26,8 @@ import org.apache.poi.hssf.record.formula.eval.RefEval;
|
|||||||
import org.apache.poi.hssf.record.formula.eval.ValueEval;
|
import org.apache.poi.hssf.record.formula.eval.ValueEval;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Implementation for the Excel function INDEX<p/>
|
* Implementation for the Excel function INDEX
|
||||||
|
* <p>
|
||||||
*
|
*
|
||||||
* Syntax : <br/>
|
* Syntax : <br/>
|
||||||
* INDEX ( reference, row_num[, column_num [, area_num]])</br>
|
* INDEX ( reference, row_num[, column_num [, area_num]])</br>
|
||||||
@ -38,7 +39,7 @@ import org.apache.poi.hssf.record.formula.eval.ValueEval;
|
|||||||
* <tr><th>column_num</th><td>selects column within the array or area reference. default is 1</td></tr>
|
* <tr><th>column_num</th><td>selects column within the array or area reference. default is 1</td></tr>
|
||||||
* <tr><th>area_num</th><td>used when reference is a union of areas</td></tr>
|
* <tr><th>area_num</th><td>used when reference is a union of areas</td></tr>
|
||||||
* </table>
|
* </table>
|
||||||
* <p/>
|
* </p>
|
||||||
*
|
*
|
||||||
* @author Josh Micich
|
* @author Josh Micich
|
||||||
*/
|
*/
|
||||||
|
@ -31,7 +31,7 @@ import org.apache.poi.hssf.record.formula.eval.ValueEval;
|
|||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Implementation for the Excel function SUMPRODUCT<p/>
|
* Implementation for the Excel function SUMPRODUCT<p>
|
||||||
*
|
*
|
||||||
* Syntax : <br/>
|
* Syntax : <br/>
|
||||||
* SUMPRODUCT ( array1[, array2[, array3[, ...]]])
|
* SUMPRODUCT ( array1[, array2[, array3[, ...]]])
|
||||||
@ -49,7 +49,7 @@ import org.apache.poi.hssf.record.formula.eval.ValueEval;
|
|||||||
* A<b>n</b><sub>(<b>i</b>,<b>j</b>)</sub>
|
* A<b>n</b><sub>(<b>i</b>,<b>j</b>)</sub>
|
||||||
* )
|
* )
|
||||||
* )
|
* )
|
||||||
*
|
* </p>
|
||||||
* @author Josh Micich
|
* @author Josh Micich
|
||||||
*/
|
*/
|
||||||
public final class Sumproduct implements Function {
|
public final class Sumproduct implements Function {
|
||||||
|
@ -671,9 +671,7 @@ public class HSSFCell implements Cell {
|
|||||||
* get the value of the cell as a string - for numeric cells we throw an exception.
|
* get the value of the cell as a string - for numeric cells we throw an exception.
|
||||||
* For blank cells we return an empty string.
|
* For blank cells we return an empty string.
|
||||||
* For formulaCells that are not string Formulas, we return empty String
|
* For formulaCells that are not string Formulas, we return empty String
|
||||||
* @deprecated Use the HSSFRichTextString return
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
public String getStringCellValue()
|
public String getStringCellValue()
|
||||||
{
|
{
|
||||||
HSSFRichTextString str = getRichStringCellValue();
|
HSSFRichTextString str = getRichStringCellValue();
|
||||||
|
@ -44,4 +44,14 @@ public class HSSFCreationHelper implements CreationHelper {
|
|||||||
public HSSFHyperlink createHyperlink(int type) {
|
public HSSFHyperlink createHyperlink(int type) {
|
||||||
return new HSSFHyperlink(type);
|
return new HSSFHyperlink(type);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates a HSSFFormulaEvaluator, the object that evaluates formula cells.
|
||||||
|
*
|
||||||
|
* @return a HSSFFormulaEvaluator instance
|
||||||
|
*/
|
||||||
|
public HSSFFormulaEvaluator createFormulaEvaluator(){
|
||||||
|
return new HSSFFormulaEvaluator(workbook);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -28,6 +28,7 @@ import org.apache.poi.ss.formula.CollaboratingWorkbooksEnvironment;
|
|||||||
import org.apache.poi.ss.formula.WorkbookEvaluator;
|
import org.apache.poi.ss.formula.WorkbookEvaluator;
|
||||||
import org.apache.poi.ss.usermodel.Cell;
|
import org.apache.poi.ss.usermodel.Cell;
|
||||||
import org.apache.poi.ss.usermodel.CellValue;
|
import org.apache.poi.ss.usermodel.CellValue;
|
||||||
|
import org.apache.poi.ss.usermodel.FormulaEvaluator;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Evaluates formula cells.<p/>
|
* Evaluates formula cells.<p/>
|
||||||
@ -39,255 +40,268 @@ import org.apache.poi.ss.usermodel.CellValue;
|
|||||||
* @author Amol S. Deshmukh < amolweb at ya hoo dot com >
|
* @author Amol S. Deshmukh < amolweb at ya hoo dot com >
|
||||||
* @author Josh Micich
|
* @author Josh Micich
|
||||||
*/
|
*/
|
||||||
public class HSSFFormulaEvaluator /* almost implements FormulaEvaluator */ {
|
public class HSSFFormulaEvaluator implements FormulaEvaluator {
|
||||||
|
|
||||||
private WorkbookEvaluator _bookEvaluator;
|
private WorkbookEvaluator _bookEvaluator;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @deprecated (Sep 2008) HSSFSheet parameter is ignored
|
* @deprecated (Sep 2008) HSSFSheet parameter is ignored
|
||||||
*/
|
*/
|
||||||
public HSSFFormulaEvaluator(HSSFSheet sheet, HSSFWorkbook workbook) {
|
public HSSFFormulaEvaluator(HSSFSheet sheet, HSSFWorkbook workbook) {
|
||||||
this(workbook);
|
this(workbook);
|
||||||
if (false) {
|
if (false) {
|
||||||
sheet.toString(); // suppress unused parameter compiler warning
|
sheet.toString(); // suppress unused parameter compiler warning
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
public HSSFFormulaEvaluator(HSSFWorkbook workbook) {
|
public HSSFFormulaEvaluator(HSSFWorkbook workbook) {
|
||||||
_bookEvaluator = new WorkbookEvaluator(HSSFEvaluationWorkbook.create(workbook));
|
_bookEvaluator = new WorkbookEvaluator(HSSFEvaluationWorkbook.create(workbook));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Coordinates several formula evaluators together so that formulas that involve external
|
|
||||||
* references can be evaluated.
|
|
||||||
* @param workbookNames the simple file names used to identify the workbooks in formulas
|
|
||||||
* with external links (for example "MyData.xls" as used in a formula "[MyData.xls]Sheet1!A1")
|
|
||||||
* @param evaluators all evaluators for the full set of workbooks required by the formulas.
|
|
||||||
*/
|
|
||||||
public static void setupEnvironment(String[] workbookNames, HSSFFormulaEvaluator[] evaluators) {
|
|
||||||
WorkbookEvaluator[] wbEvals = new WorkbookEvaluator[evaluators.length];
|
|
||||||
for (int i = 0; i < wbEvals.length; i++) {
|
|
||||||
wbEvals[i] = evaluators[i]._bookEvaluator;
|
|
||||||
}
|
|
||||||
CollaboratingWorkbooksEnvironment.setup(workbookNames, wbEvals);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Does nothing
|
* Coordinates several formula evaluators together so that formulas that involve external
|
||||||
* @deprecated (Aug 2008) - not needed, since the current row can be derived from the cell
|
* references can be evaluated.
|
||||||
*/
|
* @param workbookNames the simple file names used to identify the workbooks in formulas
|
||||||
public void setCurrentRow(HSSFRow row) {
|
* with external links (for example "MyData.xls" as used in a formula "[MyData.xls]Sheet1!A1")
|
||||||
// do nothing
|
* @param evaluators all evaluators for the full set of workbooks required by the formulas.
|
||||||
if (false) {
|
*/
|
||||||
row.getClass(); // suppress unused parameter compiler warning
|
public static void setupEnvironment(String[] workbookNames, HSSFFormulaEvaluator[] evaluators) {
|
||||||
}
|
WorkbookEvaluator[] wbEvals = new WorkbookEvaluator[evaluators.length];
|
||||||
}
|
for (int i = 0; i < wbEvals.length; i++) {
|
||||||
|
wbEvals[i] = evaluators[i]._bookEvaluator;
|
||||||
|
}
|
||||||
|
CollaboratingWorkbooksEnvironment.setup(workbookNames, wbEvals);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Should be called whenever there are major changes (e.g. moving sheets) to input cells
|
* Does nothing
|
||||||
* in the evaluated workbook. If performance is not critical, a single call to this method
|
* @deprecated (Aug 2008) - not needed, since the current row can be derived from the cell
|
||||||
* may be used instead of many specific calls to the notify~ methods.
|
*/
|
||||||
*
|
public void setCurrentRow(HSSFRow row) {
|
||||||
* Failure to call this method after changing cell values will cause incorrect behaviour
|
// do nothing
|
||||||
* of the evaluate~ methods of this class
|
if (false) {
|
||||||
*/
|
row.getClass(); // suppress unused parameter compiler warning
|
||||||
public void clearAllCachedResultValues() {
|
}
|
||||||
_bookEvaluator.clearAllCachedResultValues();
|
}
|
||||||
}
|
|
||||||
/**
|
|
||||||
* Should be called to tell the cell value cache that the specified (value or formula) cell
|
|
||||||
* has changed.
|
|
||||||
* Failure to call this method after changing cell values will cause incorrect behaviour
|
|
||||||
* of the evaluate~ methods of this class
|
|
||||||
*/
|
|
||||||
public void notifyUpdateCell(HSSFCell cell) {
|
|
||||||
_bookEvaluator.notifyUpdateCell(new HSSFEvaluationCell(cell));
|
|
||||||
}
|
|
||||||
/**
|
|
||||||
* Should be called to tell the cell value cache that the specified cell has just been
|
|
||||||
* deleted.
|
|
||||||
* Failure to call this method after changing cell values will cause incorrect behaviour
|
|
||||||
* of the evaluate~ methods of this class
|
|
||||||
*/
|
|
||||||
public void notifyDeleteCell(HSSFCell cell) {
|
|
||||||
_bookEvaluator.notifyDeleteCell(new HSSFEvaluationCell(cell));
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* If cell contains a formula, the formula is evaluated and returned,
|
* Should be called whenever there are major changes (e.g. moving sheets) to input cells
|
||||||
* else the CellValue simply copies the appropriate cell value from
|
* in the evaluated workbook. If performance is not critical, a single call to this method
|
||||||
* the cell and also its cell type. This method should be preferred over
|
* may be used instead of many specific calls to the notify~ methods.
|
||||||
* evaluateInCell() when the call should not modify the contents of the
|
*
|
||||||
* original cell.
|
* Failure to call this method after changing cell values will cause incorrect behaviour
|
||||||
*
|
* of the evaluate~ methods of this class
|
||||||
* @param cell may be <code>null</code> signifying that the cell is not present (or blank)
|
*/
|
||||||
* @return <code>null</code> if the supplied cell is <code>null</code> or blank
|
public void clearAllCachedResultValues() {
|
||||||
*/
|
_bookEvaluator.clearAllCachedResultValues();
|
||||||
public CellValue evaluate(Cell cell) {
|
}
|
||||||
if (cell == null) {
|
/**
|
||||||
return null;
|
* Should be called to tell the cell value cache that the specified (value or formula) cell
|
||||||
}
|
* has changed.
|
||||||
|
* Failure to call this method after changing cell values will cause incorrect behaviour
|
||||||
|
* of the evaluate~ methods of this class
|
||||||
|
*/
|
||||||
|
public void notifyUpdateCell(HSSFCell cell) {
|
||||||
|
_bookEvaluator.notifyUpdateCell(new HSSFEvaluationCell(cell));
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* Should be called to tell the cell value cache that the specified cell has just been
|
||||||
|
* deleted.
|
||||||
|
* Failure to call this method after changing cell values will cause incorrect behaviour
|
||||||
|
* of the evaluate~ methods of this class
|
||||||
|
*/
|
||||||
|
public void notifyDeleteCell(HSSFCell cell) {
|
||||||
|
_bookEvaluator.notifyDeleteCell(new HSSFEvaluationCell(cell));
|
||||||
|
}
|
||||||
|
public void notifyDeleteCell(Cell cell) {
|
||||||
|
_bookEvaluator.notifyDeleteCell(new HSSFEvaluationCell((HSSFCell)cell));
|
||||||
|
}
|
||||||
|
|
||||||
switch (cell.getCellType()) {
|
/**
|
||||||
case HSSFCell.CELL_TYPE_BOOLEAN:
|
* Should be called to tell the cell value cache that the specified (value or formula) cell
|
||||||
return CellValue.valueOf(cell.getBooleanCellValue());
|
* has changed.
|
||||||
case HSSFCell.CELL_TYPE_ERROR:
|
* Failure to call this method after changing cell values will cause incorrect behaviour
|
||||||
return CellValue.getError(cell.getErrorCellValue());
|
* of the evaluate~ methods of this class
|
||||||
case HSSFCell.CELL_TYPE_FORMULA:
|
*/
|
||||||
return evaluateFormulaCellValue(cell);
|
public void notifySetFormula(Cell cell) {
|
||||||
case HSSFCell.CELL_TYPE_NUMERIC:
|
_bookEvaluator.notifyUpdateCell(new HSSFEvaluationCell((HSSFCell)cell));
|
||||||
return new CellValue(cell.getNumericCellValue());
|
}
|
||||||
case HSSFCell.CELL_TYPE_STRING:
|
|
||||||
return new CellValue(cell.getRichStringCellValue().getString());
|
/**
|
||||||
case HSSFCell.CELL_TYPE_BLANK:
|
* If cell contains a formula, the formula is evaluated and returned,
|
||||||
return null;
|
* else the CellValue simply copies the appropriate cell value from
|
||||||
}
|
* the cell and also its cell type. This method should be preferred over
|
||||||
throw new IllegalStateException("Bad cell type (" + cell.getCellType() + ")");
|
* evaluateInCell() when the call should not modify the contents of the
|
||||||
}
|
* original cell.
|
||||||
|
*
|
||||||
|
* @param cell may be <code>null</code> signifying that the cell is not present (or blank)
|
||||||
|
* @return <code>null</code> if the supplied cell is <code>null</code> or blank
|
||||||
|
*/
|
||||||
|
public CellValue evaluate(Cell cell) {
|
||||||
|
if (cell == null) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
switch (cell.getCellType()) {
|
||||||
|
case HSSFCell.CELL_TYPE_BOOLEAN:
|
||||||
|
return CellValue.valueOf(cell.getBooleanCellValue());
|
||||||
|
case HSSFCell.CELL_TYPE_ERROR:
|
||||||
|
return CellValue.getError(cell.getErrorCellValue());
|
||||||
|
case HSSFCell.CELL_TYPE_FORMULA:
|
||||||
|
return evaluateFormulaCellValue(cell);
|
||||||
|
case HSSFCell.CELL_TYPE_NUMERIC:
|
||||||
|
return new CellValue(cell.getNumericCellValue());
|
||||||
|
case HSSFCell.CELL_TYPE_STRING:
|
||||||
|
return new CellValue(cell.getRichStringCellValue().getString());
|
||||||
|
case HSSFCell.CELL_TYPE_BLANK:
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
throw new IllegalStateException("Bad cell type (" + cell.getCellType() + ")");
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* If cell contains formula, it evaluates the formula, and saves the result of the formula. The
|
* If cell contains formula, it evaluates the formula, and saves the result of the formula. The
|
||||||
* cell remains as a formula cell. If the cell does not contain formula, this method returns -1
|
* cell remains as a formula cell. If the cell does not contain formula, this method returns -1
|
||||||
* and leaves the cell unchanged.
|
* and leaves the cell unchanged.
|
||||||
*
|
*
|
||||||
* Note that the type of the <em>formula result</em> is returned, so you know what kind of
|
* Note that the type of the <em>formula result</em> is returned, so you know what kind of
|
||||||
* cached formula result is also stored with the formula.
|
* cached formula result is also stored with the formula.
|
||||||
* <pre>
|
* <pre>
|
||||||
* int evaluatedCellType = evaluator.evaluateFormulaCell(cell);
|
* int evaluatedCellType = evaluator.evaluateFormulaCell(cell);
|
||||||
* </pre>
|
* </pre>
|
||||||
* Be aware that your cell will hold both the formula, and the result. If you want the cell
|
* Be aware that your cell will hold both the formula, and the result. If you want the cell
|
||||||
* replaced with the result of the formula, use {@link #evaluateInCell(org.apache.poi.ss.usermodel.Cell)}
|
* replaced with the result of the formula, use {@link #evaluateInCell(org.apache.poi.ss.usermodel.Cell)}
|
||||||
* @param cell The cell to evaluate
|
* @param cell The cell to evaluate
|
||||||
* @return -1 for non-formula cells, or the type of the <em>formula result</em>
|
* @return -1 for non-formula cells, or the type of the <em>formula result</em>
|
||||||
*/
|
*/
|
||||||
public int evaluateFormulaCell(Cell cell) {
|
public int evaluateFormulaCell(Cell cell) {
|
||||||
if (cell == null || cell.getCellType() != HSSFCell.CELL_TYPE_FORMULA) {
|
if (cell == null || cell.getCellType() != HSSFCell.CELL_TYPE_FORMULA) {
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
CellValue cv = evaluateFormulaCellValue(cell);
|
CellValue cv = evaluateFormulaCellValue(cell);
|
||||||
// cell remains a formula cell, but the cached value is changed
|
// cell remains a formula cell, but the cached value is changed
|
||||||
setCellValue(cell, cv);
|
setCellValue(cell, cv);
|
||||||
return cv.getCellType();
|
return cv.getCellType();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* If cell contains formula, it evaluates the formula, and
|
* If cell contains formula, it evaluates the formula, and
|
||||||
* puts the formula result back into the cell, in place
|
* puts the formula result back into the cell, in place
|
||||||
* of the old formula.
|
* of the old formula.
|
||||||
* Else if cell does not contain formula, this method leaves
|
* Else if cell does not contain formula, this method leaves
|
||||||
* the cell unchanged.
|
* the cell unchanged.
|
||||||
* Note that the same instance of HSSFCell is returned to
|
* Note that the same instance of HSSFCell is returned to
|
||||||
* allow chained calls like:
|
* allow chained calls like:
|
||||||
* <pre>
|
* <pre>
|
||||||
* int evaluatedCellType = evaluator.evaluateInCell(cell).getCellType();
|
* int evaluatedCellType = evaluator.evaluateInCell(cell).getCellType();
|
||||||
* </pre>
|
* </pre>
|
||||||
* Be aware that your cell value will be changed to hold the
|
* Be aware that your cell value will be changed to hold the
|
||||||
* result of the formula. If you simply want the formula
|
* result of the formula. If you simply want the formula
|
||||||
* value computed for you, use {@link #evaluateFormulaCell(org.apache.poi.ss.usermodel.Cell)}}
|
* value computed for you, use {@link #evaluateFormulaCell(org.apache.poi.ss.usermodel.Cell)}}
|
||||||
* @param cell
|
* @param cell
|
||||||
*/
|
*/
|
||||||
public HSSFCell evaluateInCell(Cell cell) {
|
public HSSFCell evaluateInCell(Cell cell) {
|
||||||
if (cell == null) {
|
if (cell == null) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
HSSFCell result = (HSSFCell) cell;
|
HSSFCell result = (HSSFCell) cell;
|
||||||
if (cell.getCellType() == HSSFCell.CELL_TYPE_FORMULA) {
|
if (cell.getCellType() == HSSFCell.CELL_TYPE_FORMULA) {
|
||||||
CellValue cv = evaluateFormulaCellValue(cell);
|
CellValue cv = evaluateFormulaCellValue(cell);
|
||||||
setCellType(cell, cv); // cell will no longer be a formula cell
|
setCellType(cell, cv); // cell will no longer be a formula cell
|
||||||
setCellValue(cell, cv);
|
setCellValue(cell, cv);
|
||||||
}
|
}
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
private static void setCellType(Cell cell, CellValue cv) {
|
private static void setCellType(Cell cell, CellValue cv) {
|
||||||
int cellType = cv.getCellType();
|
int cellType = cv.getCellType();
|
||||||
switch (cellType) {
|
switch (cellType) {
|
||||||
case HSSFCell.CELL_TYPE_BOOLEAN:
|
case HSSFCell.CELL_TYPE_BOOLEAN:
|
||||||
case HSSFCell.CELL_TYPE_ERROR:
|
case HSSFCell.CELL_TYPE_ERROR:
|
||||||
case HSSFCell.CELL_TYPE_NUMERIC:
|
case HSSFCell.CELL_TYPE_NUMERIC:
|
||||||
case HSSFCell.CELL_TYPE_STRING:
|
case HSSFCell.CELL_TYPE_STRING:
|
||||||
cell.setCellType(cellType);
|
cell.setCellType(cellType);
|
||||||
return;
|
return;
|
||||||
case HSSFCell.CELL_TYPE_BLANK:
|
case HSSFCell.CELL_TYPE_BLANK:
|
||||||
// never happens - blanks eventually get translated to zero
|
// never happens - blanks eventually get translated to zero
|
||||||
case HSSFCell.CELL_TYPE_FORMULA:
|
case HSSFCell.CELL_TYPE_FORMULA:
|
||||||
// this will never happen, we have already evaluated the formula
|
// this will never happen, we have already evaluated the formula
|
||||||
}
|
}
|
||||||
throw new IllegalStateException("Unexpected cell value type (" + cellType + ")");
|
throw new IllegalStateException("Unexpected cell value type (" + cellType + ")");
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void setCellValue(Cell cell, CellValue cv) {
|
private static void setCellValue(Cell cell, CellValue cv) {
|
||||||
int cellType = cv.getCellType();
|
int cellType = cv.getCellType();
|
||||||
switch (cellType) {
|
switch (cellType) {
|
||||||
case HSSFCell.CELL_TYPE_BOOLEAN:
|
case HSSFCell.CELL_TYPE_BOOLEAN:
|
||||||
cell.setCellValue(cv.getBooleanValue());
|
cell.setCellValue(cv.getBooleanValue());
|
||||||
break;
|
break;
|
||||||
case HSSFCell.CELL_TYPE_ERROR:
|
case HSSFCell.CELL_TYPE_ERROR:
|
||||||
cell.setCellErrorValue(cv.getErrorValue());
|
cell.setCellErrorValue(cv.getErrorValue());
|
||||||
break;
|
break;
|
||||||
case HSSFCell.CELL_TYPE_NUMERIC:
|
case HSSFCell.CELL_TYPE_NUMERIC:
|
||||||
cell.setCellValue(cv.getNumberValue());
|
cell.setCellValue(cv.getNumberValue());
|
||||||
break;
|
break;
|
||||||
case HSSFCell.CELL_TYPE_STRING:
|
case HSSFCell.CELL_TYPE_STRING:
|
||||||
cell.setCellValue(new HSSFRichTextString(cv.getStringValue()));
|
cell.setCellValue(new HSSFRichTextString(cv.getStringValue()));
|
||||||
break;
|
break;
|
||||||
case HSSFCell.CELL_TYPE_BLANK:
|
case HSSFCell.CELL_TYPE_BLANK:
|
||||||
// never happens - blanks eventually get translated to zero
|
// never happens - blanks eventually get translated to zero
|
||||||
case HSSFCell.CELL_TYPE_FORMULA:
|
case HSSFCell.CELL_TYPE_FORMULA:
|
||||||
// this will never happen, we have already evaluated the formula
|
// this will never happen, we have already evaluated the formula
|
||||||
default:
|
default:
|
||||||
throw new IllegalStateException("Unexpected cell value type (" + cellType + ")");
|
throw new IllegalStateException("Unexpected cell value type (" + cellType + ")");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Loops over all cells in all sheets of the supplied
|
* Loops over all cells in all sheets of the supplied
|
||||||
* workbook.
|
* workbook.
|
||||||
* For cells that contain formulas, their formulas are
|
* For cells that contain formulas, their formulas are
|
||||||
* evaluated, and the results are saved. These cells
|
* evaluated, and the results are saved. These cells
|
||||||
* remain as formula cells.
|
* remain as formula cells.
|
||||||
* For cells that do not contain formulas, no changes
|
* For cells that do not contain formulas, no changes
|
||||||
* are made.
|
* are made.
|
||||||
* This is a helpful wrapper around looping over all
|
* This is a helpful wrapper around looping over all
|
||||||
* cells, and calling evaluateFormulaCell on each one.
|
* cells, and calling evaluateFormulaCell on each one.
|
||||||
*/
|
*/
|
||||||
public static void evaluateAllFormulaCells(HSSFWorkbook wb) {
|
public static void evaluateAllFormulaCells(HSSFWorkbook wb) {
|
||||||
HSSFFormulaEvaluator evaluator = new HSSFFormulaEvaluator(wb);
|
HSSFFormulaEvaluator evaluator = new HSSFFormulaEvaluator(wb);
|
||||||
for(int i=0; i<wb.getNumberOfSheets(); i++) {
|
for(int i=0; i<wb.getNumberOfSheets(); i++) {
|
||||||
HSSFSheet sheet = wb.getSheetAt(i);
|
HSSFSheet sheet = wb.getSheetAt(i);
|
||||||
|
|
||||||
for (Iterator rit = sheet.rowIterator(); rit.hasNext();) {
|
for (Iterator rit = sheet.rowIterator(); rit.hasNext();) {
|
||||||
HSSFRow r = (HSSFRow)rit.next();
|
HSSFRow r = (HSSFRow)rit.next();
|
||||||
|
|
||||||
for (Iterator cit = r.cellIterator(); cit.hasNext();) {
|
for (Iterator cit = r.cellIterator(); cit.hasNext();) {
|
||||||
HSSFCell c = (HSSFCell)cit.next();
|
HSSFCell c = (HSSFCell)cit.next();
|
||||||
if (c.getCellType() == HSSFCell.CELL_TYPE_FORMULA)
|
if (c.getCellType() == HSSFCell.CELL_TYPE_FORMULA)
|
||||||
evaluator.evaluateFormulaCell(c);
|
evaluator.evaluateFormulaCell(c);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns a CellValue wrapper around the supplied ValueEval instance.
|
* Returns a CellValue wrapper around the supplied ValueEval instance.
|
||||||
* @param eval
|
* @param eval
|
||||||
*/
|
*/
|
||||||
private CellValue evaluateFormulaCellValue(Cell cell) {
|
private CellValue evaluateFormulaCellValue(Cell cell) {
|
||||||
ValueEval eval = _bookEvaluator.evaluate(new HSSFEvaluationCell((HSSFCell)cell));
|
ValueEval eval = _bookEvaluator.evaluate(new HSSFEvaluationCell((HSSFCell)cell));
|
||||||
if (eval instanceof NumberEval) {
|
if (eval instanceof NumberEval) {
|
||||||
NumberEval ne = (NumberEval) eval;
|
NumberEval ne = (NumberEval) eval;
|
||||||
return new CellValue(ne.getNumberValue());
|
return new CellValue(ne.getNumberValue());
|
||||||
}
|
}
|
||||||
if (eval instanceof BoolEval) {
|
if (eval instanceof BoolEval) {
|
||||||
BoolEval be = (BoolEval) eval;
|
BoolEval be = (BoolEval) eval;
|
||||||
return CellValue.valueOf(be.getBooleanValue());
|
return CellValue.valueOf(be.getBooleanValue());
|
||||||
}
|
}
|
||||||
if (eval instanceof StringEval) {
|
if (eval instanceof StringEval) {
|
||||||
StringEval ne = (StringEval) eval;
|
StringEval ne = (StringEval) eval;
|
||||||
return new CellValue(ne.getStringValue());
|
return new CellValue(ne.getStringValue());
|
||||||
}
|
}
|
||||||
if (eval instanceof ErrorEval) {
|
if (eval instanceof ErrorEval) {
|
||||||
return CellValue.getError(((ErrorEval)eval).getErrorCode());
|
return CellValue.getError(((ErrorEval)eval).getErrorCode());
|
||||||
}
|
}
|
||||||
throw new RuntimeException("Unexpected eval class (" + eval.getClass().getName() + ")");
|
throw new RuntimeException("Unexpected eval class (" + eval.getClass().getName() + ")");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -183,6 +183,15 @@ public interface Cell {
|
|||||||
|
|
||||||
RichTextString getRichStringCellValue();
|
RichTextString getRichStringCellValue();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the value of the cell as a string - for numeric cells we throw an exception
|
||||||
|
* <p>
|
||||||
|
* For blank cells we return an empty string.
|
||||||
|
* For formulaCells that are not string Formulas, we return empty String
|
||||||
|
* </p>
|
||||||
|
*/
|
||||||
|
String getStringCellValue();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* set a boolean value for the cell
|
* set a boolean value for the cell
|
||||||
*
|
*
|
||||||
|
@ -44,4 +44,11 @@ public interface CreationHelper {
|
|||||||
* Creates a new Hyperlink, of the given type
|
* Creates a new Hyperlink, of the given type
|
||||||
*/
|
*/
|
||||||
Hyperlink createHyperlink(int type);
|
Hyperlink createHyperlink(int type);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates FormulaEvaluator - an object that evaluates formula cells.
|
||||||
|
*
|
||||||
|
* @return a FormulaEvaluator instance
|
||||||
|
*/
|
||||||
|
FormulaEvaluator createFormulaEvaluator();
|
||||||
}
|
}
|
@ -31,7 +31,7 @@ public class XSSFCreationHelper implements CreationHelper {
|
|||||||
* Creates a new XSSFRichTextString for you.
|
* Creates a new XSSFRichTextString for you.
|
||||||
*/
|
*/
|
||||||
public XSSFRichTextString createRichTextString(String text) {
|
public XSSFRichTextString createRichTextString(String text) {
|
||||||
XSSFRichTextString rt =new XSSFRichTextString(text);
|
XSSFRichTextString rt = new XSSFRichTextString(text);
|
||||||
rt.setStylesTableReference(workbook.getStylesSource());
|
rt.setStylesTableReference(workbook.getStylesSource());
|
||||||
return rt;
|
return rt;
|
||||||
}
|
}
|
||||||
@ -43,4 +43,14 @@ public class XSSFCreationHelper implements CreationHelper {
|
|||||||
public XSSFHyperlink createHyperlink(int type) {
|
public XSSFHyperlink createHyperlink(int type) {
|
||||||
return new XSSFHyperlink(type);
|
return new XSSFHyperlink(type);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates a XSSFFormulaEvaluator, the object that evaluates formula cells.
|
||||||
|
*
|
||||||
|
* @return a XSSFFormulaEvaluator instance
|
||||||
|
*/
|
||||||
|
public XSSFFormulaEvaluator createFormulaEvaluator(){
|
||||||
|
return new XSSFFormulaEvaluator(workbook);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user