diff --git a/src/documentation/content/xdocs/changes.xml b/src/documentation/content/xdocs/changes.xml index 9a1908d99..d138d1503 100644 --- a/src/documentation/content/xdocs/changes.xml +++ b/src/documentation/content/xdocs/changes.xml @@ -37,6 +37,7 @@ + 45031- added implementation for CHOOSE() function 46361 - resolve licensing issues around the HDGF resource file, chunks_parse_cmds.tbl 46410 - added implementation for TIME() function 46320 - added HSSFPictureData.getFormat() diff --git a/src/documentation/content/xdocs/status.xml b/src/documentation/content/xdocs/status.xml index c13cb6cf2..4f1aafec4 100644 --- a/src/documentation/content/xdocs/status.xml +++ b/src/documentation/content/xdocs/status.xml @@ -34,6 +34,7 @@ + 45031- added implementation for CHOOSE() function 46361 - resolve licensing issues around the HDGF resource file, chunks_parse_cmds.tbl 46410 - added implementation for TIME() function 46320 - added HSSFPictureData.getFormat() diff --git a/src/java/org/apache/poi/hssf/record/formula/functions/Choose.java b/src/java/org/apache/poi/hssf/record/formula/functions/Choose.java index e7acc93c4..2f3ac240a 100644 --- a/src/java/org/apache/poi/hssf/record/formula/functions/Choose.java +++ b/src/java/org/apache/poi/hssf/record/formula/functions/Choose.java @@ -1,25 +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. -*/ -/* - * Created on May 15, 2005 - * - */ +/* ==================================================================== + 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.hssf.record.formula.functions; -public class Choose extends NotImplementedFunction { +import org.apache.poi.hssf.record.formula.eval.ErrorEval; +import org.apache.poi.hssf.record.formula.eval.Eval; +import org.apache.poi.hssf.record.formula.eval.EvaluationException; +import org.apache.poi.hssf.record.formula.eval.OperandResolver; +import org.apache.poi.hssf.record.formula.eval.ValueEval; +/** + * + * @author Josh Micich + */ +public final class Choose implements Function { + + public Eval evaluate(Eval[] args, int srcRowIndex, short srcColumnIndex) { + if (args.length < 2) { + return ErrorEval.VALUE_INVALID; + } + + try { + ValueEval ev = OperandResolver.getSingleValue(args[0], srcRowIndex, srcColumnIndex); + int ix = OperandResolver.coerceValueToInt(ev); + if (ix < 1 || ix >= args.length) { + return ErrorEval.VALUE_INVALID; + } + return OperandResolver.getSingleValue(args[ix], srcRowIndex, srcColumnIndex); + } catch (EvaluationException e) { + return e.getErrorEval(); + } + } } diff --git a/src/testcases/org/apache/poi/hssf/data/FormulaEvalTestData.xls b/src/testcases/org/apache/poi/hssf/data/FormulaEvalTestData.xls index 344a072cd..29e474abb 100644 Binary files a/src/testcases/org/apache/poi/hssf/data/FormulaEvalTestData.xls and b/src/testcases/org/apache/poi/hssf/data/FormulaEvalTestData.xls differ