From d5309773479853f14eaa7cb6e335af7576b82f9d Mon Sep 17 00:00:00 2001 From: PJ Fanning Date: Fri, 21 Jun 2024 20:25:46 +0000 Subject: [PATCH] [bug-69147] fix issues with text function when input is a datetime in string format git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1918501 13f79535-47bb-0310-9956-ffa450edef68 --- .../apache/poi/ss/formula/functions/TextFunction.java | 10 +++++++++- .../org/apache/poi/ss/formula/functions/TestText.java | 7 +++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/poi/src/main/java/org/apache/poi/ss/formula/functions/TextFunction.java b/poi/src/main/java/org/apache/poi/ss/formula/functions/TextFunction.java index d0a78c4bc1..518fe08e0c 100644 --- a/poi/src/main/java/org/apache/poi/ss/formula/functions/TextFunction.java +++ b/poi/src/main/java/org/apache/poi/ss/formula/functions/TextFunction.java @@ -31,6 +31,7 @@ import org.apache.poi.ss.formula.eval.StringEval; import org.apache.poi.ss.formula.eval.StringValueEval; import org.apache.poi.ss.formula.eval.ValueEval; import org.apache.poi.ss.usermodel.DataFormatter; +import org.apache.poi.ss.usermodel.DateUtil; public abstract class TextFunction implements Function { protected static final DataFormatter formatter = new DataFormatter(); @@ -39,6 +40,7 @@ public abstract class TextFunction implements Function { ValueEval ve = OperandResolver.getSingleValue(eval, srcRow, srcCol); return OperandResolver.coerceValueToString(ve); } + protected static int evaluateIntArg(ValueEval arg, int srcCellRow, int srcCellCol) throws EvaluationException { ValueEval ve = OperandResolver.getSingleValue(arg, srcCellRow, srcCellCol); return OperandResolver.coerceValueToInt(ve); @@ -363,8 +365,14 @@ public abstract class TextFunction implements Function { } else if (valueVe instanceof StringEval) { evaluated = ((StringEval) valueVe).getStringValue(); valueDouble = OperandResolver.parseDouble(evaluated); + if (valueDouble == null) { + try { + valueDouble = DateUtil.parseDateTime(evaluated); + } catch (Exception ignored) { + valueDouble = null; + } + } } - if (valueDouble != null) { String format = formatPatternValueEval2String(formatVe); evaluated = formatter.formatRawCellContents(valueDouble, -1, format); diff --git a/poi/src/test/java/org/apache/poi/ss/formula/functions/TestText.java b/poi/src/test/java/org/apache/poi/ss/formula/functions/TestText.java index 8a7dab1cb6..7be6ba5664 100644 --- a/poi/src/test/java/org/apache/poi/ss/formula/functions/TestText.java +++ b/poi/src/test/java/org/apache/poi/ss/formula/functions/TestText.java @@ -286,6 +286,13 @@ final class TestText { testText(new NumberEval(DateUtil.getExcelDate(ld)), new StringEval("MMM"), "Feb"); } + @Test + void testTextMMMStringInput() { + // https://bz.apache.org/bugzilla/show_bug.cgi?id=67475 + String dateInput = "02/28/2022"; + testText(new StringEval(dateInput), new StringEval("MMM"), "Feb"); + } + private void testText(ValueEval valueArg, ValueEval formatArg, String expectedResult) { ValueEval[] args = { valueArg, formatArg }; ValueEval result = TextFunction.TEXT.evaluate(args, -1, -1); -- 2.39.5