diff options
-rw-r--r-- | poi/src/main/java/org/apache/poi/ss/formula/OperationEvaluationContext.java | 43 | ||||
-rw-r--r-- | poi/src/test/java/org/apache/poi/ss/formula/functions/TestIndirect.java | 26 |
2 files changed, 64 insertions, 5 deletions
diff --git a/poi/src/main/java/org/apache/poi/ss/formula/OperationEvaluationContext.java b/poi/src/main/java/org/apache/poi/ss/formula/OperationEvaluationContext.java index bdadbfad42..12e88abd51 100644 --- a/poi/src/main/java/org/apache/poi/ss/formula/OperationEvaluationContext.java +++ b/poi/src/main/java/org/apache/poi/ss/formula/OperationEvaluationContext.java @@ -242,8 +242,49 @@ public final class OperationEvaluationContext { // no ':' switch (part1refType) { case COLUMN: + if (isA1Style) { + return ErrorEval.REF_INVALID; + } else { + try { + String upRef = refStrPart1.toUpperCase(LocaleUtil.getUserLocale()); + int cpos = upRef.indexOf('C'); + String cval = refStrPart1.substring(cpos + 1).trim(); + int absoluteC; + if (cval.startsWith("[") && cval.endsWith("]")) { + int relativeC = Integer.parseInt(cval.substring(1, cval.length() - 1).trim()); + absoluteC = getColumnIndex() + relativeC; + } else if (!cval.isEmpty()) { + absoluteC = Integer.parseInt(cval) - 1; + } else { + return ErrorEval.REF_INVALID; + } + return new LazyAreaEval(0, absoluteC, ssVersion.getLastRowIndex(), absoluteC, sre); + } catch (Exception e) { + return ErrorEval.REF_INVALID; + } + } case ROW: - return ErrorEval.REF_INVALID; + if (isA1Style) { + return ErrorEval.REF_INVALID; + } else { + try { + String upRef = refStrPart1.toUpperCase(LocaleUtil.getUserLocale()); + int rpos = upRef.indexOf('R'); + String rval = refStrPart1.substring(rpos + 1).trim(); + int absoluteR; + if (rval.startsWith("[") && rval.endsWith("]")) { + int relativeR = Integer.parseInt(rval.substring(1, rval.length() - 1).trim()); + absoluteR = getRowIndex() + relativeR; + } else if (!rval.isEmpty()) { + absoluteR = Integer.parseInt(rval) - 1; + } else { + return ErrorEval.REF_INVALID; + } + return new LazyAreaEval(absoluteR, 0, absoluteR, ssVersion.getLastColumnIndex(), sre); + } catch (Exception e) { + return ErrorEval.REF_INVALID; + } + } case CELL: CellReference cr; if (isA1Style) { diff --git a/poi/src/test/java/org/apache/poi/ss/formula/functions/TestIndirect.java b/poi/src/test/java/org/apache/poi/ss/formula/functions/TestIndirect.java index 3a7f24f1f4..45f960e945 100644 --- a/poi/src/test/java/org/apache/poi/ss/formula/functions/TestIndirect.java +++ b/poi/src/test/java/org/apache/poi/ss/formula/functions/TestIndirect.java @@ -169,6 +169,8 @@ final class TestIndirect { HSSFFormulaEvaluator feA = new HSSFFormulaEvaluator(wbA); // non-error cases + confirm(feA, c, "INDIRECT(\"R1C1\", FALSE)", 11); + confirm(feA, c, "INDIRECT(\"R1C4\", FALSE)", 14); confirm(feA, c, "INDIRECT(\"R2C3\", FALSE)", 23); confirm(feA, c, "INDIRECT(\"r2c3\", FALSE)", 23); confirm(feA, c, "INDIRECT(\"R[-4]C[0]\", FALSE)", 23); @@ -179,10 +181,6 @@ final class TestIndirect { confirm(feA, c, "SUM(INDIRECT(\"Sheet2!r1c2:r3c3\", FALSE))", 351); // area ref confirm(feA, c, "SUM(INDIRECT(\"Sheet2! R1C2 : R3C3 \", FALSE))", 351); // spaces in area ref - //scenarios yet to support - //R[-4] -- supports getting full row - //C[-4] -- supports getting full column - // simple error propagation: confirm(feA, c, "INDIRECT(\"'Sheet1 '!R3C4\", FALSE)", ErrorEval.REF_INVALID); @@ -191,6 +189,26 @@ final class TestIndirect { } @Test + void testR1C1FullColumn() throws Exception { + try (HSSFWorkbook wbA = createWBA()) { + HSSFCell c = wbA.getSheetAt(0).getRow(0).createCell(20); + HSSFFormulaEvaluator feA = new HSSFFormulaEvaluator(wbA); + confirm(feA, c, "INDIRECT(\"C[-17]\", FALSE)", 14.0); + confirm(feA, c, "INDIRECT(\"C4\", FALSE)", 14.0); + } + } + + @Test + void testR1C1FullRow() throws Exception { + try (HSSFWorkbook wbA = createWBA()) { + HSSFCell c = wbA.getSheetAt(0).createRow(100).createCell(0); + HSSFFormulaEvaluator feA = new HSSFFormulaEvaluator(wbA); + confirm(feA, c, "INDIRECT(\"R[-100]\", FALSE)", 11.0); + confirm(feA, c, "INDIRECT(\"R1\", FALSE)", 11.0); + } + } + + @Test void testMultipleWorkbooks() throws Exception { HSSFWorkbook wbA = createWBA(); HSSFCell cellA = wbA.getSheetAt(0).createRow(10).createCell(0); |