1    
2    
3    /* ====================================================================
4    
5     * The Apache Software License, Version 1.1
6    
7     *
8    
9     * Copyright (c) 2002 The Apache Software Foundation.  All rights
10   
11    * reserved.
12   
13    *
14   
15    * Redistribution and use in source and binary forms, with or without
16   
17    * modification, are permitted provided that the following conditions
18   
19    * are met:
20   
21    *
22   
23    * 1. Redistributions of source code must retain the above copyright
24   
25    *    notice, this list of conditions and the following disclaimer.
26   
27    *
28   
29    * 2. Redistributions in binary form must reproduce the above copyright
30   
31    *    notice, this list of conditions and the following disclaimer in
32   
33    *    the documentation and/or other materials provided with the
34   
35    *    distribution.
36   
37    *
38   
39    * 3. The end-user documentation included with the redistribution,
40   
41    *    if any, must include the following acknowledgment:
42   
43    *       "This product includes software developed by the
44   
45    *        Apache Software Foundation (http://www.apache.org/)."
46   
47    *    Alternately, this acknowledgment may appear in the software itself,
48   
49    *    if and wherever such third-party acknowledgments normally appear.
50   
51    *
52   
53    * 4. The names "Apache" and "Apache Software Foundation" and
54   
55    *    "Apache POI" must not be used to endorse or promote products
56   
57    *    derived from this software without prior written permission. For
58   
59    *    written permission, please contact apache@apache.org.
60   
61    *
62   
63    * 5. Products derived from this software may not be called "Apache",
64   
65    *    "Apache POI", nor may "Apache" appear in their name, without
66   
67    *    prior written permission of the Apache Software Foundation.
68   
69    *
70   
71    * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
72   
73    * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
74   
75    * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
76   
77    * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
78   
79    * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
80   
81    * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
82   
83    * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
84   
85    * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
86   
87    * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
88   
89    * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
90   
91    * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
92   
93    * SUCH DAMAGE.
94   
95    * ====================================================================
96   
97    *
98   
99    * This software consists of voluntary contributions made by many
100  
101   * individuals on behalf of the Apache Software Foundation.  For more
102  
103   * information on the Apache Software Foundation, please see
104  
105   * <http://www.apache.org/>.
106  
107   */
108  
109  
110  
111  package org.apache.poi.hssf.usermodel;
112  
113  
114  
115  import org.apache.poi.util.POILogFactory;
116  
117  import org.apache.poi.hssf.model.Sheet;
118  
119  import org.apache.poi.hssf.model.Workbook;
120  
121  import org.apache.poi.hssf.record.*;
122  
123  import org.apache.poi.hssf.util.Region;
124  
125  import org.apache.poi.util.POILogger;
126  
127  
128  
129  import java.util.Iterator;
130  
131  import java.util.TreeMap;
132  
133  import org.apache.poi.hssf.util.RangeAddress;
134  
135  
136  
137  /**
138  
139   * Title:        High Level Represantion of Named Range <P>
140  
141   * REFERENCE:  <P>
142  
143   * @author Libin Roman (Vista Portal LDT. Developer)
144  
145   * @version 1.0-pre
146  
147   */
148  
149  
150  
151  public class HSSFName {
152  
153      private Workbook         book;
154  
155      private NameRecord       name;
156  
157      
158  
159      /** Creates new HSSFName   - called by HSSFWorkbook to create a sheet from
160  
161       * scratch.
162  
163       *
164  
165       * @see #org.apache.poi.hssf.usermodel.HSSFWorkbook.createName()
166  
167       * @param name the Name Record
168  
169       * @param book - lowlevel Workbook object associated with the sheet.
170  
171       * @param book the Workbook */
172  
173      
174  
175      protected HSSFName(Workbook book, NameRecord name) {
176  
177          this.book = book;
178  
179          this.name = name;
180  
181      }
182  
183      
184  
185      /** private default constructor prevents bogus initializationless construction */
186  
187      
188  
189      private HSSFName() {
190  
191      }
192  
193      
194  
195      /** Get the sheets name which this named range is referenced to
196  
197       * @return sheet name, which this named range refered to
198  
199       */    
200  
201      public String getSheetName() {
202  
203          String result ;
204  
205          short indexToExternSheet = name.getExternSheetNumber();
206  
207          
208  
209          result = book.findSheetNameFromExternSheet(indexToExternSheet);
210  
211          
212  
213          return result;
214  
215      }
216  
217      
218  
219      /** gets the name of the named range
220  
221       * @return named range name
222  
223       */    
224  
225      public String getNameName(){
226  
227          String result = name.getNameText();
228  
229          
230  
231          return result;
232  
233      }
234  
235      
236  
237      /** sets the name of the named range
238  
239       * @param nameName named range name to set
240  
241       */    
242  
243      public void setNameName(String nameName){
244  
245          name.setNameText(nameName);
246  
247          name.setNameTextLength((byte)nameName.length());
248  
249      }
250  
251      
252  
253      /** gets the reference of the named range
254  
255       * @return reference of the named range
256  
257       */    
258  
259      public String getReference() {
260  
261          String result;
262  
263          
264  
265          result = getSheetName() + "." + name.getAreaReference();
266  
267          
268  
269          return result;
270  
271      }
272  
273      
274  
275      /** sets the sheet name which this named range referenced to
276  
277       * @param sheetName the sheet name of the reference
278  
279       */    
280  
281      public void setSheetName(String sheetName){
282  
283          int sheetNumber = book.getSheetIndex(sheetName);
284  
285          
286  
287          short externSheetNumber = book.checkExternSheet(sheetNumber);
288  
289          name.setExternSheetNumber(externSheetNumber);
290  
291  //        name.setIndexToSheet(externSheetNumber);
292  
293      }
294  
295      
296  
297      /** sets the reference of this named range
298  
299       * @param ref the reference to set
300  
301       */    
302  
303      public void setReference(String ref){
304  
305          RangeAddress ra = new RangeAddress(ref);
306  
307          
308  
309          String sheetName = ra.getSheetName();
310  
311          
312  
313          if (ra.hasSheetName()) {
314  
315              setSheetName(sheetName);
316  
317          }
318  
319          
320  
321          if (ra.getFromCell().equals(ra.getToCell()) == false) {
322  
323              name.setAreaReference(ra.getFromCell() + ":" + ra.getToCell());
324  
325          } else {
326  
327              name.setAreaReference(ra.getFromCell());            
328  
329          }
330  
331          
332  
333      }
334  
335      
336  
337              
338  
339  }
340  
341  ???????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????HSSFName?????????????Workbook??????????????????????????????book?????????????NameRecord??????????????????????????????name???????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????HSSFName????????????????????????Workbook???????????????????????????????????????NameRecord?????????????????????book?????????????????????name???????????????????????????????????????????????????????????????????????????????????????????????????HSSFName??????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????getSheetName????????????????????????????????????name?????????????????????????????????????????getExternSheetNumber?????????result??????????????????book???????????????????????findSheetNameFromExternSheet????????????????????????????????????????????????????indexToExternSheet????????????????result??????????????????????????????????????????????????????????????????????????????????????????????????????getNameName?????????????????????????name??????????????????????????????getNameText????????????????result???????????????????????????????????????????????????????????????????????????????????????????????????????????????????setNameName?????????name??????????????setNameText??????????????????????????nameName?????????name??????????????setNameTextLength??????????????????????????????????????nameName???????????????????????????????????????????????????????????????????????????????????????????????????????????????????????getReference?????????result??????????????????getSheetName?????????????????????????????????????????name??????????????????????????????????????????????getAreaReference????????????????result????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????setSheetName???????????????????????????book????????????????????????????????getSheetIndex??????????????????????????????????????????????sheetName???????????????????????????????????book????????????????????????????????????????checkExternSheet?????????????????????????????????????????????????????????sheetNumber?????????name??????????????setExternSheetNumber???????????????????????????????????externSheetNumber????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????setReference?????????RangeAddress???????????????????????????????RangeAddress????????????????????????????????????????????ref????????????????????????????ra???????????????????????????????getSheetName?????????????ra????????????????hasSheetName?????????????setSheetName??????????????????????????sheetName?????????????ra????????????????getFromCell?????????????????????????????????????ra????????????????????????????????????????getToCell?????????????name??????????????????setAreaReference???????????????????????????????????ra??????????????????????????????????????getFromCell????????????????????????????????????????????????????????????ra???????????????????????????????????????????????????????????????getToCell?????????????name??????????????????setAreaReference???????????????????????????????????ra??????????????????????????????????????getFromCell