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.

TestXSSFDataFormat.java 5.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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.usermodel;
  16. import static org.apache.poi.xssf.XSSFTestDataSamples.openSampleWorkbook;
  17. import static org.junit.jupiter.api.Assertions.assertEquals;
  18. import java.io.IOException;
  19. import org.apache.poi.ss.formula.ConditionalFormattingEvaluator;
  20. import org.apache.poi.ss.formula.WorkbookEvaluatorProvider;
  21. import org.apache.poi.ss.usermodel.BaseTestDataFormat;
  22. import org.apache.poi.ss.usermodel.Cell;
  23. import org.apache.poi.ss.usermodel.CellStyle;
  24. import org.apache.poi.ss.usermodel.DataFormatter;
  25. import org.apache.poi.ss.usermodel.FormulaEvaluator;
  26. import org.apache.poi.ss.usermodel.Workbook;
  27. import org.apache.poi.ss.util.CellReference;
  28. import org.apache.poi.xssf.XSSFITestDataProvider;
  29. import org.apache.poi.xssf.XSSFTestDataSamples;
  30. import org.junit.jupiter.api.Test;
  31. /**
  32. * Tests for {@link XSSFDataFormat}
  33. */
  34. public final class TestXSSFDataFormat extends BaseTestDataFormat {
  35. public TestXSSFDataFormat() {
  36. super(XSSFITestDataProvider.instance);
  37. }
  38. /**
  39. * [Bug 58778] Built-in number formats can be overridden with XSSFDataFormat.putFormat(int id, String fmt)
  40. */
  41. @Test
  42. void test58778() throws IOException {
  43. try (XSSFWorkbook wb1 = new XSSFWorkbook()) {
  44. Cell cell = wb1.createSheet("bug58778").createRow(0).createCell(0);
  45. cell.setCellValue(5.25);
  46. CellStyle style = wb1.createCellStyle();
  47. XSSFDataFormat dataFormat = wb1.createDataFormat();
  48. short poundFmtIdx = 6;
  49. dataFormat.putFormat(poundFmtIdx, POUND_FMT);
  50. style.setDataFormat(poundFmtIdx);
  51. cell.setCellStyle(style);
  52. // Cell should appear as "<poundsymbol>5"
  53. try (XSSFWorkbook wb2 = XSSFTestDataSamples.writeOutCloseAndReadBack(wb1)) {
  54. cell = wb2.getSheet("bug58778").getRow(0).getCell(0);
  55. assertEquals(5.25, cell.getNumericCellValue(), 0);
  56. style = cell.getCellStyle();
  57. assertEquals(POUND_FMT, style.getDataFormatString());
  58. assertEquals(poundFmtIdx, style.getDataFormat());
  59. // manually check the file to make sure the cell is rendered as "<poundsymbol>5"
  60. // Verified with LibreOffice 4.2.8.2 on 2015-12-28
  61. }
  62. }
  63. }
  64. @Test
  65. void testConditionalFormattingEvaluation() throws IOException {
  66. try (Workbook wb = openSampleWorkbook("61060-conditional-number-formatting.xlsx")) {
  67. final DataFormatter formatter = new DataFormatter();
  68. final FormulaEvaluator evaluator = wb.getCreationHelper().createFormulaEvaluator();
  69. final ConditionalFormattingEvaluator cfEvaluator = new ConditionalFormattingEvaluator(wb, (WorkbookEvaluatorProvider) evaluator);
  70. CellReference ref = new CellReference("A1");
  71. Cell cell = wb.getSheetAt(0).getRow(ref.getRow()).getCell(ref.getCol());
  72. assertEquals("0.10", formatter.formatCellValue(cell, evaluator, cfEvaluator));
  73. // verify cell format without the conditional rule applied
  74. assertEquals("0.1", formatter.formatCellValue(cell, evaluator));
  75. ref = new CellReference("A3");
  76. cell = wb.getSheetAt(0).getRow(ref.getRow()).getCell(ref.getCol());
  77. assertEquals("-2.00E+03", formatter.formatCellValue(cell, evaluator, cfEvaluator));
  78. // verify cell format without the conditional rule applied
  79. assertEquals("-2000", formatter.formatCellValue(cell, evaluator));
  80. ref = new CellReference("A4");
  81. cell = wb.getSheetAt(0).getRow(ref.getRow()).getCell(ref.getCol());
  82. assertEquals("100", formatter.formatCellValue(cell, evaluator, cfEvaluator));
  83. ref = new CellReference("A5");
  84. cell = wb.getSheetAt(0).getRow(ref.getRow()).getCell(ref.getCol());
  85. assertEquals("$1,000", formatter.formatCellValue(cell, evaluator, cfEvaluator));
  86. // verify cell format without the conditional rule applied
  87. assertEquals("1000", formatter.formatCellValue(cell, evaluator));
  88. }
  89. }
  90. @Test
  91. void testSetUseCachedValuesForFormulaCells() throws Exception {
  92. try (XSSFWorkbook wb = openSampleWorkbook("formula-eval.xlsx")) {
  93. final DataFormatter formatter = new DataFormatter();
  94. XSSFSheet sheet = wb.getSheetAt(0);
  95. XSSFRow row = sheet.getRow(0);
  96. XSSFCell d1 = row.getCell(3);
  97. assertEquals("SUM(A1:C1)", formatter.formatCellValue(d1));
  98. formatter.setUseCachedValuesForFormulaCells(true);
  99. assertEquals("6.75", formatter.formatCellValue(d1));
  100. }
  101. }
  102. }