You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@poi.apache.org by GitBox <gi...@apache.org> on 2022/06/04 08:17:07 UTC

[GitHub] [poi] funakawa718 commented on a diff in pull request #334: Add SHRINK_TO_FIT and QUOTE_PREFIXED to CellUtil

funakawa718 commented on code in PR #334:
URL: https://github.com/apache/poi/pull/334#discussion_r889502782


##########
poi/src/test/java/org/apache/poi/ss/util/BaseTestCellUtil.java:
##########
@@ -104,6 +105,126 @@ void setCellStylePropertyBorderWithShortAndEnum() throws IOException {
         }
     }
 
+    @Test()
+    void setCellStylePropertyWithShrinkToFit() throws IOException {
+        try (Workbook wb = _testDataProvider.createWorkbook()) {
+            Sheet s = wb.createSheet();
+            Row r = s.createRow(0);
+            Cell c = r.createCell(0);
+
+            // Assert that the default shrinkToFit is false
+            assertFalse(c.getCellStyle().getShrinkToFit());
+
+            // Set shrinkToFit to true
+            CellUtil.setCellStyleProperty(c, CellUtil.SHRINK_TO_FIT, true);
+            assertTrue(c.getCellStyle().getShrinkToFit());
+        }
+    }
+
+    @Test()
+    void setCellStylePropertyWithQuotePrefixed() throws IOException {
+        try (Workbook wb = _testDataProvider.createWorkbook()) {
+            Sheet s = wb.createSheet();
+            Row r = s.createRow(0);
+            Cell c = r.createCell(0);
+
+            // Assert that the default quotePrefixed is false
+            assertFalse(c.getCellStyle().getQuotePrefixed());
+
+            // Set quotePrefixed to true
+            CellUtil.setCellStyleProperty(c, CellUtil.QUOTE_PREFIXED, true);
+            assertFalse(c.getCellStyle().getShrinkToFit());
+        }
+    }
+
+    @Test()
+    void setCellStylePropertyWithExistingStyles() throws IOException {
+        try (Workbook wb = _testDataProvider.createWorkbook()) {
+            Sheet s = wb.createSheet();
+            Row r = s.createRow(0);
+            Cell c = r.createCell(0);
+            Font f = wb.createFont();
+            f.setBold(true);
+
+            // Assert that the default cell style is not the same as the one being changed
+            assertNotEquals(HorizontalAlignment.CENTER, c.getCellStyle().getAlignment());
+            assertNotEquals(BorderStyle.THIN, c.getCellStyle().getBorderBottom());
+            assertNotEquals(BorderStyle.THIN, c.getCellStyle().getBorderLeft());
+            assertNotEquals(BorderStyle.THIN, c.getCellStyle().getBorderRight());
+            assertNotEquals(BorderStyle.THIN, c.getCellStyle().getBorderTop());
+            assertNotEquals(IndexedColors.RED.index, c.getCellStyle().getBottomBorderColor());
+            assertNotEquals(IndexedColors.RED.index, c.getCellStyle().getLeftBorderColor());
+            assertNotEquals(IndexedColors.RED.index, c.getCellStyle().getRightBorderColor());
+            assertNotEquals(IndexedColors.RED.index, c.getCellStyle().getTopBorderColor());
+            assertNotEquals(wb.createDataFormat().getFormat("#,##0"), c.getCellStyle().getDataFormat());
+            assertNotEquals(IndexedColors.BLUE.index, c.getCellStyle().getFillForegroundColor());
+            assertNotEquals(IndexedColors.BLUE.index, c.getCellStyle().getFillBackgroundColor());
+            assertNotEquals(FillPatternType.DIAMONDS, c.getCellStyle().getFillPattern());
+            assertNotEquals(f.getIndex(), c.getCellStyle().getFontIndex());
+            assertFalse(c.getCellStyle().getHidden());
+            assertNotEquals((short) 3, c.getCellStyle().getIndention());
+            assertTrue(c.getCellStyle().getLocked());
+            assertNotEquals((short) 45, c.getCellStyle().getRotation());
+            assertNotEquals(VerticalAlignment.CENTER, c.getCellStyle().getVerticalAlignment());
+            assertFalse(c.getCellStyle().getWrapText());
+            assertFalse(c.getCellStyle().getShrinkToFit());
+            assertFalse(c.getCellStyle().getQuotePrefixed());
+
+            // Set all styles
+            CellStyle cs = wb.createCellStyle();
+            cs.setAlignment(HorizontalAlignment.CENTER);
+            cs.setBorderBottom(BorderStyle.THIN);
+            cs.setBorderLeft(BorderStyle.THIN);
+            cs.setBorderRight(BorderStyle.THIN);
+            cs.setBorderTop(BorderStyle.THIN);
+            cs.setBottomBorderColor(IndexedColors.RED.index);
+            cs.setLeftBorderColor(IndexedColors.RED.index);
+            cs.setRightBorderColor(IndexedColors.RED.index);
+            cs.setTopBorderColor(IndexedColors.RED.index);
+            cs.setDataFormat(wb.createDataFormat().getFormat("#,##0"));
+            cs.setFillForegroundColor(IndexedColors.BLUE.index);
+            cs.setFillBackgroundColor(IndexedColors.BLUE.index);
+            cs.setFillPattern(FillPatternType.DIAMONDS);
+            cs.setFont(f);
+            cs.setHidden(true);
+            cs.setIndention((short) 3);
+            cs.setLocked(false);
+            cs.setRotation((short) 45);
+            cs.setVerticalAlignment(VerticalAlignment.CENTER);
+            cs.setWrapText(true);
+            cs.setShrinkToFit(true);
+            cs.setQuotePrefixed(true);
+            c.setCellStyle(cs);
+
+            // Set BorderBottom from THIN to DOUBLE with setCellStyleProperty()
+            CellUtil.setCellStyleProperty(c, CellUtil.BORDER_BOTTOM, BorderStyle.DOUBLE);
+
+            // Assert that only BorderBottom has been changed and no others.
+            assertEquals(BorderStyle.DOUBLE, c.getCellStyle().getBorderBottom());
+            assertEquals(HorizontalAlignment.CENTER, c.getCellStyle().getAlignment());
+            assertEquals(BorderStyle.THIN, c.getCellStyle().getBorderLeft());
+            assertEquals(BorderStyle.THIN, c.getCellStyle().getBorderRight());
+            assertEquals(BorderStyle.THIN, c.getCellStyle().getBorderTop());
+            assertEquals(IndexedColors.RED.index, c.getCellStyle().getBottomBorderColor());
+            assertEquals(IndexedColors.RED.index, c.getCellStyle().getLeftBorderColor());
+            assertEquals(IndexedColors.RED.index, c.getCellStyle().getRightBorderColor());
+            assertEquals(IndexedColors.RED.index, c.getCellStyle().getTopBorderColor());
+            assertEquals(wb.createDataFormat().getFormat("#,##0"), c.getCellStyle().getDataFormat());
+            assertEquals(IndexedColors.BLUE.index, c.getCellStyle().getFillForegroundColor());
+            assertEquals(IndexedColors.BLUE.index, c.getCellStyle().getFillBackgroundColor());
+            assertEquals(FillPatternType.DIAMONDS, c.getCellStyle().getFillPattern());
+            assertEquals(f.getIndex(), c.getCellStyle().getFontIndex());
+            assertTrue(c.getCellStyle().getHidden());
+            assertEquals((short) 3, c.getCellStyle().getIndention());
+            assertFalse(c.getCellStyle().getLocked());
+            assertEquals((short) 45, c.getCellStyle().getRotation());
+            assertEquals(VerticalAlignment.CENTER, c.getCellStyle().getVerticalAlignment());
+            assertTrue(c.getCellStyle().getWrapText());
+            assertTrue(c.getCellStyle().getShrinkToFit());
+            assertTrue(c.getCellStyle().getQuotePrefixed());

Review Comment:
   Before the fix, `c.getCellStyle().getShrinkToFit()` would be `false` here and the test would fail.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: dev-unsubscribe@poi.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@poi.apache.org
For additional commands, e-mail: dev-help@poi.apache.org