This is an automated email from the ASF dual-hosted git repository.
fanningpj pushed a commit to branch 5.5.x
in repository https://gitbox.apache.org/repos/asf/poi.git
The following commit(s) were added to refs/heads/5.5.x by this push:
new 2f464f21cf nullSafeFormatProperties (#961)
2f464f21cf is described below
commit 2f464f21cf62725e1e27a68da13604da4110b45e
Author: PJ Fanning <[email protected]>
AuthorDate: Fri Dec 5 10:51:57 2025 +0100
nullSafeFormatProperties (#961)
---
poi/src/main/java/org/apache/poi/ss/util/CellUtil.java | 13 ++++++++++---
1 file changed, 10 insertions(+), 3 deletions(-)
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 e1e758fd2a..54a1fa39fc 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
@@ -614,7 +614,7 @@ public final class CellUtil {
CellStyle originalStyle = cell.getCellStyle();
CellStyle newStyle = null;
- EnumMap<CellPropertyType, Object> values =
originalStyle.getFormatProperties();
+ EnumMap<CellPropertyType, Object> values =
nullSafeFormatProperties(originalStyle);
if
(properties.containsKey(CellPropertyType.FILL_FOREGROUND_COLOR_COLOR) &&
properties.get(CellPropertyType.FILL_FOREGROUND_COLOR_COLOR) == null) {
values.remove(CellPropertyType.FILL_FOREGROUND_COLOR);
}
@@ -635,7 +635,7 @@ public final class CellUtil {
for (int i = 0; i < numberCellStyles; i++) {
CellStyle wbStyle = workbook.getCellStyleAt(i);
- EnumMap<CellPropertyType, Object> wbStyleMap =
wbStyle.getFormatProperties();
+ EnumMap<CellPropertyType, Object> wbStyleMap =
nullSafeFormatProperties(wbStyle);
// the desired style already exists in the workbook. Use the
existing style.
if (styleMapsMatch(wbStyleMap, values, disableNullColorCheck)) {
@@ -653,6 +653,13 @@ public final class CellUtil {
cell.setCellStyle(newStyle);
}
+ // try to get format properties from CellStyle but the method may return
null
+ // in some implementations, so we need to be null safe here
+ private static EnumMap<CellPropertyType, Object>
nullSafeFormatProperties(CellStyle style) {
+ EnumMap<CellPropertyType, Object> props = style.getFormatProperties();
+ return props == null ? getFormatProperties(style) : props;
+ }
+
private static boolean styleMapsMatch(final Map<CellPropertyType, Object>
newProps,
final Map<CellPropertyType, Object>
storedProps, final boolean disableNullColorCheck) {
final EnumMap<CellPropertyType, Object> map1Copy = new
EnumMap<>(newProps);
@@ -888,7 +895,7 @@ public final class CellUtil {
if (src == null || dest == null) {
throw new IllegalArgumentException("Source and destination styles
must not be null");
}
- EnumMap<CellPropertyType, Object> properties =
src.getFormatProperties();
+ EnumMap<CellPropertyType, Object> properties =
nullSafeFormatProperties(src);
setFormatProperties(dest, destWorkbook, properties);
}
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]