diff options
author | PJ Fanning <fanningpj@apache.org> | 2021-08-07 11:16:34 +0000 |
---|---|---|
committer | PJ Fanning <fanningpj@apache.org> | 2021-08-07 11:16:34 +0000 |
commit | f00456e38d5be4c268a78dae383df6f93e7f8005 (patch) | |
tree | 0f8c5e32fbba495e08ee7a383364646159171df4 /poi/src/main/java | |
parent | d475e00f3a4c09546ec8c5ed49d456225cc78a43 (diff) | |
download | poi-f00456e38d5be4c268a78dae383df6f93e7f8005.tar.gz poi-f00456e38d5be4c268a78dae383df6f93e7f8005.zip |
fix TextJoin use case that was not handled
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1892067 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'poi/src/main/java')
-rw-r--r-- | poi/src/main/java/org/apache/poi/ss/formula/atp/TextJoinFunction.java | 37 |
1 files changed, 31 insertions, 6 deletions
diff --git a/poi/src/main/java/org/apache/poi/ss/formula/atp/TextJoinFunction.java b/poi/src/main/java/org/apache/poi/ss/formula/atp/TextJoinFunction.java index 5c64fd8bcd..8cd588715a 100644 --- a/poi/src/main/java/org/apache/poi/ss/formula/atp/TextJoinFunction.java +++ b/poi/src/main/java/org/apache/poi/ss/formula/atp/TextJoinFunction.java @@ -71,8 +71,7 @@ final class TextJoinFunction implements FreeRefFunction { try { // Get the delimiter argument - ValueEval delimiterArg = OperandResolver.getSingleValue(args[0], srcRowIndex, srcColumnIndex); - String delimiter = OperandResolver.coerceValueToString(delimiterArg); + List<ValueEval> delimiterArgs = getValues(args[0], srcRowIndex, srcColumnIndex, true); // Get the boolean ignoreEmpty argument ValueEval ignoreEmptyArg = OperandResolver.getSingleValue(args[1], srcRowIndex, srcColumnIndex); @@ -82,7 +81,7 @@ final class TextJoinFunction implements FreeRefFunction { ArrayList<String> textValues = new ArrayList<>(); for (int i = 2; i < args.length; i++) { - List<ValueEval> textArgs = getValues(args[i], srcRowIndex, srcColumnIndex); + List<ValueEval> textArgs = getValues(args[i], srcRowIndex, srcColumnIndex, false); for (ValueEval textArg : textArgs) { String textValue = OperandResolver.coerceValueToString(textArg); @@ -94,17 +93,43 @@ final class TextJoinFunction implements FreeRefFunction { } // Join the list of values with the specified delimiter and return - return new StringEval(String.join(delimiter, textValues)); + if (delimiterArgs.size() == 0) { + return new StringEval(String.join("", textValues)); + } else if (delimiterArgs.size() == 1) { + String delimiter = OperandResolver.coerceValueToString(delimiterArgs.get(0)); + return new StringEval(String.join(delimiter, textValues)); + } else { + //https://support.microsoft.com/en-us/office/textjoin-function-357b449a-ec91-49d0-80c3-0e8fc845691c + //see example 3 to see why this is needed + List<String> delimiters = new ArrayList<>(); + for (ValueEval delimiterArg: delimiterArgs) { + delimiters.add(OperandResolver.coerceValueToString(delimiterArg)); + } + StringBuilder sb = new StringBuilder(); + for (int i = 0; i < textValues.size(); i++) { + if (i > 0) { + int delimiterIndex = (i - 1) % delimiters.size(); + sb.append(delimiters.get(delimiterIndex)); + } + sb.append(textValues.get(i)); + } + return new StringEval(sb.toString()); + } } catch (EvaluationException e){ return e.getErrorEval(); } } - private List<ValueEval> getValues(ValueEval eval, int srcRowIndex, int srcColumnIndex) throws EvaluationException { + //https://support.microsoft.com/en-us/office/textjoin-function-357b449a-ec91-49d0-80c3-0e8fc845691c + //in example 3, the delimiter is defined by a large area but only the last row of that area seems to be used + //this is why lastRowOnly is supported + private List<ValueEval> getValues(ValueEval eval, int srcRowIndex, int srcColumnIndex, + boolean lastRowOnly) throws EvaluationException { if (eval instanceof AreaEval) { AreaEval ae = (AreaEval)eval; List<ValueEval> list = new ArrayList<>(); - for (int r = ae.getFirstRow(); r <= ae.getLastRow(); r++) { + int startRow = lastRowOnly ? ae.getLastRow() : ae.getFirstRow(); + for (int r = startRow; r <= ae.getLastRow(); r++) { for (int c = ae.getFirstColumn(); c <= ae.getLastColumn(); c++) { list.add(OperandResolver.getSingleValue(ae.getAbsoluteValue(r, c), r, c)); } |