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.

TestStructuredReferences.java 6.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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;
  16. import static org.junit.Assert.assertEquals;
  17. import static org.junit.Assert.assertFalse;
  18. import static org.junit.Assert.assertTrue;
  19. import static org.junit.Assert.fail;
  20. import org.apache.poi.ss.usermodel.Cell;
  21. import org.apache.poi.ss.usermodel.CellType;
  22. import org.apache.poi.ss.usermodel.CellValue;
  23. import org.apache.poi.ss.usermodel.FormulaEvaluator;
  24. import org.apache.poi.ss.usermodel.Row;
  25. import org.apache.poi.ss.usermodel.Table;
  26. import org.apache.poi.ss.util.AreaReference;
  27. import org.apache.poi.ss.util.CellReference;
  28. import org.apache.poi.xssf.XSSFTestDataSamples;
  29. import org.apache.poi.xssf.usermodel.XSSFFormulaEvaluator;
  30. import org.apache.poi.xssf.usermodel.XSSFSheet;
  31. import org.apache.poi.xssf.usermodel.XSSFTable;
  32. import org.apache.poi.xssf.usermodel.XSSFWorkbook;
  33. import org.junit.Test;
  34. /**
  35. * Tests Excel Table expressions (structured references)
  36. * @see <a href="https://support.office.com/en-us/article/Using-structured-references-with-Excel-tables-F5ED2452-2337-4F71-BED3-C8AE6D2B276E">
  37. * Excel Structured Reference Syntax
  38. * </a>
  39. */
  40. public class TestStructuredReferences {
  41. /**
  42. * Test the regular expression used in INDIRECT() evaluation to recognize structured references
  43. */
  44. @Test
  45. public void testTableExpressionSyntax() {
  46. assertTrue("Valid structured reference syntax didn't match expression", Table.isStructuredReference.matcher("abc[col1]").matches());
  47. assertTrue("Valid structured reference syntax didn't match expression", Table.isStructuredReference.matcher("_abc[col1]").matches());
  48. assertTrue("Valid structured reference syntax didn't match expression", Table.isStructuredReference.matcher("_[col1]").matches());
  49. assertTrue("Valid structured reference syntax didn't match expression", Table.isStructuredReference.matcher("\\[col1]").matches());
  50. assertTrue("Valid structured reference syntax didn't match expression", Table.isStructuredReference.matcher("\\[col1]").matches());
  51. assertTrue("Valid structured reference syntax didn't match expression", Table.isStructuredReference.matcher("\\[#This Row]").matches());
  52. assertTrue("Valid structured reference syntax didn't match expression", Table.isStructuredReference.matcher("\\[ [col1], [col2] ]").matches());
  53. // can't have a space between the table name and open bracket
  54. assertFalse("Invalid structured reference syntax didn't fail expression", Table.isStructuredReference.matcher("\\abc [ [col1], [col2] ]").matches());
  55. }
  56. @Test
  57. public void testTableFormulas() throws Exception {
  58. XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("StructuredReferences.xlsx");
  59. try {
  60. final FormulaEvaluator eval = new XSSFFormulaEvaluator(wb);
  61. final XSSFSheet tableSheet = wb.getSheet("Table");
  62. final XSSFSheet formulaSheet = wb.getSheet("Formulas");
  63. confirm(eval, tableSheet.getRow(5).getCell(0), 49);
  64. confirm(eval, formulaSheet.getRow(0).getCell(0), 209);
  65. confirm(eval, formulaSheet.getRow(1).getCell(0), "one");
  66. // test changing a table value, to see if the caches are properly cleared
  67. // Issue 59814
  68. // this test passes before the fix for 59814
  69. tableSheet.getRow(1).getCell(1).setCellValue("ONEA");
  70. confirm(eval, formulaSheet.getRow(1).getCell(0), "ONEA");
  71. // test adding a row to a table, issue 59814
  72. Row newRow = tableSheet.getRow(7);
  73. if (newRow == null) newRow = tableSheet.createRow(7);
  74. newRow.createCell(0, CellType.FORMULA).setCellFormula("\\_Prime.1[[#This Row],[@Number]]*\\_Prime.1[[#This Row],[@Number]]");
  75. newRow.createCell(1, CellType.STRING).setCellValue("thirteen");
  76. newRow.createCell(2, CellType.NUMERIC).setCellValue(13);
  77. // update Table
  78. final XSSFTable table = wb.getTable("\\_Prime.1");
  79. final AreaReference newArea = new AreaReference(table.getStartCellReference(), new CellReference(table.getEndRowIndex() + 1, table.getEndColIndex()));
  80. String newAreaStr = newArea.formatAsString();
  81. table.getCTTable().setRef(newAreaStr);
  82. table.getCTTable().getAutoFilter().setRef(newAreaStr);
  83. table.updateHeaders();
  84. table.updateReferences();
  85. // these fail before the fix for 59814
  86. confirm(eval, tableSheet.getRow(7).getCell(0), 13*13);
  87. confirm(eval, formulaSheet.getRow(0).getCell(0), 209 + 13*13);
  88. } finally {
  89. wb.close();
  90. }
  91. }
  92. private static void confirm(FormulaEvaluator fe, Cell cell, double expectedResult) {
  93. fe.clearAllCachedResultValues();
  94. CellValue cv = fe.evaluate(cell);
  95. if (cv.getCellTypeEnum() != CellType.NUMERIC) {
  96. fail("expected numeric cell type but got " + cv.formatAsString());
  97. }
  98. assertEquals(expectedResult, cv.getNumberValue(), 0.0);
  99. }
  100. private static void confirm(FormulaEvaluator fe, Cell cell, String expectedResult) {
  101. fe.clearAllCachedResultValues();
  102. CellValue cv = fe.evaluate(cell);
  103. if (cv.getCellTypeEnum() != CellType.STRING) {
  104. fail("expected String cell type but got " + cv.formatAsString());
  105. }
  106. assertEquals(expectedResult, cv.getStringValue());
  107. }
  108. }