From: PJ Fanning Date: Mon, 6 Dec 2021 10:10:16 +0000 (+0000) Subject: add xlookup test X-Git-Tag: REL_5_2_0~117 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=50bab16ecede7454892a9f086cfa59fd83506fac;p=poi.git add xlookup test git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1895613 13f79535-47bb-0310-9956-ffa450edef68 --- diff --git a/poi-ooxml/src/test/java/org/apache/poi/xssf/TestXLookupFunction.java b/poi-ooxml/src/test/java/org/apache/poi/xssf/TestXLookupFunction.java deleted file mode 100644 index 16122cfffc..0000000000 --- a/poi-ooxml/src/test/java/org/apache/poi/xssf/TestXLookupFunction.java +++ /dev/null @@ -1,75 +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.xssf; - -import org.apache.poi.ss.util.CellRangeAddress; -import org.apache.poi.ss.util.CellReference; -import org.apache.poi.xssf.usermodel.*; -import org.junit.jupiter.api.Test; - -import java.io.IOException; -import java.util.Locale; - -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 testMicrosoftExample2() throws IOException { - String formulaText = "XLOOKUP(B2,B5:B14,C5:D14)"; - try (XSSFWorkbook wb = initWorkbook2()) { - XSSFFormulaEvaluator fe = new XSSFFormulaEvaluator(wb); - XSSFSheet sheet = wb.getSheetAt(0); - XSSFRow row1 = sheet.getRow(1); - String col1 = CellReference.convertNumToColString(2); - String col2 = CellReference.convertNumToColString(3); - String cellRef = String.format(Locale.ENGLISH, "%s2:%s2", col1, col2); - sheet.setArrayFormula(formulaText, CellRangeAddress.valueOf(cellRef)); - fe.evaluateAll(); - assertEquals("Dianne Pugh", row1.getCell(2).getStringCellValue()); - assertEquals("Finance", row1.getCell(3).getStringCellValue()); - } - } - - - private XSSFWorkbook initWorkbook2() { - XSSFWorkbook wb = new XSSFWorkbook(); - XSSFSheet 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; - } - -} diff --git a/poi-ooxml/src/test/java/org/apache/poi/xssf/TestXSSFXLookupFunction.java b/poi-ooxml/src/test/java/org/apache/poi/xssf/TestXSSFXLookupFunction.java new file mode 100644 index 0000000000..dbe7c956fc --- /dev/null +++ b/poi-ooxml/src/test/java/org/apache/poi/xssf/TestXSSFXLookupFunction.java @@ -0,0 +1,89 @@ + +/* ==================================================================== + 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.xssf; + +import org.apache.poi.ss.util.CellRangeAddress; +import org.apache.poi.ss.util.CellReference; +import org.apache.poi.xssf.usermodel.*; +import org.junit.jupiter.api.Test; + +import java.io.IOException; +import java.util.Locale; + +import static org.apache.poi.ss.util.Utils.*; +import static org.junit.jupiter.api.Assertions.assertEquals; + +/** + * Testcase for function XLOOKUP() + */ +public class TestXSSFXLookupFunction { + + //https://support.microsoft.com/en-us/office/xlookup-function-b7fd680e-6d10-43e6-84f9-88eae8bf5929 + + @Test + void testMicrosoftExample2() throws IOException { + String formulaText = "XLOOKUP(B2,B5:B14,C5:D14)"; + try (XSSFWorkbook wb = initWorkbook2()) { + XSSFFormulaEvaluator fe = new XSSFFormulaEvaluator(wb); + XSSFSheet sheet = wb.getSheetAt(0); + XSSFRow row1 = sheet.getRow(1); + String col1 = CellReference.convertNumToColString(2); + String col2 = CellReference.convertNumToColString(3); + String cellRef = String.format(Locale.ENGLISH, "%s2:%s2", col1, col2); + sheet.setArrayFormula(formulaText, CellRangeAddress.valueOf(cellRef)); + fe.evaluateAll(); + assertEquals("Dianne Pugh", row1.getCell(2).getStringCellValue()); + assertEquals("Finance", row1.getCell(3).getStringCellValue()); + } + } + + @Test + void testXLookupFile() throws Exception { + try (XSSFWorkbook workbook = XSSFTestDataSamples.openSampleWorkbook("xlookup.xlsx")) { + XSSFSheet sheet = workbook.getSheetAt(0); + XSSFFormulaEvaluator fe = new XSSFFormulaEvaluator(workbook); + XSSFRow row1 = sheet.getRow(1); + assertEquals("Dianne Pugh", row1.getCell(2).getStringCellValue()); + assertEquals("Finance", row1.getCell(3).getStringCellValue()); + fe.evaluateAll(); + row1 = sheet.getRow(1); + assertEquals("Dianne Pugh", row1.getCell(2).getStringCellValue()); + assertEquals("Finance", row1.getCell(3).getStringCellValue()); + } + } + + private XSSFWorkbook initWorkbook2() { + XSSFWorkbook wb = new XSSFWorkbook(); + XSSFSheet 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; + } + +} diff --git a/test-data/spreadsheet/xlookup.xlsx b/test-data/spreadsheet/xlookup.xlsx new file mode 100644 index 0000000000..58b6ae9bc8 Binary files /dev/null and b/test-data/spreadsheet/xlookup.xlsx differ