You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

TestXSSFXLookupFunction.java 3.9KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. /* ====================================================================
  2. Licensed to the Apache Software Foundation (ASF) under one or more
  3. contributor license agreements. See the NOTICE file distributed with
  4. this work for additional information regarding copyright ownership.
  5. The ASF licenses this file to You under the Apache License, Version 2.0
  6. (the "License"); you may not use this file except in compliance with
  7. the License. You may obtain a copy of the License at
  8. http://www.apache.org/licenses/LICENSE-2.0
  9. Unless required by applicable law or agreed to in writing, software
  10. distributed under the License is distributed on an "AS IS" BASIS,
  11. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. See the License for the specific language governing permissions and
  13. limitations under the License.
  14. ==================================================================== */
  15. package org.apache.poi.xssf;
  16. import org.apache.poi.ss.util.CellRangeAddress;
  17. import org.apache.poi.ss.util.CellReference;
  18. import org.apache.poi.xssf.usermodel.*;
  19. import org.junit.jupiter.api.Test;
  20. import java.io.IOException;
  21. import java.util.Locale;
  22. import static org.apache.poi.ss.util.Utils.*;
  23. import static org.junit.jupiter.api.Assertions.assertEquals;
  24. /**
  25. * Testcase for function XLOOKUP()
  26. */
  27. class TestXSSFXLookupFunction {
  28. //https://support.microsoft.com/en-us/office/xlookup-function-b7fd680e-6d10-43e6-84f9-88eae8bf5929
  29. @Test
  30. void testMicrosoftExample2() throws IOException {
  31. String formulaText = "XLOOKUP(B2,B5:B14,C5:D14)";
  32. try (XSSFWorkbook wb = initWorkbook2()) {
  33. XSSFFormulaEvaluator fe = new XSSFFormulaEvaluator(wb);
  34. XSSFSheet sheet = wb.getSheetAt(0);
  35. XSSFRow row1 = sheet.getRow(1);
  36. String col1 = CellReference.convertNumToColString(2);
  37. String col2 = CellReference.convertNumToColString(3);
  38. String cellRef = String.format(Locale.ENGLISH, "%s2:%s2", col1, col2);
  39. sheet.setArrayFormula(formulaText, CellRangeAddress.valueOf(cellRef));
  40. fe.evaluateAll();
  41. assertEquals("Dianne Pugh", row1.getCell(2).getStringCellValue());
  42. assertEquals("Finance", row1.getCell(3).getStringCellValue());
  43. }
  44. }
  45. @Test
  46. void testXLookupFile() throws Exception {
  47. try (XSSFWorkbook workbook = XSSFTestDataSamples.openSampleWorkbook("xlookup.xlsx")) {
  48. XSSFSheet sheet = workbook.getSheetAt(0);
  49. XSSFFormulaEvaluator fe = new XSSFFormulaEvaluator(workbook);
  50. XSSFRow row1 = sheet.getRow(1);
  51. assertEquals("Dianne Pugh", row1.getCell(2).getStringCellValue());
  52. assertEquals("Finance", row1.getCell(3).getStringCellValue());
  53. fe.evaluateAll();
  54. row1 = sheet.getRow(1);
  55. assertEquals("Dianne Pugh", row1.getCell(2).getStringCellValue());
  56. assertEquals("Finance", row1.getCell(3).getStringCellValue());
  57. }
  58. }
  59. private XSSFWorkbook initWorkbook2() {
  60. XSSFWorkbook wb = new XSSFWorkbook();
  61. XSSFSheet sheet = wb.createSheet();
  62. addRow(sheet, 0, null, "Emp Id", "Employee Name", "Department");
  63. addRow(sheet, 1, null, 8389);
  64. addRow(sheet, 3, null, "Emp Id", "Employee Name", "Department");
  65. addRow(sheet, 4, null, 4390, "Ned Lanning", "Marketing");
  66. addRow(sheet, 5, null, 8604, "Margo Hendrix", "Sales");
  67. addRow(sheet, 6, null, 8389, "Dianne Pugh", "Finance");
  68. addRow(sheet, 7, null, 4937, "Earlene McCarty", "Accounting");
  69. addRow(sheet, 8, null, 8299, "Mia Arnold", "Operation");
  70. addRow(sheet, 9, null, 2643, "Jorge Fellows", "Executive");
  71. addRow(sheet, 10, null, 5243, "Rose Winters", "Sales");
  72. addRow(sheet, 11, null, 9693, "Carmela Hahn", "Finance");
  73. addRow(sheet, 12, null, 1636, "Delia Cochran", "Accounting");
  74. addRow(sheet, 13, null, 6703, "Marguerite Cervantes", "Marketing");
  75. return wb;
  76. }
  77. }