From 16d25b62aff7736e11c9e1c83cf183b2b98173d8 Mon Sep 17 00:00:00 2001 From: PJ Fanning Date: Tue, 3 Apr 2018 21:51:30 +0000 Subject: [PATCH] [bug-62254] update offset function to support optional offset values git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1828288 13f79535-47bb-0310-9956-ffa450edef68 --- .../poi/ss/formula/functions/Offset.java | 10 ++-- .../org/apache/poi/xssf/TestXSSFOffset.java | 48 +++++++++++++++++++ .../poi/ss/formula/functions/TestOffset.java | 22 +++++++++ 3 files changed, 76 insertions(+), 4 deletions(-) create mode 100644 src/ooxml/testcases/org/apache/poi/xssf/TestXSSFOffset.java 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 b69b0990c0..60409f6c65 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 0000000000..5751e534a8 --- /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 81968663a9..66a7211633 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()); + } + } } -- 2.39.5