diff --git a/src/java/org/apache/poi/ss/formula/functions/Offset.java b/src/java/org/apache/poi/ss/formula/functions/Offset.java index b69b0990c..60409f6c6 100644 --- a/src/java/org/apache/poi/ss/formula/functions/Offset.java +++ b/src/java/org/apache/poi/ss/formula/functions/Offset.java @@ -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 index 000000000..5751e534a --- /dev/null +++ b/src/ooxml/testcases/org/apache/poi/xssf/TestXSSFOffset.java @@ -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()); + } + } +} diff --git a/src/testcases/org/apache/poi/ss/formula/functions/TestOffset.java b/src/testcases/org/apache/poi/ss/formula/functions/TestOffset.java index 81968663a..66a721163 100644 --- a/src/testcases/org/apache/poi/ss/formula/functions/TestOffset.java +++ b/src/testcases/org/apache/poi/ss/formula/functions/TestOffset.java @@ -17,12 +17,18 @@ 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()); + } + } }