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 2013/08/20 20:44:44 UTC

svn commit: r1515916 - in /poi/trunk/src/ooxml: java/org/apache/poi/xssf/usermodel/XSSFSheet.java testcases/org/apache/poi/xssf/usermodel/TestXSSFWorkbook.java

Author: centic
Date: Tue Aug 20 18:44:44 2013
New Revision: 1515916

URL: http://svn.apache.org/r1515916
Log:
Bug 52233: try to fix this without breaking the format of xlsx-files.
The set to null is necessary to not have an empty <cols/> element in the
xlsx, however later on stuff breaks if no colsArray is availalbe,
therefore we now re-create the empty cols array if we did remove it
before.

Modified:
    poi/trunk/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFSheet.java
    poi/trunk/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFWorkbook.java

Modified: poi/trunk/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFSheet.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFSheet.java?rev=1515916&r1=1515915&r2=1515916&view=diff
==============================================================================
--- poi/trunk/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFSheet.java (original)
+++ poi/trunk/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFSheet.java Tue Aug 20 18:44:44 2013
@@ -1554,7 +1554,8 @@ public class XSSFSheet extends POIXMLDoc
      * be the third row if say for instance the second row is undefined.
      * Call getRowNum() on each row if you care which one it is.
      */
-    public Iterator<Row> rowIterator() {
+    @SuppressWarnings("unchecked")
+	public Iterator<Row> rowIterator() {
         return (Iterator<Row>)(Iterator<? extends Row>) _rows.values().iterator();
     }
 
@@ -2689,9 +2690,11 @@ public class XSSFSheet extends POIXMLDoc
     }
 
     protected void write(OutputStream out) throws IOException {
+    	boolean setToNull = false;
         if(worksheet.sizeOfColsArray() == 1) {
         	CTCols col = worksheet.getColsArray(0);
             if(col.sizeOfColArray() == 0) {
+            	setToNull = true;
             	// this is necessary so that we do not write an empty <cols/> item into the sheet-xml in the xlsx-file
             	// Excel complains about a corrupted file if this shows up there!
                 worksheet.setColsArray(null);
@@ -2728,6 +2731,11 @@ public class XSSFSheet extends POIXMLDoc
         xmlOptions.setSaveSuggestedPrefixes(map);
 
         worksheet.save(out, xmlOptions);
+
+        // Bug 52233: Ensure that we have a col-array even if write() removed it
+    	if(setToNull) {
+    		worksheet.addNewCols();
+    	}
     }
 
     /**

Modified: poi/trunk/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFWorkbook.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFWorkbook.java?rev=1515916&r1=1515915&r2=1515916&view=diff
==============================================================================
--- poi/trunk/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFWorkbook.java (original)
+++ poi/trunk/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFWorkbook.java Tue Aug 20 18:44:44 2013
@@ -462,15 +462,20 @@ public final class TestXSSFWorkbook exte
                 sh.getCTWorksheet().getSheetPr().getTabColor().getIndexed());
     }
 
-    // TODO: disabled as the fix for this had severe side-effects
-	public void doNotRuntestColumnWidthPOI52233() throws Exception {
+	public void testColumnWidthPOI52233() throws Exception {
 		XSSFWorkbook workbook = new XSSFWorkbook();
 		XSSFSheet sheet = workbook.createSheet();
 		XSSFRow row = sheet.createRow(0);
 		XSSFCell cell = row.createCell(0);
 		cell.setCellValue("hello world");
-		assertEquals("hello world", workbook.getSheetAt(0).getRow(0).getCell(0).getStringCellValue());
-		assertEquals(2048, workbook.getSheetAt(0).getColumnWidth(0)); // <-works
+
+		sheet = workbook.createSheet();
+        sheet.setColumnWidth(4, 5000);
+        sheet.setColumnWidth(5, 5000);
+       
+        sheet.groupColumn((short) 4, (short) 5);
+
+        accessWorkbook(workbook);
 
 		ByteArrayOutputStream stream = new ByteArrayOutputStream();
 		try {
@@ -479,7 +484,14 @@ public final class TestXSSFWorkbook exte
 			stream.close();
 		}
 
+		accessWorkbook(workbook);
+	}
+
+	private void accessWorkbook(XSSFWorkbook workbook) {
+		workbook.getSheetAt(1).setColumnGroupCollapsed(4, true);
+		workbook.getSheetAt(1).setColumnGroupCollapsed(4, false);
+
 		assertEquals("hello world", workbook.getSheetAt(0).getRow(0).getCell(0).getStringCellValue());
-		assertEquals(2048, workbook.getSheetAt(0).getColumnWidth(0)); // <- did throw IndexOutOfBoundsException before fixing the bug
+		assertEquals(2048, workbook.getSheetAt(0).getColumnWidth(0)); // <-works
 	}
 }



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