private final int _nColumns;
private final int _nRows;
+ protected AreaEvalBase(int firstRow, int firstColumn, int lastRow, int lastColumn) {
+ _firstColumn = firstColumn;
+ _firstRow = firstRow;
+ _lastColumn = lastColumn;
+ _lastRow = lastRow;
+
+ _nColumns = _lastColumn - _firstColumn + 1;
+ _nRows = _lastRow - _firstRow + 1;
+ }
+
protected AreaEvalBase(AreaI ptg) {
_firstRow = ptg.getFirstRow();
_firstColumn = ptg.getFirstColumn();
_lastRow = ptg.getLastRow();
_lastColumn = ptg.getLastColumn();
-
+
_nColumns = _lastColumn - _firstColumn + 1;
_nRows = _lastRow - _firstRow + 1;
}
public final ValueEval getValueAt(int row, int col) {
int rowOffsetIx = row - _firstRow;
int colOffsetIx = col - _firstColumn;
-
+
if(rowOffsetIx < 0 || rowOffsetIx >= _nRows) {
- throw new IllegalArgumentException("Specified row index (" + row
+ throw new IllegalArgumentException("Specified row index (" + row
+ ") is outside the allowed range (" + _firstRow + ".." + _lastRow + ")");
}
if(colOffsetIx < 0 || colOffsetIx >= _nColumns) {
- throw new IllegalArgumentException("Specified column index (" + col
+ throw new IllegalArgumentException("Specified column index (" + col
+ ") is outside the allowed range (" + _firstColumn + ".." + col + ")");
}
return getRelativeValue(rowOffsetIx, colOffsetIx);
}
public final boolean contains(int row, int col) {
- return _firstRow <= row && _lastRow >= row
+ return _firstRow <= row && _lastRow >= row
&& _firstColumn <= col && _lastColumn >= col;
}
/**
*
- * @author Josh Micich
+ * @author Josh Micich
*/
final class LazyAreaEval extends AreaEvalBase {
private final SheetRefEvaluator _evaluator;
- public LazyAreaEval(AreaI ptg, SheetRefEvaluator evaluator) {
+ LazyAreaEval(AreaI ptg, SheetRefEvaluator evaluator) {
super(ptg);
_evaluator = evaluator;
}
- public ValueEval getRelativeValue(int relativeRowIndex, int relativeColumnIndex) {
-
+ public LazyAreaEval(int firstRowIndex, int firstColumnIndex, int lastRowIndex,
+ int lastColumnIndex, SheetRefEvaluator evaluator) {
+ super(firstRowIndex, firstColumnIndex, lastRowIndex, lastColumnIndex);
+ _evaluator = evaluator;
+ }
+
+ public ValueEval getRelativeValue(int relativeRowIndex, int relativeColumnIndex) {
+
int rowIx = (relativeRowIndex + getFirstRow() ) & 0xFFFF;
int colIx = (relativeColumnIndex + getFirstColumn() ) & 0x00FF;
-
+
return _evaluator.getEvalForCell(rowIx, colIx);
}
package org.apache.poi.ss.formula;
import org.apache.poi.hssf.record.formula.AreaI;
-import org.apache.poi.hssf.record.formula.Ref3DPtg;
-import org.apache.poi.hssf.record.formula.RefPtg;
import org.apache.poi.hssf.record.formula.AreaI.OffsetArea;
import org.apache.poi.hssf.record.formula.eval.AreaEval;
import org.apache.poi.hssf.record.formula.eval.RefEvalBase;
}
_evaluator = sre;
}
- public LazyRefEval(RefPtg ptg, SheetRefEvaluator sre) {
- this(ptg.getRow(), ptg.getColumn(), sre);
- }
- public LazyRefEval(Ref3DPtg ptg, SheetRefEvaluator sre) {
- this(ptg.getRow(), ptg.getColumn(), sre);
- }
public ValueEval getInnerValueEval() {
return _evaluator.getEvalForCell(getRow(), getColumn());
package org.apache.poi.ss.formula;
-import org.apache.poi.hssf.record.formula.AreaI;
import org.apache.poi.hssf.record.formula.eval.AreaEval;
import org.apache.poi.hssf.record.formula.eval.ErrorEval;
import org.apache.poi.hssf.record.formula.eval.RefEval;
return _columnIndex;
}
- /* package */ SheetRefEvaluator createExternSheetRefEvaluator(ExternSheetReferenceToken ptg) {
- int externSheetIndex = ptg.getExternSheetIndex();
+ SheetRefEvaluator createExternSheetRefEvaluator(ExternSheetReferenceToken ptg) {
+ return createExternSheetRefEvaluator(ptg.getExternSheetIndex());
+ }
+ SheetRefEvaluator createExternSheetRefEvaluator(int externSheetIndex) {
ExternalSheet externalSheet = _workbook.getExternalSheet(externSheetIndex);
WorkbookEvaluator targetEvaluator;
int otherSheetIndex;
default:
throw new IllegalStateException("Unexpected reference classification of '" + refStrPart1 + "'.");
}
- return new LazyAreaEval(new AI(firstRow, firstCol, lastRow, lastCol), sre);
+ return new LazyAreaEval(firstRow, firstCol, lastRow, lastCol, sre);
}
private static int parseRowRef(String refStrPart) {
return Integer.parseInt(refStrPart) - 1;
}
- private static final class AI implements AreaI {
-
- private final int _fr;
- private final int _lr;
- private final int _fc;
- private final int _lc;
-
- public AI(int fr, int fc, int lr, int lc) {
- _fr = Math.min(fr, lr);
- _lr = Math.max(fr, lr);
- _fc = Math.min(fc, lc);
- _lc = Math.max(fc, lc);
- }
- public int getFirstColumn() {
- return _fc;
- }
- public int getFirstRow() {
- return _fr;
- }
- public int getLastColumn() {
- return _lc;
- }
- public int getLastRow() {
- return _lr;
- }
- }
-
private static NameType classifyCellReference(String str, SpreadsheetVersion ssVersion) {
int len = str.length();
if (len < 1) {
public FreeRefFunction findUserDefinedFunction(String functionName) {
return _bookEvaluator.findUserDefinedFunction(functionName);
}
+
+ public ValueEval getRefEval(int rowIndex, int columnIndex) {
+ SheetRefEvaluator sre = getRefEvaluatorForCurrentSheet();
+ return new LazyRefEval(rowIndex, columnIndex, sre);
+ }
+ public ValueEval getRef3DEval(int rowIndex, int columnIndex, int extSheetIndex) {
+ SheetRefEvaluator sre = createExternSheetRefEvaluator(extSheetIndex);
+ return new LazyRefEval(rowIndex, columnIndex, sre);
+ }
+ public ValueEval getAreaEval(int firstRowIndex, int firstColumnIndex,
+ int lastRowIndex, int lastColumnIndex) {
+ SheetRefEvaluator sre = getRefEvaluatorForCurrentSheet();
+ return new LazyAreaEval(firstRowIndex, firstColumnIndex, lastRowIndex, lastColumnIndex, sre);
+ }
+ public ValueEval getArea3DEval(int firstRowIndex, int firstColumnIndex,
+ int lastRowIndex, int lastColumnIndex, int extSheetIndex) {
+ SheetRefEvaluator sre = createExternSheetRefEvaluator(extSheetIndex);
+ return new LazyAreaEval(firstRowIndex, firstColumnIndex, lastRowIndex, lastColumnIndex, sre);
+ }
}
return ErrorEval.REF_INVALID;
}
if (ptg instanceof Ref3DPtg) {
- Ref3DPtg refPtg = (Ref3DPtg) ptg;
- SheetRefEvaluator sre = ec.createExternSheetRefEvaluator(refPtg);
- return new LazyRefEval(refPtg, sre);
+ Ref3DPtg rptg = (Ref3DPtg) ptg;
+ return ec.getRef3DEval(rptg.getRow(), rptg.getColumn(), rptg.getExternSheetIndex());
}
if (ptg instanceof Area3DPtg) {
Area3DPtg aptg = (Area3DPtg) ptg;
- SheetRefEvaluator sre = ec.createExternSheetRefEvaluator(aptg);
- return new LazyAreaEval(aptg, sre);
+ return ec.getArea3DEval(aptg.getFirstRow(), aptg.getFirstColumn(), aptg.getLastRow(), aptg.getLastColumn(), aptg.getExternSheetIndex());
}
- SheetRefEvaluator sre = ec.getRefEvaluatorForCurrentSheet();
if (ptg instanceof RefPtg) {
- return new LazyRefEval(((RefPtg) ptg), sre);
+ RefPtg rptg = (RefPtg) ptg;
+ return ec.getRefEval(rptg.getRow(), rptg.getColumn());
}
if (ptg instanceof AreaPtg) {
- return new LazyAreaEval(((AreaPtg) ptg), sre);
+ AreaPtg aptg = (AreaPtg) ptg;
+ return ec.getAreaEval(aptg.getFirstRow(), aptg.getFirstColumn(), aptg.getLastRow(), aptg.getLastColumn());
}
if (ptg instanceof UnknownPtg) {