]> source.dussan.org Git - poi.git/commitdiff
CellUtil: allow fill colors to be set to null
authorPJ Fanning <fanningpj@apache.org>
Fri, 2 Sep 2022 11:58:19 +0000 (11:58 +0000)
committerPJ Fanning <fanningpj@apache.org>
Fri, 2 Sep 2022 11:58:19 +0000 (11:58 +0000)
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1903828 13f79535-47bb-0310-9956-ffa450edef68

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

index 096cd0c1ba2a6363d20b3c1a73d60cc231a5202b..d62bb690ebe1a2f0474157bba381741638bf1ce8 100644 (file)
@@ -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 BaseTestCellUtil {
             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
index 89e0fc74fdade27aad756804863c3304d2d37587..7fd48aeae3ddeed03dd72ece861bec552c4fbe8d 100644 (file)
@@ -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);
     }
 
     /**