aboutsummaryrefslogtreecommitdiffstats
path: root/poi
diff options
context:
space:
mode:
authorPJ Fanning <fanningpj@apache.org>2024-06-21 20:25:46 +0000
committerPJ Fanning <fanningpj@apache.org>2024-06-21 20:25:46 +0000
commitd5309773479853f14eaa7cb6e335af7576b82f9d (patch)
tree0c962507cb88c341dc490da372e8873a1f9ee442 /poi
parent461691ef0e69da8cc0e750b17ce8aaf1c263d89e (diff)
downloadpoi-d5309773479853f14eaa7cb6e335af7576b82f9d.tar.gz
poi-d5309773479853f14eaa7cb6e335af7576b82f9d.zip
[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
Diffstat (limited to 'poi')
-rw-r--r--poi/src/main/java/org/apache/poi/ss/formula/functions/TextFunction.java10
-rw-r--r--poi/src/test/java/org/apache/poi/ss/formula/functions/TestText.java7
2 files changed, 16 insertions, 1 deletions
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);