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.

TestExternalNameReference.java 6.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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.ss.formula.ptg;
  16. import static org.junit.jupiter.api.Assertions.assertEquals;
  17. import static org.junit.jupiter.api.Assertions.assertNotNull;
  18. import org.apache.poi.hssf.HSSFTestDataSamples;
  19. import org.apache.poi.hssf.usermodel.HSSFCell;
  20. import org.apache.poi.hssf.usermodel.HSSFFormulaEvaluator;
  21. import org.apache.poi.hssf.usermodel.HSSFName;
  22. import org.apache.poi.hssf.usermodel.HSSFWorkbook;
  23. import org.apache.poi.ss.util.CellReference;
  24. import org.junit.jupiter.api.Test;
  25. import java.math.BigDecimal;
  26. /**
  27. * Tests for proper calculation of named ranges from external workbooks.
  28. */
  29. final class TestExternalNameReference {
  30. double MARKUP_COST_1 = 1.8d;
  31. double MARKUP_COST_2 = 1.5d;
  32. double PART_COST = 12.3d;
  33. double NEW_QUANT = 7.0d;
  34. double NEW_PART_COST = 15.3d;
  35. /**
  36. * tests {@code NameXPtg for external cell reference by name} and logic in Workbook below that
  37. */
  38. @Test
  39. void testReadCalcSheet() {
  40. HSSFWorkbook wb = HSSFTestDataSamples.openSampleWorkbook("XRefCalc.xls");
  41. HSSFName name = wb.getName("QUANT");
  42. assertNotNull(name);
  43. assertEquals("Sheet1!$A$2", name.getRefersToFormula());
  44. name = wb.getName("PART");
  45. assertNotNull(name);
  46. assertEquals("Sheet1!$B$2", name.getRefersToFormula());
  47. assertEquals("x123",wb.getSheet("Sheet1").getRow(1).getCell(1).getStringCellValue());
  48. name = wb.getName("UNITCOST");
  49. assertNotNull(name);
  50. assertEquals("Sheet1!$C$2", name.getRefersToFormula());
  51. name = wb.getName("UNITCOST");
  52. assertNotNull(name);
  53. CellReference cellRef = new CellReference(name.getRefersToFormula());
  54. HSSFCell cell = wb.getSheet(cellRef.getSheetName()).getRow(cellRef.getRow()).getCell(cellRef.getCol());
  55. assertEquals("VLOOKUP(PART,COSTS,2,FALSE)",cell.getCellFormula());
  56. name = wb.getName("COST");
  57. assertNotNull(name);
  58. assertEquals("Sheet1!$D$2", name.getRefersToFormula());
  59. name = wb.getName("COST");
  60. assertNotNull(name);
  61. cellRef = new CellReference(name.getRefersToFormula());
  62. cell = wb.getSheet(cellRef.getSheetName()).getRow(cellRef.getRow()).getCell(cellRef.getCol());
  63. assertEquals("UNITCOST*Quant",cell.getCellFormula());
  64. name = wb.getName("TOTALCOST");
  65. assertNotNull(name);
  66. assertEquals("Sheet1!$E$2", name.getRefersToFormula());
  67. name = wb.getName("TOTALCOST");
  68. assertNotNull(name);
  69. cellRef = new CellReference(name.getRefersToFormula());
  70. cell = wb.getSheet(cellRef.getSheetName()).getRow(cellRef.getRow()).getCell(cellRef.getCol());
  71. assertEquals("Cost*Markup_Cost",cell.getCellFormula());
  72. }
  73. @Test
  74. void testReadReferencedSheet() {
  75. HSSFWorkbook wb = HSSFTestDataSamples.openSampleWorkbook("XRefCalcData.xls");
  76. HSSFName name = wb.getName("COSTS");
  77. assertNotNull(name);
  78. assertEquals("CostSheet!$A$2:$B$3", name.getRefersToFormula());
  79. assertEquals("x123",wb.getSheet("CostSheet").getRow(1).getCell(0).getStringCellValue());
  80. assertEquals(PART_COST,wb.getSheet("CostSheet").getRow(1).getCell(1).getNumericCellValue(), 0);
  81. name = wb.getName("Markup_Cost");
  82. assertNotNull(name);
  83. assertEquals("MarkupSheet!$B$1", name.getRefersToFormula());
  84. assertEquals(MARKUP_COST_1,wb.getSheet("MarkupSheet").getRow(0).getCell(1).getNumericCellValue(), 0);
  85. }
  86. @Test
  87. void testEvaluate() {
  88. HSSFWorkbook wb = HSSFTestDataSamples.openSampleWorkbook("XRefCalc.xls");
  89. HSSFWorkbook wb2 = HSSFTestDataSamples.openSampleWorkbook("XRefCalcData.xls");
  90. HSSFName name = wb.getName("QUANT");
  91. assertNotNull(name);
  92. CellReference cellRef = new CellReference(name.getRefersToFormula());
  93. HSSFCell cell = wb.getSheet(cellRef.getSheetName()).getRow(cellRef.getRow()).getCell(cellRef.getCol());
  94. cell.setCellValue(NEW_QUANT);
  95. cell = wb2.getSheet("CostSheet").getRow(1).getCell(1);
  96. cell.setCellValue(NEW_PART_COST);
  97. HSSFFormulaEvaluator evaluator = new HSSFFormulaEvaluator(wb);
  98. HSSFFormulaEvaluator evaluatorCost = new HSSFFormulaEvaluator(wb2);
  99. String[] bookNames = { "XRefCalc.xls", "XRefCalcData.xls" };
  100. HSSFFormulaEvaluator[] evaluators = { evaluator, evaluatorCost, };
  101. HSSFFormulaEvaluator.setupEnvironment(bookNames, evaluators);
  102. name = wb.getName("UNITCOST");
  103. assertNotNull(name);
  104. cellRef = new CellReference(name.getRefersToFormula());
  105. HSSFCell uccell = wb.getSheet(cellRef.getSheetName()).getRow(cellRef.getRow()).getCell(cellRef.getCol());
  106. name = wb.getName("COST");
  107. assertNotNull(name);
  108. cellRef = new CellReference(name.getRefersToFormula());
  109. HSSFCell ccell = wb.getSheet(cellRef.getSheetName()).getRow(cellRef.getRow()).getCell(cellRef.getCol());
  110. name = wb.getName("TOTALCOST");
  111. assertNotNull(name);
  112. cellRef = new CellReference(name.getRefersToFormula());
  113. HSSFCell tccell = wb.getSheet(cellRef.getSheetName()).getRow(cellRef.getRow()).getCell(cellRef.getCol());
  114. evaluator.evaluateFormulaCell(uccell);
  115. evaluator.evaluateFormulaCell(ccell);
  116. evaluator.evaluateFormulaCell(tccell);
  117. assertEquals(NEW_PART_COST, uccell.getNumericCellValue(), 0);
  118. assertEquals(new BigDecimal(NEW_PART_COST).multiply(new BigDecimal(NEW_QUANT)).doubleValue(), ccell.getNumericCellValue(), 0.000000001);
  119. assertEquals(NEW_PART_COST*NEW_QUANT*MARKUP_COST_2, tccell.getNumericCellValue(), 0.000000001);
  120. }
  121. }