Bug 27622

Summary: Named range reference becomes invalid on removeSheet
Product: POI Reporter: Simon Niederberger <simon.niederberger>
Component: HSSFAssignee: POI Developers List <dev>
Status: RESOLVED LATER    
Severity: normal    
Priority: P3    
Version: 2.0-FINAL   
Target Milestone: ---   
Hardware: PC   
OS: All   

Description Simon Niederberger 2004-03-12 08:24:07 UTC
(1) My workbook has sheets SHA, SHB, SHC, ...
(2) Create a named range pointing to e.g. SHD!$A$2:$A$16
(3) Delete some sheets (not SHD, of course)
(4) The named range now points to e.g. SHA!$A$2:$A$16

From taking a glimpse at HSSFName, I guess named ranges are internally linked 
to the sheet nidex rather than the name. I guess when running 
workbook.removeSheet, the indexes of the named ranges are not update (or 
something like that)
NOTE: haven't tried release 2.5, maybe it's already fixed there.

Thanks nd good luck
Simon
Comment 1 Simon Niederberger 2004-03-15 19:55:42 UTC
Here's the source code (snippet) [works with a workbook containing sheets 
SHA ... SHG]

			// **** CREATE NAMED RANGE **** 
			int idxRange = _book.getNameIndex("MYRANGE");
			HSSFName name;
			if (idxRange == -1) {
				name = _book.createName();
				name.setNameName("MYRANGE");
			}
			else {
				name = _book.getNameAt(idxRange);
			}
			
			name.setReference("SHC!$A$2:$A$8");

			// **** DELETE SOME SHEETS **** 
			int idxSheet;
			idxSheet = _book.getSheetIndex("SHB");
			if (idxSheet > 0) {
				_book.removeSheetAt(idxSheet);
			}
			
			idxSheet = _book.getSheetIndex("SHF");
			if (idxSheet > 0) {
				_book.removeSheetAt(idxSheet);
			}

... and MYRANGE will point to SHD!$A$2:$A$8 (instead of SHC).

Current workaround is to first delete the sheets, then create the range, so 
the bug fix isn't really urgent.
Comment 2 Yegor Kozlov 2008-05-17 04:33:35 UTC
Could you please try poi-3.1-beta1 and report if the problem still persists? 

Yegor
Comment 3 Simon Niederberger 2008-05-20 07:54:24 UTC
Sorry, I've moved to a different project in the meantime and don't have the original source code available.
Comment 4 Nick Burch 2008-08-05 15:57:54 UTC
Guessing this is now fixed. Please re-open the bug if it still occurs on re-testing with a recent version (eg 3.1), if you do happen to find the source to test against again