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 6.8KB

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