]> source.dussan.org Git - poi.git/commitdiff
add CellUtil test
authorPJ Fanning <fanningpj@apache.org>
Fri, 2 Sep 2022 22:54:03 +0000 (22:54 +0000)
committerPJ Fanning <fanningpj@apache.org>
Fri, 2 Sep 2022 22:54:03 +0000 (22:54 +0000)
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1903837 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 8a6f85e85af8cb8c219a94b2749210f14b0b5921..8b0547cb694fd7f31390d74ae61c43400dae5a85 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.FillPatternType;
 import org.apache.poi.ss.usermodel.IndexedColors;
 import org.apache.poi.ss.usermodel.Row;
 import org.apache.poi.ss.usermodel.Sheet;
@@ -31,7 +32,11 @@ import org.apache.poi.xssf.usermodel.XSSFColor;
 import org.apache.poi.xssf.usermodel.XSSFWorkbook;
 import org.junit.jupiter.api.Test;
 
+import java.io.FileOutputStream;
 import java.io.IOException;
+import java.io.OutputStream;
+import java.util.LinkedHashMap;
+import java.util.Map;
 
 import static org.junit.jupiter.api.Assertions.*;
 
@@ -83,4 +88,37 @@ class TestXSSFCellUtil extends BaseTestCellUtil {
             assertEquals(IndexedColors.AUTOMATIC.getIndex(), cell.getCellStyle().getFillForegroundColor());
         }
     }
+
+    @Test
+    public void testSetForegroundColorCellStylePropertiesToNull() 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("FF0000"));
+
+            {
+                final Map<String, Object> properties = new LinkedHashMap<>();
+
+                properties.put(CellUtil.FILL_FOREGROUND_COLOR_COLOR, color);
+                properties.put(CellUtil.FILL_PATTERN, FillPatternType.SOLID_FOREGROUND);
+
+                CellUtil.setCellStyleProperties(cell, properties);
+            }
+            assertEquals(color, cell.getCellStyle().getFillForegroundColorColor());
+
+            {
+                final Map<String, Object> properties = new LinkedHashMap<>();
+
+                properties.put(CellUtil.FILL_FOREGROUND_COLOR_COLOR, null);
+                properties.put(CellUtil.FILL_PATTERN, FillPatternType.NO_FILL);
+
+                CellUtil.setCellStyleProperties(cell, properties);
+            }
+            assertNull(cell.getCellStyle().getFillForegroundColorColor());
+            assertEquals(IndexedColors.AUTOMATIC.getIndex(), cell.getCellStyle().getFillForegroundColor());
+        }
+    }
 }
\ No newline at end of file
index 44ee0b53c10749f38db033d62dda1b46feb2abd5..0ba11231614e9f8da897edd451241f923203039f 100644 (file)
@@ -395,6 +395,12 @@ public final class CellUtil {
 
         CellStyle newStyle = null;
         Map<String, Object> values = getFormatProperties(originalStyle);
+        if (properties.containsKey(FILL_FOREGROUND_COLOR_COLOR) && properties.get(FILL_FOREGROUND_COLOR_COLOR) == null) {
+            values.remove(FILL_FOREGROUND_COLOR);
+        }
+        if (properties.containsKey(FILL_BACKGROUND_COLOR_COLOR) && properties.get(FILL_BACKGROUND_COLOR_COLOR) == null) {
+            values.remove(FILL_BACKGROUND_COLOR);
+        }
         putAll(properties, values);
 
         // index seems like what index the cellstyle is in the list of styles for a workbook.