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 c516ce47e3..449d6d7f35 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 @@ -148,13 +148,14 @@ public abstract class TextFunction implements Function { /** * An implementation of the TRIM function: * Removes leading and trailing spaces from value if evaluated operand - * value is string. + * value is string. Since POI 5.0.1, this also trims double spaces so that only 1 + * is kept (https://bz.apache.org/bugzilla/show_bug.cgi?id=65230). * Author: Manda Wilson < wilson at c bio dot msk cc dot org > */ public static final Function TRIM = new SingleArgTextFunc() { @Override protected ValueEval evaluate(String arg) { - return new StringEval(arg.trim()); + return new StringEval(arg.trim().replaceAll(" +", " ")); } }; diff --git a/poi/src/test/java/org/apache/poi/ss/formula/functions/TestTrim.java b/poi/src/test/java/org/apache/poi/ss/formula/functions/TestTrim.java index 58977577ff..95ed6f2583 100644 --- a/poi/src/test/java/org/apache/poi/ss/formula/functions/TestTrim.java +++ b/poi/src/test/java/org/apache/poi/ss/formula/functions/TestTrim.java @@ -52,7 +52,6 @@ final class TestTrim { @Test void testBasic() { - confirmTrim(new StringEval(" hi "), "hi"); confirmTrim(new StringEval("hi "), "hi"); confirmTrim(new StringEval(" hi"), "hi"); @@ -61,6 +60,13 @@ final class TestTrim { confirmTrim(new StringEval(" "), ""); } + @Test + void testExtraSpaces() { + //https://bz.apache.org/bugzilla/show_bug.cgi?id=65230 + confirmTrim(new StringEval(" hi there "), "hi there"); + confirmTrim(new StringEval("hi there"), "hi there"); + } + /** * Valid cases where text arg is not exactly a string */