From 270107d9e80bc40c73e1478a9f74ec3a690013a6 Mon Sep 17 00:00:00 2001 From: PJ Fanning Date: Thu, 12 Aug 2021 14:46:41 +0000 Subject: [PATCH] remove XLookup git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1892285 13f79535-47bb-0310-9956-ffa450edef68 --- .../poi/ss/formula/atp/AnalysisToolPak.java | 1 - .../poi/ss/formula/atp/XLookupFunction.java | 127 ------------------ .../poi/ss/formula/functions/LookupUtils.java | 2 + .../ss/formula/atp/TestXLookupFunction.java | 102 -------------- 4 files changed, 2 insertions(+), 230 deletions(-) delete mode 100644 poi/src/main/java/org/apache/poi/ss/formula/atp/XLookupFunction.java delete mode 100644 poi/src/test/java/org/apache/poi/ss/formula/atp/TestXLookupFunction.java diff --git a/poi/src/main/java/org/apache/poi/ss/formula/atp/AnalysisToolPak.java b/poi/src/main/java/org/apache/poi/ss/formula/atp/AnalysisToolPak.java index 05e3a7a678..ca0a91df00 100644 --- a/poi/src/main/java/org/apache/poi/ss/formula/atp/AnalysisToolPak.java +++ b/poi/src/main/java/org/apache/poi/ss/formula/atp/AnalysisToolPak.java @@ -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); diff --git a/poi/src/main/java/org/apache/poi/ss/formula/atp/XLookupFunction.java b/poi/src/main/java/org/apache/poi/ss/formula/atp/XLookupFunction.java deleted file mode 100644 index 1e08ff23f7..0000000000 --- a/poi/src/main/java/org/apache/poi/ss/formula/atp/XLookupFunction.java +++ /dev/null @@ -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. - * - * Syntax
- * XLOOKUP(lookup_value, lookup_array, return_array, [if_not_found], [match_mode], [search_mode])

- * - * @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 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 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); - } -} 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 8710991dcc..bc371ec44c 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 @@ -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 { /** diff --git a/poi/src/test/java/org/apache/poi/ss/formula/atp/TestXLookupFunction.java b/poi/src/test/java/org/apache/poi/ss/formula/atp/TestXLookupFunction.java deleted file mode 100644 index dbf11b7611..0000000000 --- a/poi/src/test/java/org/apache/poi/ss/formula/atp/TestXLookupFunction.java +++ /dev/null @@ -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; - } - -}