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
This commit is contained in:
PJ Fanning 2022-09-02 11:58:19 +00:00
parent fe8c8eca89
commit dc12e93abb
2 changed files with 58 additions and 10 deletions

View 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());
}
}
}

View 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);
}
/**