poi-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From j...@apache.org
Subject svn commit: r692614 - in /poi/trunk/src: java/org/apache/poi/hssf/record/formula/atp/ testcases/org/apache/poi/hssf/data/ testcases/org/apache/poi/hssf/record/formula/ testcases/org/apache/poi/hssf/record/formula/atp/
Date Sat, 06 Sep 2008 06:04:02 GMT
Author: josh
Date: Fri Sep  5 23:04:01 2008
New Revision: 692614

URL: http://svn.apache.org/viewvc?rev=692614&view=rev
Log:
Minor fixes to YEARFRAC(). Added ISEVEN() and ISODD(). Added test cases.

Added:
    poi/trunk/src/java/org/apache/poi/hssf/record/formula/atp/ParityFunction.java
Modified:
    poi/trunk/src/java/org/apache/poi/hssf/record/formula/atp/AnalysisToolPak.java
    poi/trunk/src/java/org/apache/poi/hssf/record/formula/atp/YearFrac.java
    poi/trunk/src/testcases/org/apache/poi/hssf/data/externalFunctionExample.xls
    poi/trunk/src/testcases/org/apache/poi/hssf/record/formula/TestExternalFunctionFormulas.java
    poi/trunk/src/testcases/org/apache/poi/hssf/record/formula/atp/TestYearFracCalculator.java

Modified: poi/trunk/src/java/org/apache/poi/hssf/record/formula/atp/AnalysisToolPak.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/java/org/apache/poi/hssf/record/formula/atp/AnalysisToolPak.java?rev=692614&r1=692613&r2=692614&view=diff
==============================================================================
--- poi/trunk/src/java/org/apache/poi/hssf/record/formula/atp/AnalysisToolPak.java (original)
+++ poi/trunk/src/java/org/apache/poi/hssf/record/formula/atp/AnalysisToolPak.java Fri Sep
 5 23:04:01 2008
@@ -109,8 +109,8 @@
 		r(m, "IMSUB", null);
 		r(m, "IMSUM", null);
 		r(m, "INTRATE", null);
-		r(m, "ISEVEN", null);
-		r(m, "ISODD", null);
+		r(m, "ISEVEN", ParityFunction.IS_EVEN);
+		r(m, "ISODD", ParityFunction.IS_ODD);
 		r(m, "LCM", null);
 		r(m, "MDURATION", null);
 		r(m, "MROUND", null);

Added: poi/trunk/src/java/org/apache/poi/hssf/record/formula/atp/ParityFunction.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/java/org/apache/poi/hssf/record/formula/atp/ParityFunction.java?rev=692614&view=auto
==============================================================================
--- poi/trunk/src/java/org/apache/poi/hssf/record/formula/atp/ParityFunction.java (added)
+++ poi/trunk/src/java/org/apache/poi/hssf/record/formula/atp/ParityFunction.java Fri Sep
 5 23:04:01 2008
@@ -0,0 +1,74 @@
+/* ====================================================================
+   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.atp;
+
+import org.apache.poi.hssf.record.formula.eval.BlankEval;
+import org.apache.poi.hssf.record.formula.eval.BoolEval;
+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;
+import org.apache.poi.hssf.record.formula.functions.FreeRefFunction;
+import org.apache.poi.hssf.usermodel.HSSFSheet;
+import org.apache.poi.hssf.usermodel.HSSFWorkbook;
+/**
+ * Implementation of Excel 'Analysis ToolPak' function ISEVEN() ISODD()<br/>
+ * 
+ * @author Josh Micich
+ */
+final class ParityFunction implements FreeRefFunction {
+
+	public static final FreeRefFunction IS_EVEN = new ParityFunction(0);
+	public static final FreeRefFunction IS_ODD = new ParityFunction(1);
+	private final int _desiredParity;
+	
+	private ParityFunction(int desiredParity) {
+		_desiredParity = desiredParity;
+	}
+
+	public ValueEval evaluate(Eval[] args, int srcCellRow, short srcCellCol, HSSFWorkbook workbook,
+			HSSFSheet sheet) {
+		if (args.length != 1) {
+			return ErrorEval.VALUE_INVALID;  
+		}
+		
+		int val;
+		try {
+			val = evaluateArgParity(args[0], srcCellRow, srcCellCol);
+		} catch (EvaluationException e) {
+			return e.getErrorEval();
+		}
+
+		return BoolEval.valueOf(val == _desiredParity);
+	}
+
+	private static int evaluateArgParity(Eval arg, int srcCellRow, short srcCellCol) throws
EvaluationException {
+		ValueEval ve = OperandResolver.getSingleValue(arg, srcCellRow, srcCellCol);
+		
+		if (ve == BlankEval.INSTANCE) {
+			return 0;
+		}
+		double d = OperandResolver.coerceValueToDouble(ve);
+		if (d < 0) {
+			d = -d;
+		}
+		long v = (long) Math.floor(d);
+		return (int) (v & 0x0001);
+	}
+}

Modified: poi/trunk/src/java/org/apache/poi/hssf/record/formula/atp/YearFrac.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/java/org/apache/poi/hssf/record/formula/atp/YearFrac.java?rev=692614&r1=692613&r2=692614&view=diff
==============================================================================
--- poi/trunk/src/java/org/apache/poi/hssf/record/formula/atp/YearFrac.java (original)
+++ poi/trunk/src/java/org/apache/poi/hssf/record/formula/atp/YearFrac.java Fri Sep  5 23:04:01
2008
@@ -21,6 +21,7 @@
 import java.util.GregorianCalendar;
 import java.util.regex.Pattern;
 
+import org.apache.poi.hssf.record.formula.eval.BlankEval;
 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;
@@ -96,6 +97,9 @@
 			Calendar date = parseDate(strVal);
 			return HSSFDateUtil.getExcelDate(date, false);
 		}
+		if (ve instanceof BlankEval) {
+			return 0.0;
+		}
 		return OperandResolver.coerceValueToDouble(ve);
 	}
 
@@ -120,7 +124,7 @@
 		} catch (NumberFormatException e) {
 			throw new EvaluationException(ErrorEval.VALUE_INVALID);
 		}
-		if (f0<0 || f1<0 || f2<0 || f0>12 || f1>12 || f2>12) {
+		if (f0<0 || f1<0 || f2<0 || (f0>12 && f1>12 && f2>12))
{
 			// easy to see this cannot be a valid date
 			throw new EvaluationException(ErrorEval.VALUE_INVALID);
 		}
@@ -150,6 +154,7 @@
 		if (day <1 || day>cal.getActualMaximum(Calendar.DAY_OF_MONTH)) {
 			throw new EvaluationException(ErrorEval.VALUE_INVALID);
 		}
+		cal.set(Calendar.DAY_OF_MONTH, day);
 		return cal;
 	}
 

Modified: poi/trunk/src/testcases/org/apache/poi/hssf/data/externalFunctionExample.xls
URL: http://svn.apache.org/viewvc/poi/trunk/src/testcases/org/apache/poi/hssf/data/externalFunctionExample.xls?rev=692614&r1=692613&r2=692614&view=diff
==============================================================================
Binary files - no diff available.

Modified: poi/trunk/src/testcases/org/apache/poi/hssf/record/formula/TestExternalFunctionFormulas.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/testcases/org/apache/poi/hssf/record/formula/TestExternalFunctionFormulas.java?rev=692614&r1=692613&r2=692614&view=diff
==============================================================================
--- poi/trunk/src/testcases/org/apache/poi/hssf/record/formula/TestExternalFunctionFormulas.java
(original)
+++ poi/trunk/src/testcases/org/apache/poi/hssf/record/formula/TestExternalFunctionFormulas.java
Fri Sep  5 23:04:01 2008
@@ -74,9 +74,18 @@
 	public void testEvaluate() {
 		HSSFWorkbook wb = HSSFTestDataSamples.openSampleWorkbook("externalFunctionExample.xls");
 		HSSFSheet sheet = wb.getSheetAt(0);
-		HSSFCell cell = sheet.getRow(0).getCell(0);
 		HSSFFormulaEvaluator fe = new HSSFFormulaEvaluator(sheet, wb);
-		CellValue evalResult = fe.evaluate(cell);
-		evalResult.toString();
+		confirmCellEval(sheet, 0, 0, fe, "YEARFRAC(B1,C1)", 29.0/90.0);
+		confirmCellEval(sheet, 1, 0, fe, "YEARFRAC(B2,C2)", 0.0);
+		confirmCellEval(sheet, 2, 0, fe, "IF(ISEVEN(3),1.2,1.6)", 1.6);
+		confirmCellEval(sheet, 3, 0, fe, "IF(ISODD(3),1.2,1.6)", 1.2);
+	}
+
+	private static void confirmCellEval(HSSFSheet sheet, int rowIx, int colIx, 
+			HSSFFormulaEvaluator fe, String expectedFormula, double expectedResult) {
+		HSSFCell cell = sheet.getRow(rowIx).getCell(colIx);
+		assertEquals(expectedFormula, cell.getCellFormula());
+		CellValue cv = fe.evaluate(cell);
+		assertEquals(expectedResult, cv.getNumberValue(), 0.0);
 	}
 }

Modified: poi/trunk/src/testcases/org/apache/poi/hssf/record/formula/atp/TestYearFracCalculator.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/testcases/org/apache/poi/hssf/record/formula/atp/TestYearFracCalculator.java?rev=692614&r1=692613&r2=692614&view=diff
==============================================================================
--- poi/trunk/src/testcases/org/apache/poi/hssf/record/formula/atp/TestYearFracCalculator.java
(original)
+++ poi/trunk/src/testcases/org/apache/poi/hssf/record/formula/atp/TestYearFracCalculator.java
Fri Sep  5 23:04:01 2008
@@ -38,6 +38,7 @@
 		confirm(md(1999, 3, 31), md(1999, 4, 3), 1, 0.008219178);
 		confirm(md(1999, 4, 5), md(1999, 4, 8), 1, 0.008219178);
 		confirm(md(1999, 4, 4), md(1999, 4, 7), 1, 0.008219178);
+		confirm(md(2000, 2, 5), md(2000, 6, 1), 0, 0.322222222);
 	}
 
 	private void confirm(double startDate, double endDate, int basis, double expectedValue)
{



---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@poi.apache.org
For additional commands, e-mail: commits-help@poi.apache.org


Mime
View raw message