]> source.dussan.org Git - poi.git/commitdiff
add param that allows invalid colors to be ignored
authorPJ Fanning <fanningpj@apache.org>
Sun, 10 Jul 2022 12:22:54 +0000 (12:22 +0000)
committerPJ Fanning <fanningpj@apache.org>
Sun, 10 Jul 2022 12:22:54 +0000 (12:22 +0000)
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1902630 13f79535-47bb-0310-9956-ffa450edef68

poi-ooxml/src/main/java/org/apache/poi/xssf/usermodel/XSSFCellStyle.java
poi/src/main/java/org/apache/poi/hssf/usermodel/HSSFCellStyle.java
poi/src/main/java/org/apache/poi/ss/usermodel/CellStyle.java
poi/src/main/java/org/apache/poi/ss/util/CellUtil.java

index fcfb8e7084d5041e6341ef7bc9c5d8f56b926e1e..40949f441577fe27c22d22a93166698e083f0341 100644 (file)
@@ -832,15 +832,17 @@ public class XSSFCellStyle implements CellStyle, Duplicatable {
     /**
      * Set the background fill color represented as a {@link org.apache.poi.ss.usermodel.Color} value.
      * <br>
-     * @param color the color to use
+     * @param color org.apache.poi.ss.usermodel.Color to set
+     * @param ignoreInvalidColors when set to true, the method will ignore issues like trying to set a
+     *                            HSSFColor on a XSSFCellStyle
      * @throws IllegalArgumentException if you provide a <code>Color</code> instance that is not a {@link XSSFColor}
      * @since POI 5.2.3
      */
     @Override
-    public void setFillBackgroundColor(org.apache.poi.ss.usermodel.Color color) {
+    public void setFillBackgroundColor(org.apache.poi.ss.usermodel.Color color, boolean ignoreInvalidColors) {
         if (color == null || color instanceof XSSFColor) {
             setFillBackgroundColor((XSSFColor)color);
-        } else {
+        } else if (!ignoreInvalidColors) {
             throw new IllegalArgumentException("XSSFCellStyle only accepts XSSFColor instances");
         }
     }
@@ -903,14 +905,16 @@ public class XSSFCellStyle implements CellStyle, Duplicatable {
      * Set the foreground fill color represented as a {@link org.apache.poi.ss.usermodel.Color} value.
      * <br>
      * @param color the color to use
+     * @param ignoreInvalidColors when set to true, the method will ignore issues like trying to set a
+     *                            HSSFColor on a XSSFCellStyle
      * @throws IllegalArgumentException if you provide a <code>Color</code> instance that is not a {@link XSSFColor}
      * @since POI 5.2.3
      */
     @Override
-    public void setFillForegroundColor(org.apache.poi.ss.usermodel.Color color) {
+    public void setFillForegroundColor(org.apache.poi.ss.usermodel.Color color, boolean ignoreInvalidColors) {
         if (color == null || color instanceof XSSFColor) {
             setFillForegroundColor((XSSFColor)color);
-        } else {
+        } else if (!ignoreInvalidColors) {
             throw new IllegalArgumentException("XSSFCellStyle only accepts XSSFColor instances");
         }
     }
index 55621d9095aea4af6d360a06f7c04bfb6d3293d8..6a34ca930faf04db415e25b7b7e4c55994cabcb7 100644 (file)
@@ -642,16 +642,18 @@ public final class HSSFCellStyle implements CellStyle, Duplicatable {
      * Set the background fill color represented as a {@link org.apache.poi.ss.usermodel.Color} value.
      * <br>
      * @param color the color to use
+     * @param ignoreInvalidColors when set to true, the method will ignore issues like trying to set a
+     *                            HSSFColor on a XSSFCellStyle
      * @throws IllegalArgumentException if you provide a <code>Color</code> instance that is not a {@link HSSFColor}
      * @since POI 5.2.3
      */
     @Override
-    public void setFillBackgroundColor(org.apache.poi.ss.usermodel.Color color)
+    public void setFillBackgroundColor(org.apache.poi.ss.usermodel.Color color, boolean ignoreInvalidColors)
     {
         if (color instanceof HSSFColor) {
             short index2 = ((HSSFColor)color).getIndex2();
             if (index2 != -1) setFillBackgroundColor(index2);
-        } else if (color != null) {
+        } else if (color != null && !ignoreInvalidColors) {
             throw new IllegalArgumentException("HSSFCellStyle only accepts HSSFColor instances");
         }
     }
@@ -701,16 +703,18 @@ public final class HSSFCellStyle implements CellStyle, Duplicatable {
      * Set the foreground fill color represented as a {@link org.apache.poi.ss.usermodel.Color} value.
      * <br>
      * @param color the color to use
+     * @param ignoreInvalidColors when set to true, the method will ignore issues like trying to set a
+     *                            HSSFColor on a XSSFCellStyle
      * @throws IllegalArgumentException if you provide a <code>Color</code> instance that is not a {@link HSSFColor}
      * @since POI 5.2.3
      */
     @Override
-    public void setFillForegroundColor(org.apache.poi.ss.usermodel.Color color)
+    public void setFillForegroundColor(org.apache.poi.ss.usermodel.Color color, boolean ignoreInvalidColors)
     {
         if (color instanceof HSSFColor) {
             short index2 = ((HSSFColor)color).getIndex2();
             if (index2 != -1) setFillForegroundColor(index2);
-        } else if (color != null) {
+        } else if (color != null && !ignoreInvalidColors) {
             throw new IllegalArgumentException("HSSFCellStyle only accepts HSSFColor instances");
         }
     }
index c4e539da8b993572deb1d95dc24275fd9d1e266d..cc446886c04499892d2334dd120ef50642355b72 100644 (file)
@@ -312,13 +312,26 @@ public interface CellStyle {
     void setFillBackgroundColor(short bg);
 
     /**
-     * set the background fill color.
-     * use not a indexed color but a {@link org.apache.poi.ss.usermodel.Color)
+     * Set the background fill color.
+     * Uses a {@link org.apache.poi.ss.usermodel.Color} instead of an indexed color.
+     *
+     * @param color org.apache.poi.ss.usermodel.Color to set
+     * @since POI 5.2.3
+     */
+    default void setFillBackgroundColor(Color color) {
+        setFillBackgroundColor(color, false);
+    }
+
+    /**
+     * Set the background fill color.
+     * Uses a {@link org.apache.poi.ss.usermodel.Color} instead of an indexed color.
      *
      * @param color org.apache.poi.ss.usermodel.Color to set
+     * @param ignoreInvalidColors when set to true, the method will ignore issues like trying to set a
+     *                            HSSFColor on a XSSFCellStyle
      * @since POI 5.2.3
      */
-    void setFillBackgroundColor(Color color);
+    void setFillBackgroundColor(Color color, boolean ignoreInvalidColors);
 
     /**
      * get the background fill color, if the fill
@@ -344,13 +357,26 @@ public interface CellStyle {
     void setFillForegroundColor(short bg);
 
     /**
-     * set the foreground fill color.
-     * use not a indexed color but a {@link org.apache.poi.ss.usermodel.Color)
+     * Set the foreground fill color.
+     * Uses a {@link org.apache.poi.ss.usermodel.Color} instead of an indexed color.
+     *
+     * @param color org.apache.poi.ss.usermodel.Color to set
+     * @since POI 5.2.3
+     */
+    default void setFillForegroundColor(Color color) {
+        setFillForegroundColor(color, false);
+    }
+
+    /**
+     * Set the foreground fill color.
+     * Uses a {@link org.apache.poi.ss.usermodel.Color} instead of an indexed color.
      *
      * @param color org.apache.poi.ss.usermodel.Color to set
+     * @param ignoreInvalidColors when set to true, the method will ignore issues like trying to set a
+     *                            HSSFColor on a XSSFCellStyle
      * @since POI 5.2.3
      */
-    void setFillForegroundColor(Color color);
+    void setFillForegroundColor(Color color, boolean ignoreInvalidColors);
 
     /**
      * get the foreground fill color, if the fill  
index 0665cefa385f7d97c3eed4c96cc053a4f08f679e..8bab37d4da0326996250a2ffe992cd111a48ae86 100644 (file)
@@ -532,8 +532,8 @@ public final class CellUtil {
         
         style.setFillForegroundColor(getShort(properties, FILL_FOREGROUND_COLOR));
         style.setFillBackgroundColor(getShort(properties, FILL_BACKGROUND_COLOR));
-        style.setFillForegroundColor(getColor(properties, FILL_FOREGROUND_COLOR_COLOR));
-        style.setFillBackgroundColor(getColor(properties, FILL_BACKGROUND_COLOR_COLOR));
+        style.setFillForegroundColor(getColor(properties, FILL_FOREGROUND_COLOR_COLOR), true);
+        style.setFillBackgroundColor(getColor(properties, FILL_BACKGROUND_COLOR_COLOR), true);
 
         style.setFont(workbook.getFontAt(getInt(properties, FONT)));
         style.setHidden(getBoolean(properties, HIDDEN));