remove XLookup

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1892285 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
PJ Fanning 2021-08-12 14:46:41 +00:00
parent ef61f3da7c
commit 270107d9e8
4 changed files with 2 additions and 230 deletions

View File

@ -175,7 +175,6 @@ public final class AnalysisToolPak implements UDFFinder {
r(m, "WEEKNUM", WeekNum.instance);
r(m, "WORKDAY", WorkdayFunction.instance);
r(m, "XIRR", null);
r(m, "XLOOKUP", XLookupFunction.instance);
r(m, "XNPV", null);
r(m, "YEARFRAC", YearFrac.instance);
r(m, "YIELD", null);

View File

@ -1,127 +0,0 @@
/* ====================================================================
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==================================================================== */
package org.apache.poi.ss.formula.atp;
import org.apache.poi.ss.formula.OperationEvaluationContext;
import org.apache.poi.ss.formula.TwoDEval;
import org.apache.poi.ss.formula.eval.*;
import org.apache.poi.ss.formula.functions.FreeRefFunction;
import org.apache.poi.ss.formula.functions.LookupUtils;
import java.util.Optional;
/**
* Implementation of Excel function XLOOKUP()
*
* POI does not currently support having return values with multiple columns and just takes the first cell
* right now.
*
* <b>Syntax</b><br>
* <b>XLOOKUP</b>(<b>lookup_value</b>, <b>lookup_array</b>, <b>return_array</b>, <b>[if_not_found]</b>, <b>[match_mode]</b>, <b>[search_mode]</b>)<p>
*
* @since POI 5.0.1
*/
final class XLookupFunction implements FreeRefFunction {
public static final FreeRefFunction instance = new XLookupFunction(ArgumentsEvaluator.instance);
private ArgumentsEvaluator evaluator;
private XLookupFunction(ArgumentsEvaluator anEvaluator) {
// enforces singleton
this.evaluator = anEvaluator;
}
public ValueEval evaluate(ValueEval[] args, OperationEvaluationContext ec) {
int srcRowIndex = ec.getRowIndex();
int srcColumnIndex = ec.getColumnIndex();
if (args.length < 3) {
return ErrorEval.VALUE_INVALID;
}
Optional<String> notFound = Optional.empty();
if (args.length > 3) {
try {
ValueEval notFoundValue = OperandResolver.getSingleValue(args[3], srcRowIndex, srcColumnIndex);
String notFoundText = laxValueToString(notFoundValue);
if (notFoundText != null) {
String trimmedText = notFoundText.trim();
if (trimmedText.length() > 0) {
notFound = Optional.of(trimmedText);
}
}
} catch (EvaluationException e) {
return e.getErrorEval();
}
}
int matchMode = 0;
if (args.length > 4) {
try {
ValueEval matchModeValue = OperandResolver.getSingleValue(args[4], srcRowIndex, srcColumnIndex);
matchMode = OperandResolver.coerceValueToInt(matchModeValue);
} catch (EvaluationException e) {
return e.getErrorEval();
}
}
int searchMode = 1;
if (args.length > 5) {
try {
ValueEval searchModeValue = OperandResolver.getSingleValue(args[5], srcRowIndex, srcColumnIndex);
searchMode = OperandResolver.coerceValueToInt(searchModeValue);
} catch (EvaluationException e) {
return e.getErrorEval();
}
}
return evaluate(srcRowIndex, srcColumnIndex, args[0], args[1], args[2], notFound, matchMode, searchMode);
}
private ValueEval evaluate(int srcRowIndex, int srcColumnIndex, ValueEval lookupEval, ValueEval indexEval,
ValueEval returnEval, Optional<String> notFound, int matchMode, int searchMode) {
try {
ValueEval lookupValue = OperandResolver.getSingleValue(lookupEval, srcRowIndex, srcColumnIndex);
TwoDEval tableArray = LookupUtils.resolveTableArrayArg(indexEval);
boolean isRangeLookup = false;
int matchedRow;
try {
matchedRow = LookupUtils.lookupIndexOfValue(lookupValue, LookupUtils.createColumnVector(tableArray, 0), isRangeLookup);
} catch (EvaluationException e) {
if (ErrorEval.NA.equals(e.getErrorEval())) {
if (notFound.isPresent()) {
return new StringEval(notFound.get());
}
return ErrorEval.NA;
} else {
return e.getErrorEval();
}
}
if (returnEval instanceof AreaEval) {
AreaEval area = (AreaEval)returnEval;
//TODO to fully support XLOOKUP, we should return the full row
//but POI does not currently support functions returning multiple cell values
return area.getRelativeValue(matchedRow, 0);
} else {
return ErrorEval.VALUE_INVALID;
}
} catch (EvaluationException e) {
return e.getErrorEval();
}
}
private String laxValueToString(ValueEval eval) {
return (eval instanceof MissingArgEval) ? "" : OperandResolver.coerceValueToString(eval);
}
}

View File

@ -32,10 +32,12 @@ import org.apache.poi.ss.formula.eval.OperandResolver;
import org.apache.poi.ss.formula.eval.RefEval;
import org.apache.poi.ss.formula.eval.StringEval;
import org.apache.poi.ss.formula.eval.ValueEval;
import org.apache.poi.util.Internal;
/**
* Common functionality used by VLOOKUP, HLOOKUP, LOOKUP and MATCH
*/
@Internal
public final class LookupUtils {
/**

View File

@ -1,102 +0,0 @@
/* ====================================================================
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==================================================================== */
package org.apache.poi.ss.formula.atp;
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.*;
import org.junit.jupiter.api.Test;
import java.io.IOException;
import static org.apache.poi.ss.util.Utils.*;
import static org.junit.jupiter.api.Assertions.assertEquals;
/**
* Testcase for function XLOOKUP()
*/
public class TestXLookupFunction {
//https://support.microsoft.com/en-us/office/xlookup-function-b7fd680e-6d10-43e6-84f9-88eae8bf5929
@Test
void testMicrosoftExample1() throws IOException {
try (HSSFWorkbook wb = initWorkbook1()) {
HSSFFormulaEvaluator fe = new HSSFFormulaEvaluator(wb);
HSSFCell cell = wb.getSheetAt(0).getRow(0).createCell(100);
assertString(fe, cell, "XLOOKUP(F2,B2:B11,D2:D11)", "+55");
}
}
@Test
void testMicrosoftExample2() throws IOException {
try (HSSFWorkbook wb = initWorkbook2()) {
HSSFFormulaEvaluator fe = new HSSFFormulaEvaluator(wb);
HSSFCell cell = wb.getSheetAt(0).getRow(0).createCell(100);
assertString(fe, cell, "XLOOKUP(B2,B5:B14,C5:D14)", "Dianne Pugh");
}
}
@Test
void testMicrosoftExample3() throws IOException {
try (HSSFWorkbook wb = initWorkbook2()) {
HSSFFormulaEvaluator fe = new HSSFFormulaEvaluator(wb);
HSSFCell cell = wb.getSheetAt(0).getRow(0).createCell(100);
assertError(fe, cell, "XLOOKUP(999999,B2:B11,D2:D11)", FormulaError.NA);
assertString(fe, cell, "XLOOKUP(999999,B2:B11,D2:D11,\"not found\")", "not found");
}
}
private HSSFWorkbook initWorkbook1() {
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet = wb.createSheet();
addRow(sheet, 0, null, "Country", "Abr", "Prefix");
addRow(sheet, 1, null, "China", "CN", "+86", null, "Brazil");
addRow(sheet, 2, null, "India", "IN", "+91");
addRow(sheet, 3, null, "United States", "US", "+1");
addRow(sheet, 4, null, "Indonesia", "ID", "+62");
addRow(sheet, 5, null, "Brazil", "BR", "+55");
addRow(sheet, 6, null, "Pakistan", "PK", "+92");
addRow(sheet, 7, null, "Nigeria", "NG", "+234");
addRow(sheet, 8, null, "Bangladesh", "BD", "+880");
addRow(sheet, 9, null, "Russia", "RU", "+7");
addRow(sheet, 10, null, "Mexico", "MX", "+52");
return wb;
}
private HSSFWorkbook initWorkbook2() {
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet = wb.createSheet();
addRow(sheet, 0, null, "Emp Id", "Employee Name", "Department");
addRow(sheet, 1, null, 8389);
addRow(sheet, 3, null, "Emp Id", "Employee Name", "Department");
addRow(sheet, 4, null, 4390, "Ned Lanning", "Marketing");
addRow(sheet, 5, null, 8604, "Margo Hendrix", "Sales");
addRow(sheet, 6, null, 8389, "Dianne Pugh", "Finance");
addRow(sheet, 7, null, 4937, "Earlene McCarty", "Accounting");
addRow(sheet, 8, null, 8299, "Mia Arnold", "Operation");
addRow(sheet, 9, null, 2643, "Jorge Fellows", "Executive");
addRow(sheet, 10, null, 5243, "Rose Winters", "Sales");
addRow(sheet, 11, null, 9693, "Carmela Hahn", "Finance");
addRow(sheet, 12, null, 1636, "Delia Cochran", "Accounting");
addRow(sheet, 13, null, 6703, "Marguerite Cervantes", "Marketing");
return wb;
}
}