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 22:54:04 UTC

svn commit: r1903837 - 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 22:54:03 2022
New Revision: 1903837

URL: http://svn.apache.org/viewvc?rev=1903837&view=rev
Log:
add CellUtil test

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=1903837&r1=1903836&r2=1903837&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 22:54:03 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.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.XSS
 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.*;
 
@@ -82,5 +87,38 @@ class TestXSSFCellUtil extends BaseTestC
             assertNull(cell.getCellStyle().getFillForegroundColorColor());
             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

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=1903837&r1=1903836&r2=1903837&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 22:54:03 2022
@@ -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.



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