1 /* ==================================================================== 2 * The Apache Software License, Version 1.1 3 * 4 * Copyright (c) 2002 The Apache Software Foundation. All rights 5 * reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 14 * 2. Redistributions in binary form must reproduce the above copyright 15 * notice, this list of conditions and the following disclaimer in 16 * the documentation and/or other materials provided with the 17 * distribution. 18 * 19 * 3. The end-user documentation included with the redistribution, 20 * if any, must include the following acknowledgment: 21 * "This product includes software developed by the 22 * Apache Software Foundation (http://www.apache.org/)." 23 * Alternately, this acknowledgment may appear in the software itself, 24 * if and wherever such third-party acknowledgments normally appear. 25 * 26 * 4. The names "Apache" and "Apache Software Foundation" and 27 * "Apache POI" must not be used to endorse or promote products 28 * derived from this software without prior written permission. For 29 * written permission, please contact apache@apache.org. 30 * 31 * 5. Products derived from this software may not be called "Apache", 32 * "Apache POI", nor may "Apache" appear in their name, without 33 * prior written permission of the Apache Software Foundation. 34 * 35 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED 36 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 37 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 38 * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR 39 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 40 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 41 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF 42 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 43 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 44 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT 45 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 46 * SUCH DAMAGE. 47 * ==================================================================== 48 * 49 * This software consists of voluntary contributions made by many 50 * individuals on behalf of the Apache Software Foundation. For more 51 * information on the Apache Software Foundation, please see 52 * <http://www.apache.org/>. 53 */ 54 55 package org.apache.poi.hssf.usermodel; 56 57 import org.apache.poi.util.POILogFactory; 58 import org.apache.poi.hssf.model.Sheet; 59 import org.apache.poi.hssf.model.Workbook; 60 import org.apache.poi.hssf.record.*; 61 import org.apache.poi.hssf.util.Region; 62 import org.apache.poi.util.POILogger; 63 64 import java.util.Iterator; 65 import java.util.TreeMap; 66 import org.apache.poi.hssf.util.RangeAddress; 67 import org.apache.poi.hssf.util.SheetReferences; 68 69 /** 70 * Title: High Level Represantion of Named Range <P> 71 * REFERENCE: <P> 72 * @author Libin Roman (Vista Portal LDT. Developer) 73 * @version 1.0-pre 74 */ 75 76 public class HSSFName { 77 private Workbook book; 78 private NameRecord name; 79 80 /** Creates new HSSFName - called by HSSFWorkbook to create a sheet from 81 * scratch. 82 * 83 * @see #org.apache.poi.hssf.usermodel.HSSFWorkbook.createName() 84 * @param name the Name Record 85 * @param book - lowlevel Workbook object associated with the sheet. 86 * @param book the Workbook */ 87 88 protected HSSFName(Workbook book, NameRecord name) { 89 this.book = book; 90 this.name = name; 91 } 92 93 /** 94 * private default constructor prevents bogus initializationless 95 * construction 96 */ 97 98 private HSSFName() { 99 } 100 101 /** Get the sheets name which this named range is referenced to 102 * @return sheet name, which this named range refered to 103 */ 104 105 public String getSheetName() { 106 String result ; 107 short indexToExternSheet = name.getExternSheetNumber(); 108 109 result = book.findSheetNameFromExternSheet(indexToExternSheet); 110 111 return result; 112 } 113 114 /** 115 * gets the name of the named range 116 * @return named range name 117 */ 118 119 public String getNameName(){ 120 String result = name.getNameText(); 121 122 return result; 123 } 124 125 /** 126 * sets the name of the named range 127 * @param nameName named range name to set 128 */ 129 130 public void setNameName(String nameName){ 131 name.setNameText(nameName); 132 name.setNameTextLength((byte)nameName.length()); 133 } 134 135 /** 136 * gets the reference of the named range 137 * @return reference of the named range 138 */ 139 140 public String getReference() { 141 String result; 142 SheetReferences refs = book.getSheetReferences(); 143 result = name.getAreaReference(refs); 144 145 return result; 146 } 147 148 149 150 /** 151 * sets the sheet name which this named range referenced to 152 * @param sheetName the sheet name of the reference 153 */ 154 155 private void setSheetName(String sheetName){ 156 int sheetNumber = book.getSheetIndex(sheetName); 157 158 short externSheetNumber = book.checkExternSheet(sheetNumber); 159 name.setExternSheetNumber(externSheetNumber); 160 // name.setIndexToSheet(externSheetNumber); 161 162 } 163 164 165 /** 166 * sets the reference of this named range 167 * @param ref the reference to set 168 */ 169 170 public void setReference(String ref){ 171 172 RangeAddress ra = new RangeAddress(ref); 173 174 String sheetName = ra.getSheetName(); 175 176 if (ra.hasSheetName()) { 177 setSheetName(sheetName); 178 } 179 180 if (ra.getFromCell().equals(ra.getToCell()) == false) { 181 name.setAreaReference(ra.getFromCell() + ":" + ra.getToCell()); 182 } else { 183 name.setAreaReference(ra.getFromCell()); 184 } 185 186 } 187 188 } 189 190