poi-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From u..@apache.org
Subject svn commit: r614199 - in /poi/branches/ooxml/src/ooxml: java/org/apache/poi/xssf/usermodel/ testcases/org/apache/poi/xssf/usermodel/
Date Tue, 22 Jan 2008 13:28:58 GMT
Author: ugo
Date: Tue Jan 22 05:28:48 2008
New Revision: 614199

URL: http://svn.apache.org/viewvc?rev=614199&view=rev
Log:
Tests for XSSFWorkbook.

Added:
    poi/branches/ooxml/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFWorkbook.java
  (with props)
Modified:
    poi/branches/ooxml/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFRow.java
    poi/branches/ooxml/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFSheet.java
    poi/branches/ooxml/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFWorkbook.java

Modified: poi/branches/ooxml/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFRow.java
URL: http://svn.apache.org/viewvc/poi/branches/ooxml/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFRow.java?rev=614199&r1=614198&r2=614199&view=diff
==============================================================================
--- poi/branches/ooxml/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFRow.java (original)
+++ poi/branches/ooxml/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFRow.java Tue Jan 22
05:28:48 2008
@@ -155,8 +155,7 @@
     }
 
     public int getRowNum() {
-        // TODO Auto-generated method stub
-        return 0;
+        return (int) (row.getR() - 1);
     }
 
     public boolean getZeroHeight() {
@@ -188,7 +187,7 @@
     }
 
     public void setRowNum(int rowNum) {
-        // TODO Auto-generated method stub
+        this.row.setR(rowNum + 1);
 
     }
 

Modified: poi/branches/ooxml/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFSheet.java
URL: http://svn.apache.org/viewvc/poi/branches/ooxml/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFSheet.java?rev=614199&r1=614198&r2=614199&view=diff
==============================================================================
--- poi/branches/ooxml/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFSheet.java (original)
+++ poi/branches/ooxml/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFSheet.java Tue Jan
22 05:28:48 2008
@@ -18,6 +18,8 @@
 package org.apache.poi.xssf.usermodel;
 
 import java.util.Iterator;
+import java.util.LinkedList;
+import java.util.List;
 
 import org.apache.poi.hssf.util.PaneInformation;
 import org.apache.poi.hssf.util.Region;
@@ -44,13 +46,17 @@
 public class XSSFSheet implements Sheet {
 
     private CTSheet sheet;
-    
     private CTWorksheet worksheet;
+    private List<Row> rows;
     
     public XSSFSheet(CTSheet sheet) {
         this.sheet = sheet;
         this.worksheet = CTWorksheet.Factory.newInstance();
         this.worksheet.addNewSheetData();
+        this.rows = new LinkedList<Row>();
+        for (CTRow row : worksheet.getSheetData().getRowArray()) {
+                this.rows.add(new XSSFRow(row));
+        }
         // XXX ???
         CTSheetViews views = this.worksheet.addNewSheetViews();
         CTSheetView view = views.addNewSheetView();
@@ -109,11 +115,33 @@
 
     }
 
+        protected XSSFRow addRow(int index, int rownum) {
+                CTRow row = this.worksheet.getSheetData().insertNewRow(index);
+                XSSFRow xrow = new XSSFRow(row);
+                xrow.setRowNum(rownum);
+//              xrow.setHeight(13.41);
+                return xrow;
+        }
+
     public Row createRow(int rownum) {
-        CTRow row = this.worksheet.getSheetData().insertNewRow(rownum);
-        row.setR(rownum + 1);
-        row.setHt(13.41); // XXX ???
-        return new XSSFRow(row);
+        int index = 0;
+        for (Row r : this.rows) {
+                if (r.getRowNum() == rownum) {
+                        // Replace r with new row
+                XSSFRow xrow = addRow(index, rownum);
+                        rows.set(index, xrow);
+                        return xrow;
+                }
+                if (r.getRowNum() > rownum) {
+                        XSSFRow xrow = addRow(index, rownum);
+                        rows.add(index, xrow);
+                        return xrow;
+                }
+                ++index;
+        }
+        XSSFRow xrow = addRow(index, rownum);
+        rows.add(xrow);
+        return xrow;
     }
 
     public void createSplitPane(int splitPos, int splitPos2, int leftmostColumn, int topRow,
int activePane) {
@@ -262,7 +290,12 @@
     }
 
     public Row getRow(int rownum) {
-        // TODO Auto-generated method stub
+        for (Iterator<Row> it = rowIterator() ; it.hasNext() ; ) {
+                Row row = it.next();
+                if (row.getRowNum() == rownum) {
+                        return row;
+                }
+        }
         return null;
     }
 
@@ -371,9 +404,8 @@
 
     }
 
-    public Iterator rowIterator() {
-        // TODO Auto-generated method stub
-        return null;
+    public Iterator<Row> rowIterator() {
+        return rows.iterator();
     }
 
     public void setAlternativeExpression(boolean b) {
@@ -546,4 +578,32 @@
 
     }
 
+    public void setTabSelected(boolean flag) {
+        CTSheetViews views = this.worksheet.getSheetViews();
+        for (CTSheetView view : views.getSheetViewArray()) {
+            view.setTabSelected(flag);
+        }
+    }
+    
+    public boolean isTabSelected() {
+        CTSheetView view = getDefaultSheetView();
+        return view != null && view.getTabSelected();
+    }
+
+    /**
+     * Return the default sheet view. This is the last one if the sheet's views, according
to sec. 3.3.1.83
+     * of the OOXML spec: "A single sheet view definition. When more than 1 sheet view is
defined in the file,
+     * it means that when opening the workbook, each sheet view corresponds to a separate
window within the 
+     * spreadsheet application, where each window is showing the particular sheet. containing
the same 
+     * workbookViewId value, the last sheetView definition is loaded, and the others are
discarded. 
+     * When multiple windows are viewing the same sheet, multiple sheetView elements (with
corresponding 
+     * workbookView entries) are saved."
+     */
+    private CTSheetView getDefaultSheetView() {
+        CTSheetViews views = this.worksheet.getSheetViews();
+        if (views == null || views.getSheetViewArray() == null || views.getSheetViewArray().length
<= 0) {
+            return null;
+        }
+        return views.getSheetViewArray(views.getSheetViewArray().length - 1);
+    }
 }

Modified: poi/branches/ooxml/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFWorkbook.java
URL: http://svn.apache.org/viewvc/poi/branches/ooxml/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFWorkbook.java?rev=614199&r1=614198&r2=614199&view=diff
==============================================================================
--- poi/branches/ooxml/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFWorkbook.java (original)
+++ poi/branches/ooxml/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFWorkbook.java Tue
Jan 22 05:28:48 2008
@@ -34,6 +34,7 @@
 import org.apache.poi.ss.usermodel.Palette;
 import org.apache.poi.ss.usermodel.Sheet;
 import org.apache.poi.ss.usermodel.Workbook;
+import org.apache.xmlbeans.XmlObject;
 import org.apache.xmlbeans.XmlOptions;
 import org.openxml4j.exceptions.InvalidFormatException;
 import org.openxml4j.opc.Package;
@@ -63,6 +64,10 @@
         this.workbook.addNewSheets();
     }
     
+    protected CTWorkbook getWorkbook() {
+        return this.workbook;
+    }
+    
     public int addPicture(byte[] pictureData, int format) {
         // TODO Auto-generated method stub
         return 0;
@@ -207,8 +212,14 @@
     }
 
     public short getSelectedTab() {
-        // TODO Auto-generated method stub
-        return 0;
+        short i = 0;
+        for (XSSFSheet sheet : this.sheets) {
+            if (sheet.isTabSelected()) {
+                return i;
+            }
+            ++i;
+        }
+        return -1;
     }
 
     public Sheet getSheet(String name) {
@@ -217,17 +228,21 @@
     }
 
     public Sheet getSheetAt(int index) {
-        return this.sheets.get(index - 1);
+        return this.sheets.get(index);
     }
 
     public int getSheetIndex(String name) {
-        // TODO Auto-generated method stub
-        return 0;
+        CTSheet[] sheets = this.workbook.getSheets().getSheetArray();  
+        for (int i = 0 ; i < sheets.length ; ++i) {
+            if (name.equals(sheets[i].getName())) {
+                return i;
+            }
+        }
+        return -1;
     }
 
     public int getSheetIndex(Sheet sheet) {
-        // TODO Auto-generated method stub
-        return 0;
+        return this.sheets.indexOf(sheet);
     }
 
     public String getSheetName(int sheet) {
@@ -285,9 +300,14 @@
 
     }
 
+    /**
+     * We only set one sheet as selected for compatibility with HSSF.
+     */
     public void setSelectedTab(short index) {
-        // TODO Auto-generated method stub
-
+        for (int i = 0 ; i < this.sheets.size() ; ++i) {
+            XSSFSheet sheet = this.sheets.get(i);
+            sheet.setTabSelected(i == index);
+        }
     }
 
     public void setSheetName(int sheet, String name) {
@@ -301,8 +321,13 @@
     }
 
     public void setSheetOrder(String sheetname, int pos) {
-        // TODO Auto-generated method stub
-
+        int idx = getSheetIndex(sheetname);
+        sheets.add(pos, sheets.remove(idx));
+        // Reorder CTSheets
+        XmlObject cts = this.workbook.getSheets().getSheetArray(idx).copy();
+        this.workbook.getSheets().removeSheet(idx);
+        CTSheet newcts = this.workbook.getSheets().insertNewSheet(pos);
+        newcts.set(cts);
     }
 
     public void unwriteProtectWorkbook() {

Added: poi/branches/ooxml/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFWorkbook.java
URL: http://svn.apache.org/viewvc/poi/branches/ooxml/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFWorkbook.java?rev=614199&view=auto
==============================================================================
--- poi/branches/ooxml/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFWorkbook.java
(added)
+++ poi/branches/ooxml/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFWorkbook.java
Tue Jan 22 05:28:48 2008
@@ -0,0 +1,73 @@
+/* ====================================================================
+   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.usermodel;
+
+import junit.framework.TestCase;
+
+import org.apache.poi.ss.usermodel.Sheet;
+import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTSheet;
+import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTWorkbook;
+
+
+public class TestXSSFWorkbook extends TestCase {
+    
+    public void testGetSheetIndex() {
+        XSSFWorkbook workbook = new XSSFWorkbook();
+        Sheet sheet1 = workbook.createSheet("sheet1");
+        Sheet sheet2 = workbook.createSheet("sheet2");
+        assertEquals(0, workbook.getSheetIndex(sheet1));
+        assertEquals(0, workbook.getSheetIndex("sheet1"));
+        assertEquals(1, workbook.getSheetIndex(sheet2));
+        assertEquals(1, workbook.getSheetIndex("sheet2"));
+        assertEquals(-1, workbook.getSheetIndex("noSheet"));
+    }
+    
+    public void testSetSheetOrder() throws Exception {
+        XSSFWorkbook workbook = new XSSFWorkbook();
+        Sheet sheet1 = workbook.createSheet("sheet1");
+        Sheet sheet2 = workbook.createSheet("sheet2");
+        assertSame(sheet1, workbook.getSheetAt(0));
+        assertSame(sheet2, workbook.getSheetAt(1));
+        workbook.setSheetOrder("sheet2", 0);
+        assertSame(sheet2, workbook.getSheetAt(0));
+        assertSame(sheet1, workbook.getSheetAt(1));
+        // Test reordering of CTSheets
+        CTWorkbook ctwb = workbook.getWorkbook();
+        CTSheet[] ctsheets = ctwb.getSheets().getSheetArray();
+        assertEquals("sheet2", ctsheets[0].getName());
+        assertEquals("sheet1", ctsheets[1].getName());
+        
+        // Borderline case: only one sheet
+        workbook = new XSSFWorkbook();
+        sheet1 = workbook.createSheet("sheet1");
+        assertSame(sheet1, workbook.getSheetAt(0));
+        workbook.setSheetOrder("sheet1", 0);
+        assertSame(sheet1, workbook.getSheetAt(0));
+    }
+    
+    public void testSetSelectedTab() throws Exception {
+        XSSFWorkbook workbook = new XSSFWorkbook();
+        Sheet sheet1 = workbook.createSheet("sheet1");
+        Sheet sheet2 = workbook.createSheet("sheet2");
+        assertEquals(-1, workbook.getSelectedTab());
+        workbook.setSelectedTab((short) 0);
+        assertEquals(0, workbook.getSelectedTab());
+        workbook.setSelectedTab((short) 1);
+        assertEquals(1, workbook.getSelectedTab());
+    }
+}

Propchange: poi/branches/ooxml/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFWorkbook.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: poi/branches/ooxml/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFWorkbook.java
------------------------------------------------------------------------------
    svn:keywords = Date Revision Author HeadURL Id

Propchange: poi/branches/ooxml/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFWorkbook.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain



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


Mime
View raw message