Author: fanningpj
Date: Mon May 12 22:27:42 2025
New Revision: 1925524

URL: http://svn.apache.org/viewvc?rev=1925524&view=rev
Log:
try best effort clone of styles if types don't match

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

Modified: 
poi/trunk/poi-ooxml/src/main/java/org/apache/poi/xssf/usermodel/XSSFCellStyle.java
URL: 
http://svn.apache.org/viewvc/poi/trunk/poi-ooxml/src/main/java/org/apache/poi/xssf/usermodel/XSSFCellStyle.java?rev=1925524&r1=1925523&r2=1925524&view=diff
==============================================================================
--- 
poi/trunk/poi-ooxml/src/main/java/org/apache/poi/xssf/usermodel/XSSFCellStyle.java
 (original)
+++ 
poi/trunk/poi-ooxml/src/main/java/org/apache/poi/xssf/usermodel/XSSFCellStyle.java
 Mon May 12 22:27:42 2025
@@ -29,6 +29,7 @@ import org.apache.poi.ss.usermodel.Horiz
 import org.apache.poi.ss.usermodel.IndexedColors;
 import org.apache.poi.ss.usermodel.ReadingOrder;
 import org.apache.poi.ss.usermodel.VerticalAlignment;
+import org.apache.poi.ss.util.CellUtil;
 import org.apache.poi.util.Internal;
 import org.apache.poi.util.Removal;
 import org.apache.poi.xssf.model.StylesTable;
@@ -202,7 +203,7 @@ public class XSSFCellStyle implements Ce
             _font = null;
             _cellAlignment = null;
         } else {
-            throw new IllegalArgumentException("Can only clone from one 
XSSFCellStyle to another, not between HSSFCellStyle and XSSFCellStyle");
+            CellUtil.cloneStyle(source, this, null);
         }
     }
 

Modified: 
poi/trunk/poi/src/main/java/org/apache/poi/hssf/usermodel/HSSFCellStyle.java
URL: 
http://svn.apache.org/viewvc/poi/trunk/poi/src/main/java/org/apache/poi/hssf/usermodel/HSSFCellStyle.java?rev=1925524&r1=1925523&r2=1925524&view=diff
==============================================================================
--- 
poi/trunk/poi/src/main/java/org/apache/poi/hssf/usermodel/HSSFCellStyle.java 
(original)
+++ 
poi/trunk/poi/src/main/java/org/apache/poi/hssf/usermodel/HSSFCellStyle.java 
Mon May 12 22:27:42 2025
@@ -35,6 +35,7 @@ import org.apache.poi.ss.usermodel.FillP
 import org.apache.poi.ss.usermodel.Font;
 import org.apache.poi.ss.usermodel.HorizontalAlignment;
 import org.apache.poi.ss.usermodel.VerticalAlignment;
+import org.apache.poi.ss.util.CellUtil;
 import org.apache.poi.util.Removal;
 import org.apache.poi.util.ThreadLocalUtil;
 
@@ -49,22 +50,29 @@ public final class HSSFCellStyle impleme
     private final ExtendedFormatRecord _format;
     private final short                _index;
     private final InternalWorkbook     _workbook;
+    private final HSSFWorkbook         _hssfWorkbook;
 
-
-    /** Creates new HSSFCellStyle why would you want to do this?? */
     protected HSSFCellStyle(short index, ExtendedFormatRecord rec, 
HSSFWorkbook workbook)
     {
-        this(index, rec, workbook.getWorkbook());
+        _workbook = workbook.getInternalWorkbook();
+        _hssfWorkbook = workbook;
+        _index = index;
+        _format = rec;
     }
+
+    @Deprecated
+    @Removal(version = "7.0.0")
     protected HSSFCellStyle(short index, ExtendedFormatRecord rec, 
InternalWorkbook workbook)
     {
         _workbook = workbook;
+        _hssfWorkbook = null;
         _index = index;
-        _format     = rec;
+        _format = rec;
     }
 
     protected HSSFCellStyle(HSSFCellStyle other) {
         _workbook = other._workbook;
+        _hssfWorkbook = other._hssfWorkbook;
         _index = other._index;
         _format = other._format;
     }
@@ -850,8 +858,8 @@ public final class HSSFCellStyle impleme
     public void cloneStyleFrom(CellStyle source) {
         if(source instanceof HSSFCellStyle) {
             this.cloneStyleFrom((HSSFCellStyle)source);
-        } else {
-            throw new IllegalArgumentException("Can only clone from one 
HSSFCellStyle to another, not between HSSFCellStyle and XSSFCellStyle");
+        } else if (_hssfWorkbook != null) {
+            CellUtil.cloneStyle(source, this, _hssfWorkbook);
         }
     }
     public void cloneStyleFrom(HSSFCellStyle source) {

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=1925524&r1=1925523&r2=1925524&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 Mon May 12 
22:27:42 2025
@@ -47,6 +47,7 @@ import org.apache.poi.ss.usermodel.CellS
 import org.apache.poi.ss.usermodel.VerticalAlignment;
 import org.apache.poi.ss.usermodel.Workbook;
 import org.apache.poi.util.Beta;
+import org.apache.poi.util.Internal;
 import org.apache.poi.util.Removal;
 
 /**
@@ -806,7 +807,7 @@ public final class CellUtil {
      * Sets the format properties of the given style based on the given map.
      *
      * @param style cell style
-     * @param workbook parent workbook
+     * @param workbook parent workbook (can be null but some fomt info will 
not be copied if null is passed)
      * @param properties map of format properties (CellPropertyType -> Object)
      * @see #getFormatProperties(CellStyle)
      */
@@ -848,7 +849,9 @@ public final class CellUtil {
             }
         }
 
-        style.setFont(workbook.getFontAt(getInt(properties, 
CellPropertyType.FONT)));
+        if (workbook != null) {
+            style.setFont(workbook.getFontAt(getInt(properties, 
CellPropertyType.FONT)));
+        }
         style.setHidden(getBoolean(properties, CellPropertyType.HIDDEN));
         style.setIndention(getShort(properties, CellPropertyType.INDENTION));
         style.setLeftBorderColor(getShort(properties, 
CellPropertyType.LEFT_BORDER_COLOR));
@@ -862,6 +865,25 @@ public final class CellUtil {
     }
 
     /**
+     * Clones the style from one cell to another. For internal use only.
+     * Users should use the cloneStyleFrom method on CellStyle instead.
+     *
+     * @param src source cell style
+     * @param dest destination cell style
+     * @param destWorkbook destination workbook (can be null but some font 
info will not be copied if null is passed)
+     * @throws IllegalArgumentException if source or destination styles are 
null
+     * @since POI 5.4.2
+     */
+    @Internal
+    public static void cloneStyle(CellStyle src, CellStyle dest, Workbook 
destWorkbook) {
+        if (src == null || dest == null) {
+            throw new IllegalArgumentException("Source and destination styles 
must not be null");
+        }
+        EnumMap<CellPropertyType, Object> properties = 
getFormatProperties(src);
+        setFormatProperties(dest, destWorkbook, properties);
+    }
+
+    /**
      * Utility method that returns the named short value from the given map.
      *
      * @param properties map of named properties (CellPropertyType -> Object)



---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to