summaryrefslogtreecommitdiffstats
path: root/poi
diff options
context:
space:
mode:
authorPJ Fanning <fanningpj@apache.org>2021-12-06 10:37:08 +0000
committerPJ Fanning <fanningpj@apache.org>2021-12-06 10:37:08 +0000
commit3e3bdd132898d47301dbe8b647aac841df0c8d45 (patch)
tree0ecd298364d9f51d9ef139d07f73c11650818bae /poi
parent50bab16ecede7454892a9f086cfa59fd83506fac (diff)
downloadpoi-3e3bdd132898d47301dbe8b647aac841df0c8d45.tar.gz
poi-3e3bdd132898d47301dbe8b647aac841df0c8d45.zip
add xmatch test
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1895615 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'poi')
-rw-r--r--poi/src/main/java/org/apache/poi/ss/formula/functions/LookupUtils.java25
-rw-r--r--poi/src/test/java/org/apache/poi/ss/formula/atp/TestXMatchFunction.java12
2 files changed, 32 insertions, 5 deletions
diff --git a/poi/src/main/java/org/apache/poi/ss/formula/functions/LookupUtils.java b/poi/src/main/java/org/apache/poi/ss/formula/functions/LookupUtils.java
index 04ec1004b3..09528ae95a 100644
--- a/poi/src/main/java/org/apache/poi/ss/formula/functions/LookupUtils.java
+++ b/poi/src/main/java/org/apache/poi/ss/formula/functions/LookupUtils.java
@@ -623,7 +623,30 @@ public final class LookupUtils {
}
public static int xlookupIndexOfValue(ValueEval lookupValue, ValueVector vector, MatchMode matchMode, SearchMode searchMode) throws EvaluationException {
- LookupValueComparer lookupComparer = createTolerantLookupComparer(lookupValue, matchMode != MatchMode.WildcardMatch, true);
+ ValueEval modifiedLookup = lookupValue;
+ if (lookupValue instanceof StringEval &&
+ (matchMode == MatchMode.ExactMatchFallbackToLargerValue || matchMode == MatchMode.ExactMatchFallbackToSmallerValue)) {
+ String lookupText = ((StringEval)lookupValue).getStringValue();
+ StringBuilder sb = new StringBuilder(lookupText.length());
+ boolean containsWildcard = false;
+ for (char c : lookupText.toCharArray()) {
+ switch (c) {
+ case '~':
+ case '?':
+ case '*':
+ containsWildcard = true;
+ break;
+ default:
+ sb.append(c);
+ }
+ if (containsWildcard)
+ break;
+ }
+ if (containsWildcard) {
+ modifiedLookup = new StringEval(sb.toString());
+ }
+ }
+ LookupValueComparer lookupComparer = createTolerantLookupComparer(modifiedLookup, matchMode != MatchMode.WildcardMatch, true);
int result;
if (searchMode == SearchMode.BinarySearchForward) {
result = binarySearchIndexOfValue(lookupComparer, vector, matchMode, false);
diff --git a/poi/src/test/java/org/apache/poi/ss/formula/atp/TestXMatchFunction.java b/poi/src/test/java/org/apache/poi/ss/formula/atp/TestXMatchFunction.java
index 58fa8d8e51..559fa22066 100644
--- a/poi/src/test/java/org/apache/poi/ss/formula/atp/TestXMatchFunction.java
+++ b/poi/src/test/java/org/apache/poi/ss/formula/atp/TestXMatchFunction.java
@@ -21,12 +21,12 @@ import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFFormulaEvaluator;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
+import org.apache.poi.ss.usermodel.FormulaError;
import org.junit.jupiter.api.Test;
import java.io.IOException;
-import static org.apache.poi.ss.util.Utils.addRow;
-import static org.apache.poi.ss.util.Utils.assertDouble;
+import static org.apache.poi.ss.util.Utils.*;
/**
* Testcase for function XMATCH()
@@ -40,16 +40,20 @@ public class TestXMatchFunction {
HSSFFormulaEvaluator fe = new HSSFFormulaEvaluator(wb);
HSSFCell cell = wb.getSheetAt(0).getRow(2).createCell(5);
assertDouble(fe, cell, "XMATCH(E3,C3:C7)", 2);
+ assertError(fe, cell, "XMATCH(\"Gra\",C3:C7)", FormulaError.NA);
}
}
@Test
void testMicrosoftExample1() throws IOException {
- try (HSSFWorkbook wb = initWorkbook("Gra")) {
+ try (HSSFWorkbook wb = initWorkbook("Gra?")) {
HSSFFormulaEvaluator fe = new HSSFFormulaEvaluator(wb);
HSSFCell cell = wb.getSheetAt(0).getRow(2).createCell(5);
- //TODO investigate issue with `Gra?`
assertDouble(fe, cell, "XMATCH(E3,C3:C7,1)", 2);
+ assertDouble(fe, cell, "XMATCH(E3,C3:C7,-1)", 5);
+ assertDouble(fe, cell, "XMATCH(\"Gra\",C3:C7,1)", 2);
+ assertDouble(fe, cell, "XMATCH(\"Graz\",C3:C7,1)", 3);
+ assertDouble(fe, cell, "XMATCH(\"Graz\",C3:C7,-1)", 2);
}
}