diff options
author | Nick Burch <nick@apache.org> | 2012-12-28 03:34:18 +0000 |
---|---|---|
committer | Nick Burch <nick@apache.org> | 2012-12-28 03:34:18 +0000 |
commit | 413c5594fbc277c4db9ed467c4b9ce95f3dfcd04 (patch) | |
tree | c1cb160aa97de9a38bb3fd8eb168c8a2e09cdce1 /src/java/org/apache | |
parent | 20f20b04dd04576fb7e1fd18289d2b2a18cadd97 (diff) | |
download | poi-413c5594fbc277c4db9ed467c4b9ce95f3dfcd04.tar.gz poi-413c5594fbc277c4db9ed467c4b9ce95f3dfcd04.zip |
Patch from Johan Karlsteen for bug #53966 - Implement IfError AP function
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1426382 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'src/java/org/apache')
-rw-r--r-- | src/java/org/apache/poi/ss/formula/atp/AnalysisToolPak.java | 5 | ||||
-rw-r--r-- | src/java/org/apache/poi/ss/formula/atp/IfError.java | 65 |
2 files changed, 67 insertions, 3 deletions
diff --git a/src/java/org/apache/poi/ss/formula/atp/AnalysisToolPak.java b/src/java/org/apache/poi/ss/formula/atp/AnalysisToolPak.java index 51fda2648b..a9343e67a3 100644 --- a/src/java/org/apache/poi/ss/formula/atp/AnalysisToolPak.java +++ b/src/java/org/apache/poi/ss/formula/atp/AnalysisToolPak.java @@ -22,8 +22,7 @@ import org.apache.poi.ss.formula.udf.UDFFinder; import java.util.*; /** - * @author Josh Micich - * @author Petr Udalau - systematized work of add-in libraries and user defined functions. + * Analysis Toolpack Function Definitions */ public final class AnalysisToolPak implements UDFFinder { @@ -112,7 +111,7 @@ public final class AnalysisToolPak implements UDFFinder { r(m, "HEX2BIN", null); r(m, "HEX2DEC", null); r(m, "HEX2OCT", null); - r(m, "IFERROR", null); + r(m, "IFERROR", IfError.instance); r(m, "IMABS", null); r(m, "IMAGINARY", null); r(m, "IMARGUMENT", null); diff --git a/src/java/org/apache/poi/ss/formula/atp/IfError.java b/src/java/org/apache/poi/ss/formula/atp/IfError.java new file mode 100644 index 0000000000..5b7e21b8d0 --- /dev/null +++ b/src/java/org/apache/poi/ss/formula/atp/IfError.java @@ -0,0 +1,65 @@ +/* ==================================================================== + 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.ss.formula.atp; + +import org.apache.poi.ss.formula.OperationEvaluationContext; +import org.apache.poi.ss.formula.eval.ErrorEval; +import org.apache.poi.ss.formula.eval.EvaluationException; +import org.apache.poi.ss.formula.eval.ValueEval; +import org.apache.poi.ss.formula.functions.FreeRefFunction; +/** + * Implementation of 'Analysis Toolpak' Excel function IFERROR()<br/> + * + * Returns an error text if there is an error in the evaluation<p/> + * + * <b>Syntax</b><br/> + * <b>IFERROR</b>(<b>expression</b>, <b>string</b>) + * + * @author Johan Karlsteen + */ +final class IfError implements FreeRefFunction { + + public static final FreeRefFunction instance = new IfError(); + + private IfError() { + // enforce singleton + } + + public ValueEval evaluate(ValueEval[] args, OperationEvaluationContext ec) { + if (args.length != 2) { + return ErrorEval.VALUE_INVALID; + } + + ValueEval val; + try { + val = evaluateArgParity(args[0], args[1], ec.getRowIndex(), ec.getColumnIndex()); + } catch (EvaluationException e) { + return e.getErrorEval(); + } + + return val; + } + + private static ValueEval evaluateArgParity(ValueEval arg, ValueEval iferror, int srcCellRow, int srcCellCol) throws EvaluationException { + if(arg instanceof ErrorEval) { + return iferror; + } else { + return arg; + } + } +}
\ No newline at end of file |