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 2016/10/29 07:38:46 UTC

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

Author: centic
Date: Sat Oct 29 07:38:46 2016
New Revision: 1767096

URL: http://svn.apache.org/viewvc?rev=1767096&view=rev
Log:
Bug 53611: populate dimension of XSSF Worksheet when writing the document

Modified:
    poi/trunk/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFSheet.java
    poi/trunk/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFBugs.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=1767096&r1=1767095&r2=1767096&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 Sat Oct 29 07:38:46 2016
@@ -3451,8 +3451,28 @@ public class XSSFSheet extends POIXMLDoc
             worksheet.getHyperlinks().setHyperlinkArray(ctHls);
         }
 
+        int minCell=Integer.MAX_VALUE, maxCell=Integer.MIN_VALUE;
         for(XSSFRow row : _rows.values()){
+            // first perform the normal write actions for the row
             row.onDocumentWrite();
+
+            // then calculate min/max cell-numbers for the worksheet-dimension
+            if(row.getFirstCellNum() != -1) {
+                minCell = Math.min(minCell, row.getFirstCellNum());
+            }
+            if(row.getLastCellNum() != -1) {
+                maxCell = Math.max(maxCell, row.getLastCellNum());
+            }
+        }
+
+        // finally, if we had at least one cell we can populate the optional dimension-field
+        if(minCell != Integer.MAX_VALUE) {
+            String ref = new CellRangeAddress(getFirstRowNum(), getLastRowNum(), minCell, maxCell).formatAsString();
+            if(worksheet.isSetDimension()) {
+                worksheet.getDimension().setRef(ref);
+            } else {
+                worksheet.addNewDimension().setRef(ref);
+            }
         }
 
         XmlOptions xmlOptions = new XmlOptions(DEFAULT_XML_OPTIONS);

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=1767096&r1=1767095&r2=1767096&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 Oct 29 07:38:46 2016
@@ -25,13 +25,7 @@ import static org.junit.Assert.assertNul
 import static org.junit.Assert.assertTrue;
 import static org.junit.Assert.fail;
 
-import java.io.ByteArrayInputStream;
-import java.io.ByteArrayOutputStream;
-import java.io.File;
-import java.io.FileInputStream;
-import java.io.FileOutputStream;
-import java.io.IOException;
-import java.io.InputStream;
+import java.io.*;
 import java.util.Arrays;
 import java.util.Calendar;
 import java.util.HashMap;
@@ -3169,4 +3163,31 @@ public final class TestXSSFBugs extends
         sheet = wb.getSheetAt(4);
         assertNotNull(sheet.getDrawingPatriarch());
     }
+
+    @Test
+    public void test53611() throws IOException {
+        Workbook wb = new XSSFWorkbook();
+        Sheet sheet = wb.createSheet("test");
+        Row row = sheet.createRow(1);
+        Cell cell = row.createCell(1);
+        cell.setCellValue("blabla");
+
+        row = sheet.createRow(4);
+        cell = row.createCell(7);
+        cell.setCellValue("blabla");
+
+        // we currently only populate the dimension during writing out
+        // to avoid having to iterate all rows/cells in each add/remove of a row or cell
+        //OutputStream str = new FileOutputStream("/tmp/53611.xlsx");
+        OutputStream str = new ByteArrayOutputStream();
+        try {
+            wb.write(str);
+        } finally {
+            str.close();
+        }
+
+        assertEquals("B2:I5", ((XSSFSheet)sheet).getCTWorksheet().getDimension().getRef());
+
+        wb.close();
+    }
 }



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