Slight tweak for named ranges pointing at deleted sheets, related to bug #30978
[poi.git] / src / java / org / apache / poi / hssf / usermodel / HSSFName.java
blob240be13f5819255d1b7f45d15e5046f92e912194
1 /* ====================================================================
2 Licensed to the Apache Software Foundation (ASF) under one or more
3 contributor license agreements. See the NOTICE file distributed with
4 this work for additional information regarding copyright ownership.
5 The ASF licenses this file to You under the Apache License, Version 2.0
6 (the "License"); you may not use this file except in compliance with
7 the License. You may obtain a copy of the License at
9 http://www.apache.org/licenses/LICENSE-2.0
11 Unless required by applicable law or agreed to in writing, software
12 distributed under the License is distributed on an "AS IS" BASIS,
13 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 See the License for the specific language governing permissions and
15 limitations under the License.
16 ==================================================================== */
18 package org.apache.poi.hssf.usermodel;
20 import org.apache.poi.hssf.model.Workbook;
21 import org.apache.poi.hssf.record.NameRecord;
22 import org.apache.poi.hssf.util.RangeAddress;
24 /**
25 * Title: High Level Represantion of Named Range <P>
26 * REFERENCE: <P>
27 * @author Libin Roman (Vista Portal LDT. Developer)
30 public class HSSFName {
31 private HSSFWorkbook book;
32 private NameRecord name;
34 /** Creates new HSSFName - called by HSSFWorkbook to create a sheet from
35 * scratch.
37 * @see org.apache.poi.hssf.usermodel.HSSFWorkbook#createName()
38 * @param name the Name Record
39 * @param book lowlevel Workbook object associated with the sheet.
42 protected HSSFName(HSSFWorkbook book, NameRecord name) {
43 this.book = book;
44 this.name = name;
47 /** Get the sheets name which this named range is referenced to
48 * @return sheet name, which this named range refered to
49 */
51 public String getSheetName() {
52 String result ;
53 short indexToExternSheet = name.getExternSheetNumber();
55 result = book.getWorkbook().findSheetNameFromExternSheet(indexToExternSheet);
57 return result;
60 /**
61 * gets the name of the named range
62 * @return named range name
63 */
65 public String getNameName(){
66 String result = name.getNameText();
68 return result;
71 /**
72 * sets the name of the named range
73 * @param nameName named range name to set
74 */
76 public void setNameName(String nameName){
77 name.setNameText(nameName);
78 name.setNameTextLength((byte)nameName.length());
79 Workbook wb = book.getWorkbook();
81 //Check to ensure no other names have the same case-insensitive name
82 for ( int i = wb.getNumNames()-1; i >=0; i-- )
84 NameRecord rec = wb.getNameRecord(i);
85 if (rec != name) {
86 if (rec.getNameText().equalsIgnoreCase(getNameName()))
87 throw new IllegalArgumentException("The workbook already contains this name (case-insensitive)");
92 /**
93 * gets the reference of the named range
94 * @return reference of the named range
95 */
97 public String getReference() {
98 String result;
99 result = name.getAreaReference(book);
101 return result;
106 /**
107 * sets the sheet name which this named range referenced to
108 * @param sheetName the sheet name of the reference
111 private void setSheetName(String sheetName){
112 int sheetNumber = book.getSheetIndex(sheetName);
114 short externSheetNumber = book.getExternalSheetIndex(sheetNumber);
115 name.setExternSheetNumber(externSheetNumber);
116 // name.setIndexToSheet(externSheetNumber);
121 /**
122 * sets the reference of this named range
123 * @param ref the reference to set
126 public void setReference(String ref){
128 RangeAddress ra = new RangeAddress(ref);
130 String sheetName = ra.getSheetName();
132 if (ra.hasSheetName()) {
133 setSheetName(sheetName);
136 //allow the poi utilities to parse it out
137 name.setAreaReference(ref);
142 * Tests if this name points to a cell that no longer exists
144 * @return true if the name refers to a deleted cell, false otherwise
146 public boolean isDeleted(){
147 String ref = getReference();
148 return "#REF!".endsWith(ref);