]> source.dussan.org Git - poi.git/commitdiff
[bug-62254] update offset function to support optional offset values
authorPJ Fanning <fanningpj@apache.org>
Tue, 3 Apr 2018 21:51:30 +0000 (21:51 +0000)
committerPJ Fanning <fanningpj@apache.org>
Tue, 3 Apr 2018 21:51:30 +0000 (21:51 +0000)
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1828288 13f79535-47bb-0310-9956-ffa450edef68

src/java/org/apache/poi/ss/formula/functions/Offset.java
src/ooxml/testcases/org/apache/poi/xssf/TestXSSFOffset.java [new file with mode: 0644]
src/testcases/org/apache/poi/ss/formula/functions/TestOffset.java

index b69b0990c06c945f465ffa6b566a5d447b927e62..60409f6c6594d6d7bf9aff1b74b51cb6e36da641 100644 (file)
@@ -163,18 +163,20 @@ public final class Offset implements Function {
 
        @SuppressWarnings("fallthrough")
        public ValueEval evaluate(ValueEval[] args, int srcCellRow, int srcCellCol) {
-               if(args.length < 3 || args.length > 5) {
+               if(args.length < 1 || args.length > 5) {
                        return ErrorEval.VALUE_INVALID;
                }
 
                try {
                        BaseRef baseRef = evaluateBaseRef(args[0]);
-                       int rowOffset = evaluateIntArg(args[1], srcCellRow, srcCellCol);
-                       int columnOffset = evaluateIntArg(args[2], srcCellRow, srcCellCol);
+                       // optional arguments
+                       // If offsets are omitted, it is assumed to be 0.
+                       int rowOffset = (args[1] instanceof MissingArgEval) ? 0 : evaluateIntArg(args[1], srcCellRow, srcCellCol);
+                       int columnOffset = (args[2] instanceof MissingArgEval) ? 0 : evaluateIntArg(args[2], srcCellRow, srcCellCol);
                        int height = baseRef.getHeight();
                        int width = baseRef.getWidth();
                        // optional arguments
-                       // If height or width is omitted, it is assumed to be the same height or width as reference.
+                       // If height or width are omitted, it is assumed to be the same height or width as reference.
                        switch(args.length) {
                                case 5:
                                        if(!(args[4] instanceof MissingArgEval)) {
diff --git a/src/ooxml/testcases/org/apache/poi/xssf/TestXSSFOffset.java b/src/ooxml/testcases/org/apache/poi/xssf/TestXSSFOffset.java
new file mode 100644 (file)
index 0000000..5751e53
--- /dev/null
@@ -0,0 +1,48 @@
+/* ====================================================================
+   Licensed to the Apache Software Foundation (ASF) under one or more
+   contributor license agreements.  See the NOTICE file distributed with
+   this work for additional information regarding copyright ownership.
+   The ASF licenses this file to You under the Apache License, Version 2.0
+   (the "License"); you may not use this file except in compliance with
+   the License.  You may obtain a copy of the License at
+
+       http://www.apache.org/licenses/LICENSE-2.0
+
+   Unless required by applicable law or agreed to in writing, software
+   distributed under the License is distributed on an "AS IS" BASIS,
+   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+   See the License for the specific language governing permissions and
+   limitations under the License.
+==================================================================== */
+
+package org.apache.poi.xssf;
+
+import java.io.IOException;
+
+import org.apache.poi.ss.usermodel.Cell;
+import org.apache.poi.ss.usermodel.CellType;
+import org.apache.poi.ss.usermodel.Workbook;
+import org.apache.poi.xssf.usermodel.XSSFWorkbook;
+import org.junit.Test;
+
+import static org.junit.Assert.assertEquals;
+
+public class TestXSSFOffset {
+
+    @Test
+    public void testOffsetWithEmpty23Arguments() throws IOException {
+        try (Workbook workbook = new XSSFWorkbook()) {
+            Cell cell = workbook.createSheet().createRow(0).createCell(0);
+            cell.setCellFormula("OFFSET(B1,,)");
+
+            String value = "EXPECTED_VALUE";
+            Cell valueCell = cell.getRow().createCell(1);
+            valueCell.setCellValue(value);
+
+            workbook.getCreationHelper().createFormulaEvaluator().evaluateAll();
+
+            assertEquals(CellType.STRING, cell.getCachedFormulaResultType());
+            assertEquals(value, cell.getStringCellValue());
+        }
+    }
+}
index 81968663a9c85a62fac72cf7db1755032a1a09e9..66a7211633eeebf47502e8888c242b14cc3ef222 100644 (file)
 
 package org.apache.poi.ss.formula.functions;
 
+import java.io.IOException;
+
 import junit.framework.AssertionFailedError;
 import junit.framework.TestCase;
 
+import org.apache.poi.hssf.usermodel.HSSFWorkbook;
 import org.apache.poi.ss.formula.eval.EvaluationException;
 import org.apache.poi.ss.formula.eval.NumberEval;
 import org.apache.poi.ss.formula.functions.Offset.LinearOffsetRange;
+import org.apache.poi.ss.usermodel.Cell;
+import org.apache.poi.ss.usermodel.CellType;
+import org.apache.poi.ss.usermodel.Workbook;
 
 /**
  * Tests for OFFSET function implementation
@@ -96,4 +102,20 @@ public final class TestOffset extends TestCase {
                assertTrue(lor.isOutOfBounds(0, 16383));
                assertFalse(lor.isOutOfBounds(0, 65535));
        }
+
+       public void testOffsetWithEmpty23Arguments() throws IOException {
+               try (Workbook workbook = new HSSFWorkbook()) {
+                       Cell cell = workbook.createSheet().createRow(0).createCell(0);
+                       cell.setCellFormula("OFFSET(B1,,)");
+
+                       String value = "EXPECTED_VALUE";
+                       Cell valueCell = cell.getRow().createCell(1);
+                       valueCell.setCellValue(value);
+
+                       workbook.getCreationHelper().createFormulaEvaluator().evaluateAll();
+
+                       assertEquals(CellType.STRING, cell.getCachedFormulaResultType());
+                       assertEquals(value, cell.getStringCellValue());
+               }
+       }
 }