From: Nick Burch Date: Mon, 31 Mar 2008 22:30:56 +0000 (+0000) Subject: XSSF named range support X-Git-Tag: REL_3_5_BETA2~150 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=117e95f06340652c3152025f15ea105ee0cb7336;p=poi.git XSSF named range support git-svn-id: https://svn.apache.org/repos/asf/poi/branches/ooxml@643189 13f79535-47bb-0310-9956-ffa450edef68 --- diff --git a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFName.java b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFName.java new file mode 100644 index 0000000000..5392bfce8d --- /dev/null +++ b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFName.java @@ -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); + } +} diff --git a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFWorkbook.java b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFWorkbook.java index 59e4e26b99..05479d9b74 100644 --- a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFWorkbook.java +++ b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFWorkbook.java @@ -58,6 +58,8 @@ import org.openxml4j.opc.PackagingURIHelper; 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 @@ public class XSSFWorkbook extends POIXMLDocument implements Workbook { private CTWorkbook workbook; private List sheets = new LinkedList(); + private List namedRanges = new LinkedList(); private SharedStringSource sharedStringSource; private StylesSource stylesSource; @@ -247,6 +250,13 @@ public class XSSFWorkbook extends POIXMLDocument implements Workbook { } 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 @@ public class XSSFWorkbook extends POIXMLDocument implements Workbook { 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 @@ public class XSSFWorkbook extends POIXMLDocument implements Workbook { 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 0) { + CTDefinedNames names = CTDefinedNames.Factory.newInstance(); + CTDefinedName[] nr = new CTDefinedName[namedRanges.size()]; + for(int i=0; i