aboutsummaryrefslogtreecommitdiffstats
path: root/poi-ooxml
diff options
context:
space:
mode:
authorPJ Fanning <fanningpj@apache.org>2021-12-02 19:10:14 +0000
committerPJ Fanning <fanningpj@apache.org>2021-12-02 19:10:14 +0000
commitc8a3870064b49ce280e87da67740696e001488fd (patch)
tree35695559c672baa69416aa29e7b656c9cff98254 /poi-ooxml
parent27d837067ffdb9cdc4ed82e09cb3bd4cacd6aa06 (diff)
downloadpoi-c8a3870064b49ce280e87da67740696e001488fd.tar.gz
poi-c8a3870064b49ce280e87da67740696e001488fd.zip
[github-243] basic version of XLookup
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1895499 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'poi-ooxml')
-rw-r--r--poi-ooxml/src/test/java/org/apache/poi/xssf/TestXLookupFunction.java78
1 files changed, 78 insertions, 0 deletions
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
new file mode 100644
index 0000000000..932629255c
--- /dev/null
+++ b/poi-ooxml/src/test/java/org/apache/poi/xssf/TestXLookupFunction.java
@@ -0,0 +1,78 @@
+
+/* ====================================================================
+ 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();
+ try (java.io.FileOutputStream fos = new java.io.FileOutputStream("/tmp/xlook.xlsx")) {
+ wb.write(fos);
+ }
+ 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;
+ }
+
+}