]> source.dussan.org Git - poi.git/commitdiff
Bug 55369: Add support for collapsing rows in SXSSF
authorDominik Stadler <centic@apache.org>
Sun, 20 Oct 2013 18:04:08 +0000 (18:04 +0000)
committerDominik Stadler <centic@apache.org>
Sun, 20 Oct 2013 18:04:08 +0000 (18:04 +0000)
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1533932 13f79535-47bb-0310-9956-ffa450edef68

src/examples/src/org/apache/poi/xssf/streaming/examples/Outlining.java [new file with mode: 0644]
src/ooxml/java/org/apache/poi/xssf/streaming/SXSSFRow.java
src/ooxml/java/org/apache/poi/xssf/streaming/SXSSFSheet.java
src/ooxml/java/org/apache/poi/xssf/streaming/SheetDataWriter.java
src/ooxml/testcases/org/apache/poi/xssf/streaming/TestOutlining.java [new file with mode: 0644]

diff --git a/src/examples/src/org/apache/poi/xssf/streaming/examples/Outlining.java b/src/examples/src/org/apache/poi/xssf/streaming/examples/Outlining.java
new file mode 100644 (file)
index 0000000..03af09d
--- /dev/null
@@ -0,0 +1,51 @@
+/* ====================================================================
+   Licensed to the Apache Software Foundation (ASF) under one or more
+   contributor license agreements.  See the NOTICE file distributed with
+   this work for additional information regarding copyright ownership.
+   The ASF licenses this file to You under the Apache License, Version 2.0
+   (the "License"); you may not use this file except in compliance with
+   the License.  You may obtain a copy of the License at
+
+       http://www.apache.org/licenses/LICENSE-2.0
+
+   Unless required by applicable law or agreed to in writing, software
+   distributed under the License is distributed on an "AS IS" BASIS,
+   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+   See the License for the specific language governing permissions and
+   limitations under the License.
+==================================================================== */
+
+package org.apache.poi.xssf.streaming.examples;
+
+import java.io.FileOutputStream;
+
+import org.apache.poi.xssf.streaming.SXSSFSheet;
+import org.apache.poi.xssf.streaming.SXSSFWorkbook;
+
+public class Outlining {
+
+       public static void main(String[] args) throws Exception {
+               Outlining o = new Outlining();
+               o.collapseRow();
+       }
+
+       private void collapseRow() throws Exception {
+               SXSSFWorkbook wb2 = new SXSSFWorkbook(100);
+               SXSSFSheet sheet2 = (SXSSFSheet) wb2.createSheet("new sheet");
+
+               int rowCount = 20;
+               for (int i = 0; i < rowCount; i++) {
+                       sheet2.createRow(i);
+               }
+
+               sheet2.groupRow(4, 9);
+               sheet2.groupRow(11, 19);
+
+               sheet2.setRowGroupCollapsed(4, true);
+
+               FileOutputStream fileOut = new FileOutputStream("outlining_collapsed.xlsx");
+               wb2.write(fileOut);
+               fileOut.close();
+               wb2.dispose();
+       }
+}
index a762de822b7862d7fdf2fdbac8c6e654a71b2d27..9ace1475a48b75c3217cf6bac11d0622f810a9dc 100644 (file)
@@ -40,7 +40,10 @@ public class SXSSFRow implements Row
     short _height=-1;
     boolean _zHeight = false;
     int _outlineLevel = 0;   // Outlining level of the row, when outlining is on
-
+    // use Boolean to have a tri-state for on/off/undefined 
+    Boolean _hidden;
+    Boolean _collapsed;
+       
     public SXSSFRow(SXSSFSheet sheet, int initialSize)
     {
         _sheet=sheet;
@@ -61,7 +64,22 @@ public class SXSSFRow implements Row
     void setOutlineLevel(int level){
         _outlineLevel = level;
     }
+    
+    public Boolean getHidden() {
+        return _hidden;
+    }
+
+    public void setHidden(Boolean hidden) {
+        this._hidden = hidden;
+    }
+
+    public Boolean getCollapsed() {
+        return _collapsed;
+    }
 
+    public void setCollapsed(Boolean collapsed) {
+        this._collapsed = collapsed;
+    }
 //begin of interface implementation
     public Iterator<Cell> iterator()
     {
index b92adddc745d199e994de6a91e9588242e7b7413..080d31fc3d4352bf9519a8eefb3eef255bfe8bf2 100644 (file)
@@ -1143,8 +1143,65 @@ public class SXSSFSheet implements Sheet, Cloneable
      */
     public void setRowGroupCollapsed(int row, boolean collapse)
     {
-        //_sh.setRowGroupCollapsed(row, collapse);
-        throw new RuntimeException("Not Implemented");
+        if (collapse) {
+            collapseRow(row);
+        } else {
+            //expandRow(rowIndex);
+            throw new RuntimeException("Not Implemented");
+        }
+    }
+    
+    /**
+     * @param rowIndex the zero based row index to collapse
+     */
+    private void collapseRow(int rowIndex) {
+        SXSSFRow row = (SXSSFRow) getRow(rowIndex);
+        if(row == null) {
+                       throw new IllegalArgumentException("Invalid row number("+ rowIndex + "). Row does not exist.");
+               } else {
+            int startRow = findStartOfRowOutlineGroup(rowIndex);
+
+            // Hide all the columns until the end of the group
+            int lastRow = writeHidden(row, startRow, true);
+            SXSSFRow lastRowObj = (SXSSFRow) getRow(lastRow); 
+            if (lastRowObj != null) {
+                lastRowObj.setCollapsed(true);
+            } else {
+                SXSSFRow newRow = (SXSSFRow) createRow(lastRow);
+                newRow.setCollapsed(true);
+            }
+        }
+    }
+    
+    /**
+     * @param rowIndex the zero based row index to find from
+     */
+    private int findStartOfRowOutlineGroup(int rowIndex) {
+        // Find the start of the group.
+               Row row = getRow(rowIndex);
+        int level = ((SXSSFRow) row).getOutlineLevel();
+        if(level == 0) {
+               throw new IllegalArgumentException("Outline level is zero for the row (" + rowIndex + ").");
+        }
+        int currentRow = rowIndex;
+        while (getRow(currentRow) != null) {
+            if (((SXSSFRow) getRow(currentRow)).getOutlineLevel() < level)
+                return currentRow + 1;
+            currentRow--;
+        }
+        return currentRow + 1;
+    }
+    
+    private int writeHidden(SXSSFRow xRow, int rowIndex, boolean hidden) {
+        int level = xRow.getOutlineLevel();
+        SXSSFRow currRow = (SXSSFRow) getRow(rowIndex);
+
+        while (currRow != null && currRow.getOutlineLevel() >= level) {
+            currRow.setHidden(hidden);
+            rowIndex++;
+            currRow = (SXSSFRow) getRow(rowIndex);
+        }
+        return rowIndex;
     }
 
     /**
index e205224758c48d5048fcf841b9d88b87c20d487c..1036c2280153dfb566f575fa8be53ec29f5a03e6 100644 (file)
 \r
 package org.apache.poi.xssf.streaming;\r
 \r
+import java.io.BufferedWriter;\r
+import java.io.File;\r
+import java.io.FileInputStream;\r
+import java.io.FileWriter;\r
+import java.io.IOException;\r
+import java.io.InputStream;\r
+import java.io.Writer;\r
+import java.util.Iterator;\r
+\r
 import org.apache.poi.ss.usermodel.Cell;\r
 import org.apache.poi.ss.usermodel.CellStyle;\r
 import org.apache.poi.ss.usermodel.FormulaError;\r
 import org.apache.poi.ss.util.CellReference;\r
-import org.apache.xmlbeans.XmlCursor;\r
-import org.openxmlformats.schemas.spreadsheetml.x2006.main.STXstring;\r
-\r
-import javax.xml.namespace.QName;\r
-import java.io.*;\r
-import java.util.Iterator;\r
 \r
 /**\r
  * Initially copied from BigGridDemo "SpreadsheetWriter".\r
@@ -110,6 +113,7 @@ public class SheetDataWriter {
         return _numberLastFlushedRow;\r
     }\r
 \r
+    @Override\r
     protected void finalize() throws Throwable {\r
         _fd.delete();\r
     }\r
@@ -148,6 +152,13 @@ public class SheetDataWriter {
         if (row.getOutlineLevel() != 0) {\r
             _out.write(" outlineLevel=\"" + row.getOutlineLevel() + "\"");\r
         }\r
+        if(row.getHidden() != null) {\r
+            _out.write(" hidden=\"" + (row.getHidden() ? "1" : "0") + "\"");\r
+        }\r
+        if(row.getCollapsed() != null) {\r
+               _out.write(" collapsed=\"" + (row.getCollapsed() ? "1" : "0") + "\"");\r
+        }\r
+        \r
         _out.write(">\n");\r
         this._rownum = rownum;\r
         _rowContainedNullCells = false;\r
diff --git a/src/ooxml/testcases/org/apache/poi/xssf/streaming/TestOutlining.java b/src/ooxml/testcases/org/apache/poi/xssf/streaming/TestOutlining.java
new file mode 100644 (file)
index 0000000..e616402
--- /dev/null
@@ -0,0 +1,102 @@
+/*
+ *  ====================================================================
+ *    Licensed to the Apache Software Foundation (ASF) under one or more
+ *    contributor license agreements.  See the NOTICE file distributed with
+ *    this work for additional information regarding copyright ownership.
+ *    The ASF licenses this file to You under the Apache License, Version 2.0
+ *    (the "License"); you may not use this file except in compliance with
+ *    the License.  You may obtain a copy of the License at
+ *
+ *        http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *    Unless required by applicable law or agreed to in writing, software
+ *    distributed under the License is distributed on an "AS IS" BASIS,
+ *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *    See the License for the specific language governing permissions and
+ *    limitations under the License.
+ * ====================================================================
+ */
+
+package org.apache.poi.xssf.streaming;
+
+import junit.framework.TestCase;
+
+public final class TestOutlining extends TestCase {
+       public void testSetRowGroupCollapsed() throws Exception {
+
+               SXSSFWorkbook wb2 = new SXSSFWorkbook(100);
+               wb2.setCompressTempFiles(true);
+               SXSSFSheet sheet2 = (SXSSFSheet) wb2.createSheet("new sheet");
+
+               int rowCount = 20;
+               for (int i = 0; i < rowCount; i++) {
+                       sheet2.createRow(i);
+               }
+
+               sheet2.groupRow(4, 9);
+               sheet2.groupRow(11, 19);
+
+               sheet2.setRowGroupCollapsed(4, true);
+
+               SXSSFRow r = (SXSSFRow) sheet2.getRow(8);
+               assertTrue(r.getHidden());
+               r = (SXSSFRow) sheet2.getRow(10);
+               assertTrue(r.getCollapsed());
+               r = (SXSSFRow) sheet2.getRow(12);
+               assertNull(r.getHidden());
+               wb2.dispose();
+       }
+
+       public void testSetRowGroupCollapsedError() throws Exception {
+
+               SXSSFWorkbook wb2 = new SXSSFWorkbook(100);
+               wb2.setCompressTempFiles(true);
+               SXSSFSheet sheet2 = (SXSSFSheet) wb2.createSheet("new sheet");
+
+               int rowCount = 20;
+               for (int i = 0; i < rowCount; i++) {
+                       sheet2.createRow(i);
+               }
+
+               sheet2.groupRow(4, 9);
+               sheet2.groupRow(11, 19);
+
+               try {
+                       sheet2.setRowGroupCollapsed(3, true);
+                       fail("Should fail with an exception");
+               } catch (IllegalArgumentException e) {
+                       assertTrue(e.getMessage().contains("row (3)"));
+               }
+
+               try {
+                       sheet2.setRowGroupCollapsed(10, true);
+                       fail("Should fail with an exception");
+               } catch (IllegalArgumentException e) {
+                       assertTrue(e.getMessage().contains("row (10)"));
+               }
+
+               try {
+                       sheet2.setRowGroupCollapsed(0, true);
+                       fail("Should fail with an exception");
+               } catch (IllegalArgumentException e) {
+                       assertTrue(e.getMessage().contains("row (0)"));
+               }
+
+               try {
+                       sheet2.setRowGroupCollapsed(20, true);
+                       fail("Should fail with an exception");
+               } catch (IllegalArgumentException e) {
+                       assertTrue("Had: " + e.getMessage(), 
+                                       e.getMessage().contains("Row does not exist"));
+               }
+
+               SXSSFRow r = (SXSSFRow) sheet2.getRow(8);
+               assertNotNull(r);
+               assertNull(r.getHidden());
+               r = (SXSSFRow) sheet2.getRow(10);
+               assertNull(r.getCollapsed());
+               r = (SXSSFRow) sheet2.getRow(12);
+               assertNull(r.getHidden());
+               wb2.dispose();
+       }
+}