From dc12e93abbdc2e9675c31e425ee910764deff1b1 Mon Sep 17 00:00:00 2001 From: PJ Fanning Date: Fri, 2 Sep 2022 11:58:19 +0000 Subject: [PATCH] CellUtil: allow fill colors to be set to null git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1903828 13f79535-47bb-0310-9956-ffa450edef68 --- .../poi/ss/tests/util/TestXSSFCellUtil.java | 32 +++++++++++++++-- .../java/org/apache/poi/ss/util/CellUtil.java | 36 +++++++++++++++---- 2 files changed, 58 insertions(+), 10 deletions(-) diff --git a/poi-ooxml/src/test/java/org/apache/poi/ss/tests/util/TestXSSFCellUtil.java b/poi-ooxml/src/test/java/org/apache/poi/ss/tests/util/TestXSSFCellUtil.java index 096cd0c1ba..d62bb690eb 100644 --- a/poi-ooxml/src/test/java/org/apache/poi/ss/tests/util/TestXSSFCellUtil.java +++ b/poi-ooxml/src/test/java/org/apache/poi/ss/tests/util/TestXSSFCellUtil.java @@ -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 diff --git a/poi/src/main/java/org/apache/poi/ss/util/CellUtil.java b/poi/src/main/java/org/apache/poi/ss/util/CellUtil.java index 89e0fc74fd..7fd48aeae3 100644 --- a/poi/src/main/java/org/apache/poi/ss/util/CellUtil.java +++ b/poi/src/main/java/org/apache/poi/ss/util/CellUtil.java @@ -385,9 +385,14 @@ public final class CellUtil { * @since POI 3.14 beta 2 */ public static void setCellStyleProperties(Cell cell, Map properties) { + setCellStyleProperties(cell, properties, false); + } + + private static void setCellStyleProperties(final Cell cell, final Map properties, + final boolean disableNullColorCheck) { Workbook workbook = cell.getSheet().getWorkbook(); CellStyle originalStyle = cell.getCellStyle(); - + CellStyle newStyle = null; Map values = getFormatProperties(originalStyle); putAll(properties, values); @@ -401,7 +406,7 @@ public final class CellUtil { Map 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 newProps, final Map storedProps) { + private static boolean styleMapsMatch(final Map newProps, + final Map storedProps, final boolean disableNullColorCheck) { final Map map1Copy = new HashMap<>(newProps); final Map 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 property = Collections.singletonMap(propertyName, propertyValue); - setCellStyleProperties(cell, property); + boolean disableNullColorCheck = false; + final Map 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); } /**