View | Details | Raw Unified | Return to bug 45126
Collapse All | Expand All

(-)java/org/apache/poi/hssf/model/Workbook.java (-16 / +59 lines)
Lines 105-110 Link Here
105
105
106
    private static POILogger   log = POILogFactory.getLogger(Workbook.class);
106
    private static POILogger   log = POILogFactory.getLogger(Workbook.class);
107
107
108
	private static final String EXCEL_REPEATING_NAME_PREFIX_ = "Excel_Name_Record_Titles_";
109
108
    /**
110
    /**
109
     * Creates new Workbook with no intitialization --useless right now
111
     * Creates new Workbook with no intitialization --useless right now
110
     * @see #createWorkbook(List)
112
     * @see #createWorkbook(List)
Lines 1918-1945 Link Here
1918
     */
1920
     */
1919
    public NameRecord addName(NameRecord name)
1921
    public NameRecord addName(NameRecord name)
1920
    {
1922
    {
1921
        
1923
    	LinkTable linkTable = getOrCreateLinkTable();
1922
        getOrCreateLinkTable().addName(name);
1924
    	      if (checkNameAlreadyExists(linkTable, name)) {
1923
1925
    	            throw new IllegalArgumentException(
1924
        return name;
1926
                        "You are trying to assign a duplicated name record: "
1927
    	                            + name.getNameText());
1928
    	
1929
    	        }
1930
    	        linkTable.addName(name);
1931
    	return name;   
1925
    }
1932
    }
1933
    
1934
    
1935
    /**
1936
         * checks if the given name is already included in the given linkTable
1937
         * @param linkTable
1938
         * @param name
1939
         * @return
1940
         */
1941
        private boolean checkNameAlreadyExists(LinkTable linkTable, NameRecord name) {
1942
        	 //Check to ensure no other names have the same case-insensitive name
1943
            for ( int i = getNumNames()-1; i >=0; i-- )
1944
            {
1945
            	NameRecord rec = getNameRecord(i);
1946
            	if (rec != name) {
1947
            		if (isDuplicatedNames(name, rec))
1948
            			return true;
1949
            	}
1950
            }         	        
1951
            return false;
1952
        }
1926
1953
1954
	private boolean isDuplicatedNames(NameRecord firstName, NameRecord lastName) {
1955
		return lastName.getNameText().equalsIgnoreCase(firstName.getNameText()) && isSameSheetNames(firstName, lastName);
1956
	}
1957
1958
	private boolean isSameSheetNames(NameRecord firstName, NameRecord lastName) {
1959
		return lastName.getEqualsToIndexToSheet() == firstName.getEqualsToIndexToSheet();
1960
	}
1961
1927
    /**Generates a NameRecord to represent a built-in region
1962
    /**Generates a NameRecord to represent a built-in region
1928
     * @return a new NameRecord unless the index is invalid
1963
     * @return a new NameRecord unless the index is invalid
1929
     */
1964
     */
1930
    public NameRecord createBuiltInName(byte builtInName, int index)
1965
        public NameRecord createBuiltInName(byte builtInName, int index)
1931
    {
1966
        {
1932
        if (index == -1 || index+1 > Short.MAX_VALUE) 
1967
           if (index == -1 || index + 1 > Short.MAX_VALUE)
1933
            throw new IllegalArgumentException("Index is not valid ["+index+"]");
1968
               throw new IllegalArgumentException("Index is not valid [" + index
1934
        
1969
                       + "]");
1935
        NameRecord name = new NameRecord(builtInName, (short)(index));
1970
   
1936
                
1971
           NameRecord name = new NameRecord(builtInName, (short) (index));
1937
        addName(name);
1972
           
1938
        
1973
           String prefix = EXCEL_REPEATING_NAME_PREFIX_ + index + " ";
1939
        return name;
1974
           int cont = 0;
1940
    }
1975
           while (checkNameAlreadyExists(linkTable, name)) {
1976
               cont++;
1977
               name = new NameRecord();
1978
               String built_in_name = prefix + cont;
1979
               // It would be better to set a different builtInName here. It does not seem possible
1980
               name.setNameText(built_in_name);
1981
           }
1982
           addName(name);
1983
           return name;
1984
        }
1941
1985
1942
1943
    /** removes the name
1986
    /** removes the name
1944
     * @param namenum name index
1987
     * @param namenum name index
1945
     */
1988
     */
(-)testcases/org/apache/poi/hssf/usermodel/TestWorkbook.java (+27 lines)
Lines 584-589 Link Here
584
        fileOut.close();
584
        fileOut.close();
585
585
586
        assertTrue("file exists",file.exists());
586
        assertTrue("file exists",file.exists());
587
        
588
        // ****************************************
589
        
590
        String filename = System.getProperty("HSSF.testdata.path");
591
        
592
		int i = 0;
593
		for (int rowItem = 0; rowItem < 10; rowItem++) {
594
			HSSFRow r = sheet.createRow(rowItem);
595
			for (int column = 0; column < 2; column++) {
596
				HSSFCell cellItem = r.createCell((short) column);
597
				cellItem.setCellType(HSSFCell.CELL_TYPE_STRING);
598
				cellItem.setCellValue("Some value here");
599
				if (rowItem == 2) {
600
					workbook.setRepeatingRowsAndColumns(0, 0, 0, 0, 3 - 1);
601
					sheet.createFreezePane(0, 3);
602
				} 
603
			}
604
		}
605
606
		for (int y = 0; y < workbook.getNumberOfNames(); y++) {
607
			HSSFName nr = workbook.getNameAt(y);
608
			System.out.println("name: " + nr.getNameName() + " ref: " + nr.getReference());
609
		}
610
611
		FileOutputStream fos = new FileOutputStream(filename + "/45126.xls");
612
		workbook.write(fos);
613
		fos.close();
587
    }
614
    }
588
615
589
616

Return to bug 45126