You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@poi.apache.org by fa...@apache.org on 2022/09/02 11:58:19 UTC

svn commit: r1903828 - in /poi/trunk: poi-ooxml/src/test/java/org/apache/poi/ss/tests/util/TestXSSFCellUtil.java poi/src/main/java/org/apache/poi/ss/util/CellUtil.java

Author: fanningpj
Date: Fri Sep  2 11:58:19 2022
New Revision: 1903828

URL: http://svn.apache.org/viewvc?rev=1903828&view=rev
Log:
CellUtil: allow fill colors to be set to null

Modified:
    poi/trunk/poi-ooxml/src/test/java/org/apache/poi/ss/tests/util/TestXSSFCellUtil.java
    poi/trunk/poi/src/main/java/org/apache/poi/ss/util/CellUtil.java

Modified: poi/trunk/poi-ooxml/src/test/java/org/apache/poi/ss/tests/util/TestXSSFCellUtil.java
URL: http://svn.apache.org/viewvc/poi/trunk/poi-ooxml/src/test/java/org/apache/poi/ss/tests/util/TestXSSFCellUtil.java?rev=1903828&r1=1903827&r2=1903828&view=diff
==============================================================================
--- poi/trunk/poi-ooxml/src/test/java/org/apache/poi/ss/tests/util/TestXSSFCellUtil.java (original)
+++ poi/trunk/poi-ooxml/src/test/java/org/apache/poi/ss/tests/util/TestXSSFCellUtil.java Fri Sep  2 11:58:19 2022
@@ -20,6 +20,7 @@ package org.apache.poi.ss.tests.util;
 import org.apache.commons.codec.DecoderException;
 import org.apache.commons.codec.binary.Hex;
 import org.apache.poi.ss.usermodel.Cell;
+import org.apache.poi.ss.usermodel.IndexedColors;
 import org.apache.poi.ss.usermodel.Row;
 import org.apache.poi.ss.usermodel.Sheet;
 import org.apache.poi.ss.usermodel.Workbook;
@@ -32,8 +33,7 @@ import org.junit.jupiter.api.Test;
 
 import java.io.IOException;
 
-import static org.junit.jupiter.api.Assertions.assertNotNull;
-import static org.junit.jupiter.api.Assertions.assertNull;
+import static org.junit.jupiter.api.Assertions.*;
 
 class TestXSSFCellUtil extends BaseTestCellUtil {
     public TestXSSFCellUtil() {
@@ -54,7 +54,33 @@ class TestXSSFCellUtil extends BaseTestC
             CellUtil.setCellStyleProperty(
                     cell, CellUtil.FILL_FOREGROUND_COLOR_COLOR, color);
 
-            assertNotNull(cell.getCellStyle().getFillForegroundColorColor());
+            assertEquals(color, cell.getCellStyle().getFillForegroundColorColor());
+        }
+    }
+
+    @Test
+    public void testSetForegroundColorCellStylePropertyToNull() throws IOException, DecoderException {
+
+        try (Workbook workbook = new XSSFWorkbook()) {
+
+            final Sheet sheet = workbook.createSheet("Sheet");
+            final Row row = sheet.createRow(0);
+            final Cell cell = row.createCell(0);
+            final XSSFColor color = new XSSFColor(Hex.decodeHex("AAAAAA"));
+
+            assertNull(cell.getCellStyle().getFillForegroundColorColor());
+
+            CellUtil.setCellStyleProperty(
+                    cell, CellUtil.FILL_FOREGROUND_COLOR_COLOR, color);
+
+            assertEquals(color, cell.getCellStyle().getFillForegroundColorColor());
+
+            CellUtil.setCellStyleProperty(
+                    cell, CellUtil.FILL_FOREGROUND_COLOR_COLOR, null);
+
+            assertNotEquals(color, cell.getCellStyle().getFillForegroundColorColor());
+            assertEquals(IndexedColors.AUTOMATIC.getIndex(),
+                    ((XSSFColor) cell.getCellStyle().getFillForegroundColorColor()).getIndex());
         }
     }
 }
\ No newline at end of file

Modified: poi/trunk/poi/src/main/java/org/apache/poi/ss/util/CellUtil.java
URL: http://svn.apache.org/viewvc/poi/trunk/poi/src/main/java/org/apache/poi/ss/util/CellUtil.java?rev=1903828&r1=1903827&r2=1903828&view=diff
==============================================================================
--- poi/trunk/poi/src/main/java/org/apache/poi/ss/util/CellUtil.java (original)
+++ poi/trunk/poi/src/main/java/org/apache/poi/ss/util/CellUtil.java Fri Sep  2 11:58:19 2022
@@ -385,9 +385,14 @@ public final class CellUtil {
      * @since POI 3.14 beta 2
      */
     public static void setCellStyleProperties(Cell cell, Map<String, Object> properties) {
+        setCellStyleProperties(cell, properties, false);
+    }
+
+    private static void setCellStyleProperties(final Cell cell, final Map<String, Object> properties,
+                                               final boolean disableNullColorCheck) {
         Workbook workbook = cell.getSheet().getWorkbook();
         CellStyle originalStyle = cell.getCellStyle();
-        
+
         CellStyle newStyle = null;
         Map<String, Object> values = getFormatProperties(originalStyle);
         putAll(properties, values);
@@ -401,7 +406,7 @@ public final class CellUtil {
             Map<String, Object> wbStyleMap = getFormatProperties(wbStyle);
 
             // the desired style already exists in the workbook. Use the existing style.
-            if (styleMapsMatch(wbStyleMap, values)) {
+            if (styleMapsMatch(wbStyleMap, values, disableNullColorCheck)) {
                 newStyle = wbStyle;
                 break;
             }
@@ -416,7 +421,8 @@ public final class CellUtil {
         cell.setCellStyle(newStyle);
     }
 
-    private static boolean styleMapsMatch(final Map<String, Object> newProps, final Map<String, Object> storedProps) {
+    private static boolean styleMapsMatch(final Map<String, Object> newProps,
+                                          final Map<String, Object> storedProps, final boolean disableNullColorCheck) {
         final Map<String, Object> map1Copy = new HashMap<>(newProps);
         final Map<String, Object> map2Copy = new HashMap<>(storedProps);
         final Object backColor1 = map1Copy.remove(FILL_BACKGROUND_COLOR_COLOR);
@@ -424,8 +430,10 @@ public final class CellUtil {
         final Object foreColor1 = map1Copy.remove(FILL_FOREGROUND_COLOR_COLOR);
         final Object foreColor2 = map2Copy.remove(FILL_FOREGROUND_COLOR_COLOR);
         if (map1Copy.equals(map2Copy)) {
-            final boolean backColorsMatch = backColor2 == null || Objects.equals(backColor1, backColor2);
-            final boolean foreColorsMatch = foreColor2 == null || Objects.equals(foreColor1, foreColor2);
+            final boolean backColorsMatch = (!disableNullColorCheck && backColor2 == null)
+                    || Objects.equals(backColor1, backColor2);
+            final boolean foreColorsMatch = (!disableNullColorCheck && foreColor2 == null)
+                    || Objects.equals(foreColor1, foreColor2);
             return backColorsMatch && foreColorsMatch;
         }
         return false;
@@ -449,8 +457,22 @@ public final class CellUtil {
      * @param propertyValue The value of the property that is to be changed.
      */
     public static void setCellStyleProperty(Cell cell, String propertyName, Object propertyValue) {
-        Map<String, Object> property = Collections.singletonMap(propertyName, propertyValue);
-        setCellStyleProperties(cell, property);
+        boolean disableNullColorCheck = false;
+        final Map<String, Object> propMap;
+        if (CellUtil.FILL_FOREGROUND_COLOR_COLOR.equals(propertyName) && propertyValue == null) {
+            disableNullColorCheck = true;
+            propMap = new HashMap<>();
+            propMap.put(CellUtil.FILL_FOREGROUND_COLOR_COLOR, null);
+            propMap.put(CellUtil.FILL_FOREGROUND_COLOR, IndexedColors.AUTOMATIC.getIndex());
+        } else if (CellUtil.FILL_BACKGROUND_COLOR_COLOR.equals(propertyName) && propertyValue == null) {
+            disableNullColorCheck = true;
+            propMap = new HashMap<>();
+            propMap.put(CellUtil.FILL_BACKGROUND_COLOR_COLOR, null);
+            propMap.put(CellUtil.FILL_BACKGROUND_COLOR, IndexedColors.AUTOMATIC.getIndex());
+        } else {
+            propMap = Collections.singletonMap(propertyName, propertyValue);
+        }
+        setCellStyleProperties(cell, propMap, disableNullColorCheck);
     }
 
     /**



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