From 8f2e06a754974aba79f64d604ede3e9573dd5c04 Mon Sep 17 00:00:00 2001 From: Josh Micich Date: Sun, 22 Nov 2009 05:49:51 +0000 Subject: [PATCH] minor fix to T() function, junit added git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@883039 13f79535-47bb-0310-9956-ffa450edef68 --- .../poi/hssf/record/formula/functions/T.java | 14 ++++++++++++-- .../record/formula/functions/TestTFunc.java | 17 +++++++++++++++++ 2 files changed, 29 insertions(+), 2 deletions(-) diff --git a/src/java/org/apache/poi/hssf/record/formula/functions/T.java b/src/java/org/apache/poi/hssf/record/formula/functions/T.java index 54e31f8b48..e4b43c0d1c 100644 --- a/src/java/org/apache/poi/hssf/record/formula/functions/T.java +++ b/src/java/org/apache/poi/hssf/record/formula/functions/T.java @@ -17,11 +17,19 @@ package org.apache.poi.hssf.record.formula.functions; +import org.apache.poi.hssf.record.formula.eval.AreaEval; import org.apache.poi.hssf.record.formula.eval.ErrorEval; import org.apache.poi.hssf.record.formula.eval.RefEval; import org.apache.poi.hssf.record.formula.eval.StringEval; import org.apache.poi.hssf.record.formula.eval.ValueEval; +/** + * Implementation of Excel T() function + *

+ * If the argument is a text or error value it is returned unmodified. All other argument types + * cause an empty string result. If the argument is an area, the first (top-left) cell is used + * (regardless of the coordinates of the evaluating formula cell). + */ public final class T implements Function { public ValueEval evaluate(ValueEval[] args, int srcCellRow, int srcCellCol) { @@ -33,8 +41,10 @@ public final class T implements Function { } ValueEval arg = args[0]; if (arg instanceof RefEval) { - RefEval re = (RefEval) arg; - arg = re.getInnerValueEval(); + arg = ((RefEval) arg).getInnerValueEval(); + } else if (arg instanceof AreaEval) { + // when the arg is an area, choose the top left cell + arg = ((AreaEval) arg).getRelativeValue(0, 0); } if (arg instanceof StringEval) { diff --git a/src/testcases/org/apache/poi/hssf/record/formula/functions/TestTFunc.java b/src/testcases/org/apache/poi/hssf/record/formula/functions/TestTFunc.java index 53482c86c8..8a502c20cc 100644 --- a/src/testcases/org/apache/poi/hssf/record/formula/functions/TestTFunc.java +++ b/src/testcases/org/apache/poi/hssf/record/formula/functions/TestTFunc.java @@ -19,6 +19,7 @@ package org.apache.poi.hssf.record.formula.functions; import junit.framework.TestCase; +import org.apache.poi.hssf.record.formula.eval.AreaEval; 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; @@ -112,4 +113,20 @@ public final class TestTFunc extends TestCase { eval = invokeTWithReference(ErrorEval.NAME_INVALID); assertTrue(eval == ErrorEval.NAME_INVALID); } + + public void testAreaArg() { + ValueEval[] areaValues = new ValueEval[] { + new StringEval("abc"), new StringEval("def"), + new StringEval("ghi"), new StringEval("jkl"), + }; + AreaEval ae = EvalFactory.createAreaEval("C10:D11", areaValues); + + ValueEval ve; + ve = invokeT(ae); + confirmString(ve, "abc"); + + areaValues[0] = new NumberEval(5.0); + ve = invokeT(ae); + confirmString(ve, ""); + } } -- 2.39.5