You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@poi.apache.org by ce...@apache.org on 2015/03/14 20:32:28 UTC

svn commit: r1666736 - in /poi/trunk: src/ooxml/java/org/apache/poi/xssf/usermodel/ src/ooxml/testcases/org/apache/poi/xssf/usermodel/ test-data/spreadsheet/

Author: centic
Date: Sat Mar 14 19:32:28 2015
New Revision: 1666736

URL: http://svn.apache.org/r1666736
Log:
Bug 56295: Fix cloning of styles across workbooks and handling of default value of attribute applyFill

Added:
    poi/trunk/test-data/spreadsheet/56295.xlsx
Modified:
    poi/trunk/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFCellStyle.java
    poi/trunk/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFBugs.java
    poi/trunk/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFCellStyle.java

Modified: poi/trunk/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFCellStyle.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFCellStyle.java?rev=1666736&r1=1666735&r2=1666736&view=diff
==============================================================================
--- poi/trunk/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFCellStyle.java (original)
+++ poi/trunk/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFCellStyle.java Sat Mar 14 19:32:28 2015
@@ -154,6 +154,13 @@ public class XSSFCellStyle implements Ce
                   _cellXf = CTXf.Factory.parse(
                         src.getCoreXf().toString()
                   );
+                  
+                  // bug 56295: ensure that the fills is available and set correctly
+                  CTFill fill = CTFill.Factory.parse(
+                		  src.getCTFill().toString()
+                		  );
+                  addFill(fill);
+                  
                   // Swap it over
                   _stylesSource.replaceCellXfAt(_cellXfId, _cellXf);
                } catch(XmlException e) {
@@ -187,6 +194,13 @@ public class XSSFCellStyle implements Ce
         }
     }
 
+	private void addFill(CTFill fill) {
+		int idx = _stylesSource.putFill(new XSSFCellFill(fill));
+
+		_cellXf.setFillId(idx);
+		_cellXf.setApplyFill(true);
+	}
+
     /**
      * Get the type of horizontal alignment for the cell
      *
@@ -444,7 +458,8 @@ public class XSSFCellStyle implements Ce
      * @return XSSFColor - fill color or <code>null</code> if not set
      */
     public XSSFColor getFillBackgroundXSSFColor() {
-        if(!_cellXf.getApplyFill()) return null;
+    	// bug 56295: handle missing applyFill attribute as "true" because Excel does as well
+        if(_cellXf.isSetApplyFill() && !_cellXf.getApplyFill()) return null;
 
         int fillIndex = (int)_cellXf.getFillId();
         XSSFCellFill fg = _stylesSource.getFillAt(fillIndex);
@@ -480,7 +495,8 @@ public class XSSFCellStyle implements Ce
      * @return XSSFColor - fill color or <code>null</code> if not set
      */
     public XSSFColor getFillForegroundXSSFColor() {
-        if(!_cellXf.getApplyFill()) return null;
+    	// bug 56295: handle missing applyFill attribute as "true" because Excel does as well
+        if(_cellXf.isSetApplyFill() && !_cellXf.getApplyFill()) return null;
 
         int fillIndex = (int)_cellXf.getFillId();
         XSSFCellFill fg = _stylesSource.getFillAt(fillIndex);
@@ -515,7 +531,8 @@ public class XSSFCellStyle implements Ce
      * @see org.apache.poi.ss.usermodel.CellStyle#DIAMONDS
      */
     public short getFillPattern() {
-        if(!_cellXf.getApplyFill()) return 0;
+    	// bug 56295: handle missing applyFill attribute as "true" because Excel does as well
+        if(_cellXf.isSetApplyFill() && !_cellXf.getApplyFill()) return 0;
 
         int fillIndex = (int)_cellXf.getFillId();
         XSSFCellFill fill = _stylesSource.getFillAt(fillIndex);
@@ -996,10 +1013,7 @@ public class XSSFCellStyle implements Ce
             ptrn.setBgColor(color.getCTColor());
         }
 
-        int idx = _stylesSource.putFill(new XSSFCellFill(ct));
-
-        _cellXf.setFillId(idx);
-        _cellXf.setApplyFill(true);
+        addFill(ct);
     }
 
     /**
@@ -1052,10 +1066,7 @@ public class XSSFCellStyle implements Ce
             ptrn.setFgColor(color.getCTColor());
         }
 
-        int idx = _stylesSource.putFill(new XSSFCellFill(ct));
-
-        _cellXf.setFillId(idx);
-        _cellXf.setApplyFill(true);
+        addFill(ct);
     }
 
     /**
@@ -1076,7 +1087,8 @@ public class XSSFCellStyle implements Ce
      */
     private CTFill getCTFill(){
         CTFill ct;
-        if(_cellXf.getApplyFill()) {
+    	// bug 56295: handle missing applyFill attribute as "true" because Excel does as well
+        if(!_cellXf.isSetApplyFill() || _cellXf.getApplyFill()) {
             int fillIndex = (int)_cellXf.getFillId();
             XSSFCellFill cf = _stylesSource.getFillAt(fillIndex);
 
@@ -1135,10 +1147,7 @@ public class XSSFCellStyle implements Ce
         if(fp == NO_FILL && ptrn.isSetPatternType()) ptrn.unsetPatternType();
         else ptrn.setPatternType(STPatternType.Enum.forInt(fp + 1));
 
-        int idx = _stylesSource.putFill(new XSSFCellFill(ct));
-
-        _cellXf.setFillId(idx);
-        _cellXf.setApplyFill(true);
+        addFill(ct);
     }
 
     /**

Modified: poi/trunk/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFBugs.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFBugs.java?rev=1666736&r1=1666735&r2=1666736&view=diff
==============================================================================
--- poi/trunk/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFBugs.java (original)
+++ poi/trunk/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFBugs.java Sat Mar 14 19:32:28 2015
@@ -31,6 +31,7 @@ import java.io.File;
 import java.io.FileInputStream;
 import java.io.FileOutputStream;
 import java.io.IOException;
+import java.io.InputStream;
 import java.io.OutputStream;
 import java.util.Calendar;
 import java.util.List;
@@ -2308,4 +2309,60 @@ public final class TestXSSFBugs extends
         // TODO Re-check sheet contents
         // TODO Re-check formula evaluation
     }
+
+    @Test
+    public void testBug56295_MergeXlslsWithStyles() throws IOException {
+        XSSFWorkbook xlsToAppendWorkbook = XSSFTestDataSamples.openSampleWorkbook("56295.xlsx");
+        XSSFSheet sheet = xlsToAppendWorkbook.getSheetAt(0);
+        XSSFRow srcRow = sheet.getRow(0);
+        XSSFCell oldCell = srcRow.getCell(0);
+        XSSFCellStyle cellStyle = oldCell.getCellStyle();
+        
+        checkStyle(cellStyle);
+        
+//        StylesTable table = xlsToAppendWorkbook.getStylesSource();
+//        List<XSSFCellFill> fills = table.getFills();
+//        System.out.println("Having " + fills.size() + " fills");
+//        for(XSSFCellFill fill : fills) {
+//        	System.out.println("Fill: " + fill.getFillBackgroundColor() + "/" + fill.getFillForegroundColor());
+//        }        
+        
+        XSSFWorkbook targetWorkbook = new XSSFWorkbook();
+        XSSFSheet newSheet = targetWorkbook.createSheet(sheet.getSheetName());
+        XSSFRow destRow = newSheet.createRow(0);
+        XSSFCell newCell = destRow.createCell(0);
+
+		//newCell.getCellStyle().cloneStyleFrom(cellStyle);
+        CellStyle newCellStyle = targetWorkbook.createCellStyle();
+        newCellStyle.cloneStyleFrom(cellStyle);
+        newCell.setCellStyle(newCellStyle);
+		checkStyle(newCell.getCellStyle());
+        newCell.setCellValue(oldCell.getStringCellValue());
+
+//        OutputStream os = new FileOutputStream("output.xlsm");
+//        try {
+//        	targetWorkbook.write(os);
+//        } finally {
+//        	os.close();
+//        }
+        
+        XSSFWorkbook wbBack = XSSFTestDataSamples.writeOutAndReadBack(targetWorkbook);
+        XSSFCellStyle styleBack = wbBack.getSheetAt(0).getRow(0).getCell(0).getCellStyle();
+        checkStyle(styleBack);
+    }
+
+	private void checkStyle(XSSFCellStyle cellStyle) {
+		assertNotNull(cellStyle);
+        assertEquals(0, cellStyle.getFillForegroundColor());
+        assertNotNull(cellStyle.getFillForegroundXSSFColor());
+        XSSFColor fgColor = cellStyle.getFillForegroundColorColor();
+		assertNotNull(fgColor);
+		assertEquals("FF00FFFF", fgColor.getARGBHex());
+
+        assertEquals(0, cellStyle.getFillBackgroundColor());
+        assertNotNull(cellStyle.getFillBackgroundXSSFColor());
+        XSSFColor bgColor = cellStyle.getFillBackgroundColorColor();
+		assertNotNull(bgColor);
+		assertEquals("FF00FFFF", fgColor.getARGBHex());
+	}
 }

Modified: poi/trunk/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFCellStyle.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFCellStyle.java?rev=1666736&r1=1666735&r2=1666736&view=diff
==============================================================================
--- poi/trunk/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFCellStyle.java (original)
+++ poi/trunk/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFCellStyle.java Sat Mar 14 19:32:28 2015
@@ -17,6 +17,8 @@
 
 package org.apache.poi.xssf.usermodel;
 
+import java.io.IOException;
+
 import junit.framework.TestCase;
 
 import org.apache.poi.hssf.usermodel.HSSFCellStyle;
@@ -82,6 +84,9 @@ public class TestXSSFCellStyle extends T
 		stylesTable.putCellStyleXf(cellStyleXf);
 		stylesTable.putCellXf(cellXf);
 		cellStyle = new XSSFCellStyle(1, 1, stylesTable, null);
+
+		assertNotNull(stylesTable.getFillAt(1).getCTFill().getPatternFill());
+		assertEquals(STPatternType.INT_DARK_GRAY, stylesTable.getFillAt(1).getCTFill().getPatternFill().getPatternType().intValue());
 	}
 
 	public void testGetSetBorderBottom() {
@@ -551,7 +556,7 @@ public class TestXSSFCellStyle extends T
         assertEquals(IndexedColors.AUTOMATIC.getIndex(), cellStyle.getFillBackgroundColor());
 	}
 
-	public void testDefaultStyles() {
+	public void testDefaultStyles() throws IOException {
 
 		XSSFWorkbook wb1 = new XSSFWorkbook();
 
@@ -577,6 +582,7 @@ public class TestXSSFCellStyle extends T
         assertEquals(style2.getBorderLeft(), style1.getBorderLeft());
         assertEquals(style2.getBorderRight(), style1.getBorderRight());
         assertEquals(style2.getBorderTop(), style1.getBorderTop());
+        wb2.close();
 	}
 
 
@@ -618,7 +624,7 @@ public class TestXSSFCellStyle extends T
 
 	public void testGetFillPattern() {
 
-        assertEquals(CellStyle.NO_FILL, cellStyle.getFillPattern());
+        assertEquals(STPatternType.INT_DARK_GRAY-1, cellStyle.getFillPattern());
 
         int num = stylesTable.getFills().size();
         cellStyle.setFillPattern(CellStyle.SOLID_FOREGROUND);

Added: poi/trunk/test-data/spreadsheet/56295.xlsx
URL: http://svn.apache.org/viewvc/poi/trunk/test-data/spreadsheet/56295.xlsx?rev=1666736&view=auto
==============================================================================
Binary files poi/trunk/test-data/spreadsheet/56295.xlsx (added) and poi/trunk/test-data/spreadsheet/56295.xlsx Sat Mar 14 19:32:28 2015 differ



---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@poi.apache.org
For additional commands, e-mail: commits-help@poi.apache.org