public EvaluationCell getCell(int rowIndex, int columnIndex) {
SXSSFRow row = _xs.getRow(rowIndex);
if (row == null) {
+ if (rowIndex <= _xs.getLastFlushedRowNum()) {
+ throw new SXSSFFormulaEvaluator.RowFlushedException(rowIndex);
+ }
return null;
}
SXSSFCell cell = row.getCell(columnIndex);
package org.apache.poi.xssf.streaming;
+import org.apache.poi.ss.formula.EvaluationCell;
import org.apache.poi.ss.formula.EvaluationSheet;
+import org.apache.poi.ss.formula.FormulaParser;
+import org.apache.poi.ss.formula.FormulaType;
+import org.apache.poi.ss.formula.ptg.Ptg;
import org.apache.poi.xssf.usermodel.BaseXSSFEvaluationWorkbook;
/**
public EvaluationSheet getSheet(int sheetIndex) {
return new SXSSFEvaluationSheet(_uBook.getSheetAt(sheetIndex));
}
+
+ public Ptg[] getFormulaTokens(EvaluationCell evalCell) {
+ SXSSFCell cell = ((SXSSFEvaluationCell)evalCell).getSXSSFCell();
+ SXSSFEvaluationWorkbook frBook = SXSSFEvaluationWorkbook.create(_uBook);
+ return FormulaParser.parse(cell.getCellFormula(), frBook, FormulaType.CELL, _uBook.getSheetIndex(cell.getSheet()));
+ }
}\r
package org.apache.poi.xssf.streaming;
import org.apache.poi.ss.formula.EvaluationCell;
+import org.apache.poi.ss.formula.IStabilityClassifier;
+import org.apache.poi.ss.formula.WorkbookEvaluator;
+import org.apache.poi.ss.formula.udf.UDFFinder;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.util.POILogFactory;
private SXSSFWorkbook wb;
public SXSSFFormulaEvaluator(SXSSFWorkbook workbook) {
- super(workbook.getXSSFWorkbook());
+ this(workbook, null, null);
+ }
+ private SXSSFFormulaEvaluator(SXSSFWorkbook workbook, IStabilityClassifier stabilityClassifier, UDFFinder udfFinder) {
+ this(workbook, new WorkbookEvaluator(SXSSFEvaluationWorkbook.create(workbook), stabilityClassifier, udfFinder));
+ }
+ private SXSSFFormulaEvaluator(SXSSFWorkbook workbook, WorkbookEvaluator bookEvaluator) {
+ super(workbook.getXSSFWorkbook(), bookEvaluator);
this.wb = workbook;
}
+ /**
+ * @param stabilityClassifier used to optimise caching performance. Pass <code>null</code>
+ * for the (conservative) assumption that any cell may have its definition changed after
+ * evaluation begins.
+ * @param udfFinder pass <code>null</code> for default (AnalysisToolPak only)
+ */
+ public static SXSSFFormulaEvaluator create(SXSSFWorkbook workbook, IStabilityClassifier stabilityClassifier, UDFFinder udfFinder) {
+ return new SXSSFFormulaEvaluator(workbook, stabilityClassifier, udfFinder);
+ }
+
/**
* Turns a SXSSFCell into a SXSSFEvaluationCell
*/
// Check they're all available
for (int i=0; i<wb.getNumberOfSheets(); i++) {
SXSSFSheet s = wb.getSheetAt(i);
- if (s.isFlushed()) {
+ if (s.areAllRowsFlushed()) {
throw new SheetsFlushedException();
}
}
SXSSFSheet s = wb.getSheetAt(i);
// Check if any rows have already been flushed out
- int firstRowNum = s.getFirstRowNum();
- int firstAvailableRowNum = s.iterator().next().getRowNum();
- if (firstRowNum != firstAvailableRowNum) {
- if (skipOutOfWindow) throw new RowsFlushedException();
- logger.log(POILogger.INFO, "Rows from " + firstRowNum + " to" +
- (firstAvailableRowNum-1) + " have already been flushed, skipping");
+ int lastFlushedRowNum = s.getLastFlushedRowNum();
+ if (lastFlushedRowNum > -1) {
+ if (! skipOutOfWindow) throw new RowFlushedException(0);
+ logger.log(POILogger.INFO, "Rows up to " + lastFlushedRowNum + " have already been flushed, skipping");
}
// Evaluate what we have
super("One or more sheets have been flushed, cannot evaluate all cells");
}
}
- public static class RowsFlushedException extends IllegalStateException {
- protected RowsFlushedException() {
- super("One or more rows have been flushed, cannot evaluate all cells");
+ public static class RowFlushedException extends IllegalStateException {
+ protected RowFlushedException(int rowNum) {
+ super("Row " + rowNum + " has been flushed, cannot evaluate all cells");
}
}
}
private SheetDataWriter _writer;
private int _randomAccessWindowSize = SXSSFWorkbook.DEFAULT_WINDOW_SIZE;
private int outlineLevelRow = 0;
- private boolean flushed = false;
+ private int lastFlushedRowNumber = -1;
+ private boolean allFlushed = false;
public SXSSFSheet(SXSSFWorkbook workbook, XSSFSheet xSheet) throws IOException {
_workbook = workbook;
initialAllocationSize=10;
SXSSFRow newRow=new SXSSFRow(this,initialAllocationSize);
_rows.put(new Integer(rownum),newRow);
- flushed = false;
+ allFlushed = false;
if(_randomAccessWindowSize>=0&&_rows.size()>_randomAccessWindowSize)
{
try
* @param rowIndex the zero based row index to collapse
*/
private void collapseRow(int rowIndex) {
- SXSSFRow row = (SXSSFRow) getRow(rowIndex);
+ SXSSFRow row = getRow(rowIndex);
if(row == null) {
throw new IllegalArgumentException("Invalid row number("+ rowIndex + "). Row does not exist.");
} else {
// Hide all the columns until the end of the group
int lastRow = writeHidden(row, startRow, true);
- SXSSFRow lastRowObj = (SXSSFRow) getRow(lastRow);
+ SXSSFRow lastRowObj = getRow(lastRow);
if (lastRowObj != null) {
lastRowObj.setCollapsed(true);
} else {
}
int currentRow = rowIndex;
while (getRow(currentRow) != null) {
- if (((SXSSFRow) getRow(currentRow)).getOutlineLevel() < level)
+ if (getRow(currentRow).getOutlineLevel() < level)
return currentRow + 1;
currentRow--;
}
private int writeHidden(SXSSFRow xRow, int rowIndex, boolean hidden) {
int level = xRow.getOutlineLevel();
- SXSSFRow currRow = (SXSSFRow) getRow(rowIndex);
+ SXSSFRow currRow = getRow(rowIndex);
while (currRow != null && currRow.getOutlineLevel() >= level) {
currRow.setHidden(hidden);
rowIndex++;
- currRow = (SXSSFRow) getRow(rowIndex);
+ currRow = getRow(rowIndex);
}
return rowIndex;
}
/**
* Are all rows flushed to disk?
*/
- public boolean isFlushed() {
- return flushed;
+ public boolean areAllRowsFlushed() {
+ return allFlushed;
+ }
+ /**
+ * @return Last row number to be flushed to disk, or -1 if none flushed yet
+ */
+ public int getLastFlushedRowNum() {
+ return lastFlushedRowNumber;
}
/**
public void flushRows(int remaining) throws IOException
{
while(_rows.size() > remaining) flushOneRow();
- if (remaining == 0) flushed = true;
+ if (remaining == 0) allFlushed = true;
}
/**
SXSSFRow row = _rows.get(firstRowNum);
_writer.writeRow(rowIndex, row);
_rows.remove(firstRowNum);
+ lastFlushedRowNumber = rowIndex;
}
}
public void changeRowNum(SXSSFRow row, int newRowNum)
* @return true if the file was deleted, false if it wasn't.
*/
boolean dispose() throws IOException {
- if (!flushed) flushRows();
+ if (!allFlushed) flushRows();
return _writer.dispose();
}
int ix = namePtg.getIndex();
return new Name(_uBook.getNameAt(ix), ix, this);
}
- public Ptg[] getFormulaTokens(EvaluationCell evalCell) {
- XSSFCell cell = ((XSSFEvaluationCell)evalCell).getXSSFCell();
- XSSFEvaluationWorkbook frBook = XSSFEvaluationWorkbook.create(_uBook);
- return FormulaParser.parse(cell.getCellFormula(), frBook, FormulaType.CELL, _uBook.getSheetIndex(cell.getSheet()));
- }
public UDFFinder getUDFFinder(){
return _uBook.getUDFFinder();
package org.apache.poi.xssf.usermodel;
+import org.apache.poi.ss.formula.EvaluationCell;
import org.apache.poi.ss.formula.EvaluationSheet;
+import org.apache.poi.ss.formula.FormulaParser;
+import org.apache.poi.ss.formula.FormulaType;
+import org.apache.poi.ss.formula.ptg.Ptg;
/**
* Internal POI use only
public EvaluationSheet getSheet(int sheetIndex) {
return new XSSFEvaluationSheet(_uBook.getSheetAt(sheetIndex));
}
+
+ public Ptg[] getFormulaTokens(EvaluationCell evalCell) {
+ XSSFCell cell = ((XSSFEvaluationCell)evalCell).getXSSFCell();
+ XSSFEvaluationWorkbook frBook = XSSFEvaluationWorkbook.create(_uBook);
+ return FormulaParser.parse(cell.getCellFormula(), frBook, FormulaType.CELL, _uBook.getSheetIndex(cell.getSheet()));
+ }
}
this(workbook, null, null);
}
private XSSFFormulaEvaluator(XSSFWorkbook workbook, IStabilityClassifier stabilityClassifier, UDFFinder udfFinder) {
- _bookEvaluator = new WorkbookEvaluator(XSSFEvaluationWorkbook.create(workbook), stabilityClassifier, udfFinder);
+ this(workbook, new WorkbookEvaluator(XSSFEvaluationWorkbook.create(workbook), stabilityClassifier, udfFinder));
+ }
+ protected XSSFFormulaEvaluator(XSSFWorkbook workbook, WorkbookEvaluator bookEvaluator) {
+ _bookEvaluator = bookEvaluator;
_book = workbook;
}
import org.apache.poi.ss.usermodel.FormulaEvaluator;
import org.apache.poi.xssf.SXSSFITestDataProvider;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
-import org.junit.Ignore;
import org.junit.Test;
/**
* cell is in the current window, and all references
* from the cell are in the current window
*/
-@Ignore
public final class TestSXSSFFormulaEvaluation {
public static final SXSSFITestDataProvider _testDataProvider = SXSSFITestDataProvider.instance;
FormulaEvaluator eval = wb.getCreationHelper().createFormulaEvaluator();
- // References outside window will fail
s.createRow(0).createCell(0).setCellFormula("1+2");
s.createRow(1).createCell(0).setCellFormula("A21");
- try {
- eval.evaluateAll();
- fail("Evaluate All shouldn't work, as references outside the window");
- } catch(Exception e) {
- System.err.println(e); // TODO
- }
+ for (int i=2; i<19; i++) { s.createRow(i); }
- // Cells outside window will fail
- s.createRow(10).createCell(0).setCellFormula("A1+A2");
+ // Cells outside window will fail, whether referenced or not
+ s.createRow(19).createCell(0).setCellFormula("A1+A2");
s.createRow(20).createCell(0).setCellFormula("A1+A11+100");
try {
eval.evaluateAll();
fail("Evaluate All shouldn't work, as some cells outside the window");
- } catch(Exception e) {
- System.err.println(e); // TODO
+ } catch(SXSSFFormulaEvaluator.RowFlushedException e) {
+ // Expected
}
public void testEvaluateRefOutsideWindowFails() {
SXSSFWorkbook wb = new SXSSFWorkbook(5);
SXSSFSheet s = wb.createSheet();
+
s.createRow(0).createCell(0).setCellFormula("1+2");
+ assertEquals(false, s.areAllRowsFlushed());
+ assertEquals(-1, s.getLastFlushedRowNum());
+
+ for (int i=1; i<=19; i++) { s.createRow(i); }
Cell c = s.createRow(20).createCell(0);
c.setCellFormula("A1+100");
+ assertEquals(false, s.areAllRowsFlushed());
+ assertEquals(15, s.getLastFlushedRowNum());
+
FormulaEvaluator eval = wb.getCreationHelper().createFormulaEvaluator();
try {
eval.evaluateFormulaCell(c);
fail("Evaluate shouldn't work, as reference outside the window");
- } catch(Exception e) {
- System.err.println(e); // TODO
+ } catch(SXSSFFormulaEvaluator.RowFlushedException e) {
+ // Expected
}
}
c = s.createRow(1).createCell(0);
c.setCellFormula("CONCATENATE(\"hello\",\" \",\"world\")");
- assertEquals("", c.getStringCellValue());
eval.evaluateFormulaCell(c);
assertEquals("hello world", c.getStringCellValue());
}