]> source.dussan.org Git - poi.git/commitdiff
add tests
authorPJ Fanning <fanningpj@apache.org>
Mon, 21 Feb 2022 14:02:37 +0000 (14:02 +0000)
committerPJ Fanning <fanningpj@apache.org>
Mon, 21 Feb 2022 14:02:37 +0000 (14:02 +0000)
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1898277 13f79535-47bb-0310-9956-ffa450edef68

poi/src/main/java/org/apache/poi/ss/formula/OperationEvaluationContext.java
poi/src/test/java/org/apache/poi/ss/formula/functions/TestIndirect.java

index bdadbfad42a2ce4f286f630c8a80675781dd7e1a..12e88abd5144559ee17c8eba22acac1c2ce0bdae 100644 (file)
@@ -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) {
index 3a7f24f1f4abd792c4340f694ed5738286096f34..45f960e945783078425cebd95cd122c3457a3f2d 100644 (file)
@@ -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);
@@ -190,6 +188,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();