]> source.dussan.org Git - poi.git/commitdiff
added Workbook.getName(String) method. Fixed javadoc on related methods
authorJosh Micich <josh@apache.org>
Thu, 16 Apr 2009 20:34:03 +0000 (20:34 +0000)
committerJosh Micich <josh@apache.org>
Thu, 16 Apr 2009 20:34:03 +0000 (20:34 +0000)
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@765751 13f79535-47bb-0310-9956-ffa450edef68

src/java/org/apache/poi/hssf/usermodel/HSSFWorkbook.java
src/java/org/apache/poi/ss/usermodel/Workbook.java
src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFWorkbook.java

index f3e0a10f2bec4b6565fad25d6bc41243bb6f130d..7a78d4a08b1aa6e42a4a6ec69f8b532ba0b151d2 100644 (file)
@@ -1292,23 +1292,31 @@ public class HSSFWorkbook extends POIDocument implements org.apache.poi.ss.userm
         return workbook;
     }
 
-    /** gets the total number of named ranges in the workboko
-     * @return number of named ranges
-     */
     public int getNumberOfNames(){
         int result = names.size();
         return result;
     }
+    
+    public HSSFName getName(String name) {
+        int nameIndex = getNameIndex(name);
+        if (nameIndex < 0) {
+            return null;
+        }
+        return (HSSFName) names.get(nameIndex);
+    }
 
-    /** gets the Named range
-     * @param index position of the named range
-     * @return named range high level
-     */
-    public HSSFName getNameAt(int index){
-        HSSFName result = (HSSFName) names.get(index);
-
-        return result;
+    public HSSFName getNameAt(int nameIndex) {
+        int nNames = names.size();
+        if (nNames < 1) {
+            throw new IllegalStateException("There are no defined names in this workbook");
+        }
+        if (nameIndex < 0 || nameIndex > nNames) {
+            throw new IllegalArgumentException("Specified name index " + nameIndex 
+                    + " is outside the allowable range (0.." + (nNames-1) + ").");
+        }
+        return (HSSFName) names.get(nameIndex);
     }
+
     public NameRecord getNameRecord(int nameIndex) {
         return getWorkbook().getNameRecord(nameIndex);
     }
@@ -1411,34 +1419,19 @@ public class HSSFWorkbook extends POIDocument implements org.apache.poi.ss.userm
         return newName;
     }
 
-    /** gets the named range index by his name
-     * <i>Note:</i>Excel named ranges are case-insensitive and
-     * this method performs a case-insensitive search.
-     *
-     * @param name named range name
-     * @return named range index
-     */
-    public int getNameIndex(String name)
-    {
-        int retval = -1;
+    public int getNameIndex(String name) {
 
-        for (int k = 0; k < names.size(); k++)
-        {
+        for (int k = 0; k < names.size(); k++) {
             String nameName = getNameName(k);
 
-            if (nameName.equalsIgnoreCase(name))
-            {
-                retval = k;
-                break;
+            if (nameName.equalsIgnoreCase(name)) {
+                return k;
             }
         }
-        return retval;
+        return -1;
     }
 
 
-    /** remove the named range by his index
-     * @param index named range index (0 based)
-     */
     public void removeName(int index){
         names.remove(index);
         workbook.removeName(index);
@@ -1456,14 +1449,11 @@ public class HSSFWorkbook extends POIDocument implements org.apache.poi.ss.userm
     return formatter;
     }
 
-    /** remove the named range by his name
-     * @param name named range name
-     */
-    public void removeName(String name){
+
+    public void removeName(String name) {
         int index = getNameIndex(name);
 
         removeName(index);
-
     }
 
     public HSSFPalette getCustomPalette()
index 051a48f67e0bfa1815cf3d522a59ae06b5aa1a4d..6ebf1013f8c8402fc9e4101265147164f6ffacd2 100644 (file)
@@ -274,48 +274,50 @@ public interface Workbook {
     void write(OutputStream stream) throws IOException;
 
     /**
-     * Gets the total number of named ranges in the workbook
-     *
-     * @return number of named ranges
+     * @return the total number of defined names in this workbook
      */
     int getNumberOfNames();
 
     /**
-     * Gets the Named range
-     *
-     * @param index position of the named range (0-based)
-     * @return named range high level
+     * @param name the name of the defined name
+     * @return the defined name with the specified name. <code>null</code> if not found.
      */
-    Name getNameAt(int index);
+    Name getName(String name);
+    /**
+     * @param nameIndex position of the named range (0-based)
+     * @return the defined name at the specified index
+     * @throws IllegalArgumentException if the supplied index is invalid
+     */
+    Name getNameAt(int nameIndex);
 
     /**
-     * Creates a new named range in this workbook
+     * Creates a new (uninitialised) defined name in this workbook
      *
-     * @return new named range object
+     * @return new defined name object
      */
     Name createName();
 
     /**
-     * Gets the named range index by his name
-      * <i>Note:</i>Excel named ranges are case-insensitive and
-      * this method performs a case-insensitive search.
-      *
-      * @param name named range name
-      * @return named range index
-      */
-     int getNameIndex(String name);
+     * Gets the defined name index by name<br/>
+     * <i>Note:</i> Excel defined names are case-insensitive and
+     * this method performs a case-insensitive search.
+     *
+     * @param name the name of the defined name
+     * @return zero based index of the defined name. <tt>-1</tt> if not found.
+     */
+    int getNameIndex(String name);
 
-     /**
-      * Remove the named range by his index
-      *
-      * @param index named range index (0 based)
-      */
-     void removeName(int index);
+    /**
+     * Remove the defined name at the specified index
+     *
+     * @param index named range index (0 based)
+     */
+    void removeName(int index);
 
     /**
-     * Remove the named range by his name
+     * Remove a defined name by name
      *
-     * @param name named range name
+      * @param name the name of the defined name
      */
     void removeName(String name);
 
index 874cd0f385f16390b8849cf2cd84c9c59bca2f75..9cb66c53118e8e7d5f19943195fd9df250002891 100644 (file)
@@ -417,11 +417,6 @@ public class XSSFWorkbook extends POIXMLDocument implements Workbook, Iterable<X
         return font;
     }
 
-    /**
-     * Create a new named range and add it to the workbook's names table
-     *
-     * @return named range high level
-     */
     public XSSFName createName() {
         XSSFName name = new XSSFName(CTDefinedName.Factory.newInstance(), this);
         namedRanges.add(name);
@@ -545,14 +540,24 @@ public class XSSFWorkbook extends POIXMLDocument implements Workbook, Iterable<X
         return stylesSource.getFontAt(idx);
     }
 
-    /**
-     * Gets the Named range at the given index number
-     *
-     * @param index position of the named range
-     * @return XSSFName at the index
-     */
-    public XSSFName getNameAt(int index) {
-        return namedRanges.get(index);
+    public XSSFName getName(String name) {
+        int nameIndex = getNameIndex(name);
+        if (nameIndex < 0) {
+            return null;
+        }
+        return namedRanges.get(nameIndex);
+    }
+
+    public XSSFName getNameAt(int nameIndex) {
+        int nNames = namedRanges.size();
+        if (nNames < 1) {
+            throw new IllegalStateException("There are no defined names in this workbook");
+        }
+        if (nameIndex < 0 || nameIndex > nNames) {
+            throw new IllegalArgumentException("Specified name index " + nameIndex 
+                    + " is outside the allowable range (0.." + (nNames-1) + ").");
+        }
+        return namedRanges.get(nameIndex);
     }
 
     /**
@@ -615,7 +620,7 @@ public class XSSFWorkbook extends POIXMLDocument implements Workbook, Iterable<X
      * @param sheetIndex Zero-based sheet index (0 Represents the first sheet to keep consistent with java)
      * @return String Null if no print area has been defined
      */
-    public String getPrintArea(int sheetIndex) {       
+    public String getPrintArea(int sheetIndex) {    
         XSSFName name = getBuiltInName(XSSFName.BUILTIN_PRINT_AREA, sheetIndex);
         if (name == null) return null;
         //adding one here because 0 indicates a global named region; doesnt make sense for print areas
@@ -713,21 +718,10 @@ public class XSSFWorkbook extends POIXMLDocument implements Workbook, Iterable<X
         return getPackagePart().getContentType().equals(XSSFRelation.MACROS_WORKBOOK.getContentType());
     }
 
-    /**
-     * Removes the name by its index
-     *
-     * @param nameIndex 0-based index of the name to remove.
-     */
     public void removeName(int nameIndex) {
         namedRanges.remove(nameIndex);
     }
 
-    /**
-     * Remove the named range by its name
-     *
-     * @param name named range name
-     * @throws IllegalArgumentException if the name was not found
-     */
     public void removeName(String name) {
         for (int i = 0; i < namedRanges.size(); i++) {
             XSSFName nm = namedRanges.get(i);
@@ -824,7 +818,7 @@ public class XSSFWorkbook extends POIXMLDocument implements Workbook, Iterable<X
         int lastSheetIx = sheets.size() - 1;
         if (index < 0 || index > lastSheetIx) {
             throw new IllegalArgumentException("Sheet index ("
-                    + index +") is out of range (0.." +        lastSheetIx + ")");
+                    + index +") is out of range (0.." +    lastSheetIx + ")");
         }
     }
 
@@ -918,7 +912,7 @@ public class XSSFWorkbook extends POIXMLDocument implements Workbook, Iterable<X
     public void setRepeatingRowsAndColumns(int sheetIndex,
                                            int startColumn, int endColumn,
                                            int startRow, int endRow) {
-        //     Check arguments
+        //    Check arguments
         if ((startColumn == -1 && endColumn != -1) || startColumn < -1 || endColumn < -1 || startColumn > endColumn)
             throw new IllegalArgumentException("Invalid column range specification");
         if ((startRow == -1 && endRow != -1) || startRow < -1 || endRow < -1 || startRow > endRow)
@@ -1223,11 +1217,11 @@ public class XSSFWorkbook extends POIXMLDocument implements Workbook, Iterable<X
     }
     
     public boolean isHidden() {
-       throw new RuntimeException("Not implemented yet");
+        throw new RuntimeException("Not implemented yet");
     }
 
     public void setHidden(boolean hiddenFlag) {
-       throw new RuntimeException("Not implemented yet");
+        throw new RuntimeException("Not implemented yet");
     }
 
     public boolean isSheetHidden(int sheetIx) {