[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
This commit is contained in:
PJ Fanning 2024-06-21 20:25:46 +00:00
parent 461691ef0e
commit d530977347
2 changed files with 16 additions and 1 deletions

View File

@ -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);

View File

@ -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);