[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
This commit is contained in:
PJ Fanning 2018-04-03 21:51:30 +00:00
parent 2ce07693c5
commit 16d25b62af
3 changed files with 76 additions and 4 deletions

View File

@ -163,18 +163,20 @@ public final class Offset implements Function {
@SuppressWarnings("fallthrough") @SuppressWarnings("fallthrough")
public ValueEval evaluate(ValueEval[] args, int srcCellRow, int srcCellCol) { 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; return ErrorEval.VALUE_INVALID;
} }
try { try {
BaseRef baseRef = evaluateBaseRef(args[0]); BaseRef baseRef = evaluateBaseRef(args[0]);
int rowOffset = evaluateIntArg(args[1], srcCellRow, srcCellCol); // optional arguments
int columnOffset = evaluateIntArg(args[2], srcCellRow, srcCellCol); // 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 height = baseRef.getHeight();
int width = baseRef.getWidth(); int width = baseRef.getWidth();
// optional arguments // 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) { switch(args.length) {
case 5: case 5:
if(!(args[4] instanceof MissingArgEval)) { if(!(args[4] instanceof MissingArgEval)) {

View File

@ -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());
}
}
}

View File

@ -17,12 +17,18 @@
package org.apache.poi.ss.formula.functions; package org.apache.poi.ss.formula.functions;
import java.io.IOException;
import junit.framework.AssertionFailedError; import junit.framework.AssertionFailedError;
import junit.framework.TestCase; 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.EvaluationException;
import org.apache.poi.ss.formula.eval.NumberEval; import org.apache.poi.ss.formula.eval.NumberEval;
import org.apache.poi.ss.formula.functions.Offset.LinearOffsetRange; 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 * Tests for OFFSET function implementation
@ -96,4 +102,20 @@ public final class TestOffset extends TestCase {
assertTrue(lor.isOutOfBounds(0, 16383)); assertTrue(lor.isOutOfBounds(0, 16383));
assertFalse(lor.isOutOfBounds(0, 65535)); 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());
}
}
} }