poi-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From n...@apache.org
Subject svn commit: r643189 - in /poi/branches/ooxml/src: ooxml/java/org/apache/poi/xssf/usermodel/ ooxml/testcases/org/apache/poi/xssf/usermodel/ testcases/org/apache/poi/hssf/data/
Date Mon, 31 Mar 2008 22:31:01 GMT
Author: nick
Date: Mon Mar 31 15:30:56 2008
New Revision: 643189

URL: http://svn.apache.org/viewvc?rev=643189&view=rev
Log:
XSSF named range support

Added:
    poi/branches/ooxml/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFName.java   (with
props)
    poi/branches/ooxml/src/testcases/org/apache/poi/hssf/data/WithVariousData.xlsx   (with
props)
Modified:
    poi/branches/ooxml/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFWorkbook.java
    poi/branches/ooxml/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFWorkbook.java

Added: poi/branches/ooxml/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFName.java
URL: http://svn.apache.org/viewvc/poi/branches/ooxml/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFName.java?rev=643189&view=auto
==============================================================================
--- poi/branches/ooxml/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFName.java (added)
+++ poi/branches/ooxml/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFName.java Mon Mar
31 15:30:56 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 org.apache.poi.ss.usermodel.Name;
+import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTDefinedName;
+
+/**
+ * XSSF Implementation of a Named Range
+ */
+public class XSSFName implements Name {
+	private XSSFWorkbook workbook;
+	private CTDefinedName ctName;
+	
+	protected XSSFName(XSSFWorkbook workbook) {
+		this.workbook = workbook;
+		this.ctName = CTDefinedName.Factory.newInstance();
+	}
+	protected XSSFName(CTDefinedName name, XSSFWorkbook workbook) {
+		this.workbook = workbook;
+		this.ctName = name;
+	}
+
+	/**
+	 * Returns the underlying named range object
+	 */
+	protected CTDefinedName getCTName() {
+		return ctName;
+	}
+	
+	public String getNameName() {
+		return ctName.getName();
+	}
+	public void setNameName(String nameName) {
+		ctName.setName(nameName);
+	}
+
+	public String getReference() {
+		return ctName.getStringValue();
+	}
+	public void setReference(String ref) {
+		ctName.setStringValue(ref);
+	}
+	
+	public String getSheetName() {
+		long sheetId = ctName.getLocalSheetId();
+		if(sheetId >= 0) {
+			return workbook.getSheetName((int)sheetId);
+		}
+		return null;
+	}
+
+	public String getComment() {
+		return ctName.getComment();
+	}
+	public void setComment(String comment) {
+		ctName.setComment(comment);
+	}
+}

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

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=643189&r1=643188&r2=643189&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 Mon
Mar 31 15:30:56 2008
@@ -58,6 +58,8 @@
 import org.openxml4j.opc.TargetMode;
 import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTBookView;
 import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTBookViews;
+import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTDefinedName;
+import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTDefinedNames;
 import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTDialogsheet;
 import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTSheet;
 import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTWorkbook;
@@ -188,6 +190,7 @@
     private CTWorkbook workbook;
     
     private List<XSSFSheet> sheets = new LinkedList<XSSFSheet>();
+    private List<XSSFName> namedRanges = new LinkedList<XSSFName>();
     
     private SharedStringSource sharedStringSource;
     private StylesSource stylesSource;
@@ -247,6 +250,13 @@
         } catch (InvalidFormatException e) {
             throw new IOException(e.toString());
         }
+        
+        // Process the named ranges
+        if(workbook.getDefinedNames() != null) {
+        	for(CTDefinedName ctName : workbook.getDefinedNames().getDefinedNameArray()) {
+        		namedRanges.add(new XSSFName(ctName, this));
+        	}
+        }
     }
 
     protected CTWorkbook getWorkbook() {
@@ -324,9 +334,10 @@
         return null;
     }
 
-    public Name createName() {
-        // TODO Auto-generated method stub
-        return null;
+    public XSSFName createName() {
+    	XSSFName name = new XSSFName(this);
+    	namedRanges.add(name);
+    	return name;
     }
 
     public Sheet createSheet() {
@@ -430,19 +441,19 @@
         return null;
     }
 
-    public Name getNameAt(int index) {
-        // TODO Auto-generated method stub
-        return null;
-    }
-
-    public int getNameIndex(String name) {
-        // TODO Auto-generated method stub
-        return 0;
+    public XSSFName getNameAt(int index) {
+    	return namedRanges.get(index);
     }
-
     public String getNameName(int index) {
-        // TODO Auto-generated method stub
-        return null;
+        return getNameAt(index).getNameName();
+    }
+    public int getNameIndex(String name) {
+    	for(int i=0; i<namedRanges.size(); i++) {
+    		if(namedRanges.get(i).getNameName().equals(name)) {
+    			return i;
+    		}
+    	}
+    	return -1;
     }
 
     public short getNumCellStyles() {
@@ -456,8 +467,7 @@
     }
 
     public int getNumberOfNames() {
-        // TODO Auto-generated method stub
-        return 0;
+        return namedRanges.size();
     }
 
     public int getNumberOfSheets() {
@@ -656,6 +666,21 @@
             if(stylesSource != null) {
 	             StylesTable st = (StylesTable)stylesSource;
 	             STYLES.save(st, corePart);
+            }
+            
+            // Named ranges
+            if(namedRanges.size() > 0) {
+            	CTDefinedNames names = CTDefinedNames.Factory.newInstance();
+            	CTDefinedName[] nr = new CTDefinedName[namedRanges.size()];
+            	for(int i=0; i<namedRanges.size(); i++) {
+            		nr[i] = namedRanges.get(i).getCTName();
+            	}
+            	names.setDefinedNameArray(nr);
+            	workbook.setDefinedNames(names);
+            } else {
+            	if(workbook.isSetDefinedNames()) {
+            		workbook.setDefinedNames(null);
+            	}
             }
 
             // Now we can write out the main Workbook, with

Modified: 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=643189&r1=643188&r2=643189&view=diff
==============================================================================
--- poi/branches/ooxml/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFWorkbook.java
(original)
+++ poi/branches/ooxml/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFWorkbook.java
Mon Mar 31 15:30:56 2008
@@ -17,15 +17,19 @@
 
 package org.apache.poi.xssf.usermodel;
 
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
 import java.io.File;
 import java.io.FileOutputStream;
 import java.io.OutputStream;
 
 import junit.framework.TestCase;
 
+import org.apache.poi.ss.usermodel.Name;
 import org.apache.poi.ss.usermodel.RichTextString;
 import org.apache.poi.ss.usermodel.Sheet;
 import org.apache.poi.ss.usermodel.StylesSource;
+import org.apache.poi.ss.usermodel.Workbook;
 import org.apache.poi.xssf.model.StylesTable;
 import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTSheet;
 import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTWorkbook;
@@ -299,7 +303,91 @@
 				st.putNumberFormat("testFORMAT2"));
 		assertEquals(10, st._getNumberFormatSize());
 		
+		
 		// Save, load back in again, and check
-		// TODO
+        ByteArrayOutputStream baos = new ByteArrayOutputStream();
+        workbook.write(baos);
+        ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
+        workbook = new XSSFWorkbook(Package.open(bais));
+        
+        ss = workbook.getStylesSource();
+		assertNotNull(ss);
+		assertTrue(ss instanceof StylesTable);
+		st = (StylesTable)ss;
+		
+		assertEquals(10, st._getNumberFormatSize());
+		assertEquals(2, st._getFontsSize());
+		assertEquals(2, st._getFillsSize());
+		assertEquals(1, st._getBordersSize());
+    }
+    
+    public void testNamedRanges() throws Exception {
+    	// First up, a new file
+        XSSFWorkbook workbook = new XSSFWorkbook();
+        assertEquals(0, workbook.getNumberOfNames());
+        
+        Name nameA = workbook.createName();
+        nameA.setReference("A2");
+        nameA.setNameName("ForA2");
+        
+        XSSFName nameB = workbook.createName();
+        nameB.setReference("B3");
+        nameB.setNameName("ForB3");
+        nameB.setComment("B3 Comment");
+        
+        // Save and re-load
+        ByteArrayOutputStream baos = new ByteArrayOutputStream();
+        workbook.write(baos);
+        ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
+        workbook = new XSSFWorkbook(Package.open(bais));
+        
+        assertEquals(2, workbook.getNumberOfNames());
+        assertEquals("A2", workbook.getNameAt(0).getReference());
+        assertEquals("ForA2", workbook.getNameAt(0).getNameName());
+        assertNull(workbook.getNameAt(0).getComment());
+        
+        assertEquals("B3", workbook.getNameAt(1).getReference());
+        assertEquals("ForB3", workbook.getNameAt(1).getNameName());
+        assertEquals("B3 Comment", workbook.getNameAt(1).getComment());
+        
+        assertEquals("ForA2", workbook.getNameName(0));
+        assertEquals(1, workbook.getNameIndex("ForB3"));
+        assertEquals(-1, workbook.getNameIndex("ForB3!!"));
+    	
+        
+        // Now, an existing file with named ranges
+		File xml = new File(
+				System.getProperty("HSSF.testdata.path") +
+				File.separator + "WithVariousData.xlsx"
+		);
+		assertTrue(xml.exists());
+    	
+		workbook = new XSSFWorkbook(xml.toString());
+		
+        assertEquals(2, workbook.getNumberOfNames());
+        assertEquals("Sheet1!$A$2:$A$7", workbook.getNameAt(0).getReference());
+        assertEquals("AllANumbers", workbook.getNameAt(0).getNameName());
+        assertEquals("All the numbers in A", workbook.getNameAt(0).getComment());
+        
+        assertEquals("Sheet1!$B$2:$B$7", workbook.getNameAt(1).getReference());
+        assertEquals("AllBStrings", workbook.getNameAt(1).getNameName());
+        assertEquals("All the strings in B", workbook.getNameAt(1).getComment());
+        
+        // Tweak, save, and re-check
+        workbook.getNameAt(1).setNameName("BStringsFun");
+        
+        baos = new ByteArrayOutputStream();
+        workbook.write(baos);
+        bais = new ByteArrayInputStream(baos.toByteArray());
+        workbook = new XSSFWorkbook(Package.open(bais));
+		
+        assertEquals(2, workbook.getNumberOfNames());
+        assertEquals("Sheet1!$A$2:$A$7", workbook.getNameAt(0).getReference());
+        assertEquals("AllANumbers", workbook.getNameAt(0).getNameName());
+        assertEquals("All the numbers in A", workbook.getNameAt(0).getComment());
+        
+        assertEquals("Sheet1!$B$2:$B$7", workbook.getNameAt(1).getReference());
+        assertEquals("BStringsFun", workbook.getNameAt(1).getNameName());
+        assertEquals("All the strings in B", workbook.getNameAt(1).getComment());
     }
 }

Added: poi/branches/ooxml/src/testcases/org/apache/poi/hssf/data/WithVariousData.xlsx
URL: http://svn.apache.org/viewvc/poi/branches/ooxml/src/testcases/org/apache/poi/hssf/data/WithVariousData.xlsx?rev=643189&view=auto
==============================================================================
Binary file - no diff available.

Propchange: poi/branches/ooxml/src/testcases/org/apache/poi/hssf/data/WithVariousData.xlsx
------------------------------------------------------------------------------
    svn:mime-type = application/octet-stream



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


Mime
View raw message