Bug 22720 - Exception occurs when deleting merged cells from cloned sheet
Summary: Exception occurs when deleting merged cells from cloned sheet
Alias: None
Product: POI
Classification: Unclassified
Component: HSSF (show other bugs)
Version: 2.0-pre3
Hardware: PC Windows XP
: P3 normal (vote)
Target Milestone: ---
Assignee: POI Developers List
Depends on:
Reported: 2003-08-26 03:20 UTC by Harry
Modified: 2015-09-22 03:08 UTC (History)
0 users


Note You need to log in before you can comment on or make changes to this bug.
Description Harry 2003-08-26 03:20:05 UTC
I clone a worksheet with merged cells, and then delete the merged cells from 
the clone:

HSSFSheet sheet = workBook.cloneSheet(templateSheetIndex);

for (int i=sheet.getNumMergedRegions()-1; i>=0; i--) {

This results in:

java.lang.IndexOutOfBoundsException: Index: 157, Size: 41
	at java.util.ArrayList.RangeCheck(ArrayList.java:508)
	at java.util.ArrayList.remove(ArrayList.java:388)
	at org.apache.poi.hssf.model.Sheet.removeMergedRegion(Sheet.java:506)
	at org.apache.poi.hssf.usermodel.HSSFSheet.removeMergedRegion

Index and size shown in the error message doesn't make sense because the 
source sheet only has 4 merged cells.
Comment 1 Danny Mui 2003-08-26 14:43:01 UTC
will correct for 2.0
Comment 2 Avik Sengupta 2003-10-28 18:24:58 UTC
Danny, is this still valid? 
Comment 3 Harry 2003-11-11 21:06:06 UTC
Tested the problem scenario with RC1, it's better than before, but now there's 
a different bug:

HSSFSheet template = workBook.getSheetAt(templateSheetIndex);
HSSFSheet clone = workBook.cloneSheet(templateSheetIndex);

// remove merged regions from clone
for (int i=sheet.getNumMergedRegions()-1; i>=0; i--) {

// check if template's merged regions are OK
if (template.getNumMergedRegions()>0) {
   // fetch the first merged region...EXCEPTION OCCURS HERE

This results in:

java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
	at java.util.ArrayList.RangeCheck(ArrayList.java:508)
	at java.util.ArrayList.get(ArrayList.java:320)
	at org.apache.poi.hssf.record.MergeCellsRecord.getAreaAt
	at org.apache.poi.hssf.model.Sheet.getMergedRegionAt(Sheet.java:543)
	at org.apache.poi.hssf.usermodel.HSSFSheet.getMergedRegionAt

From this test, the delete of cloned merged regions works.  However, it 
appears that the original sheet's merged regions are also deleted.  The 
template.getNumMergedRegions() returns the correct value (in my test 
spreadsheet there are 4 merged regions), but throws an Exception when 
getMergedRegionAt() is called to retrieve the regions.
Comment 4 Danny Mui 2003-11-17 21:12:02 UTC
Thanks for the bug report, committed November 17.  Should be in RC2 or Release
(minor fix).
Comment 5 Javen O'Neal 2015-09-22 03:08:43 UTC
I'm fixing another bug to detect overlapping merged regions (not allowed by Microsoft Excel), which breaks this unit test.

I'm assuming that two merged regions are overlapping is coincidental/unintentional and doesn't change the meaning of this test if the addresses are changed.

//Original code[1]
template.addMergedRegion(new Region(0, (short)0, 1, (short)2));
template.addMergedRegion(new Region(1, (short)0, 2, (short)2));

//Latest code
template.addMergedRegion(new CellRangeAddress(0, 1, 0, 2));
template.addMergedRegion(new CellRangeAddress(1, 2, 0, 2));

[1] https://svn.apache.org/viewvc/jakarta/poi/trunk/src/testcases/org/apache/poi/hssf/usermodel/TestBugs.java?revision=353586&view=markup&pathrev=353586#l374
[2] https://svn.apache.org/viewvc/poi/trunk/src/testcases/org/apache/poi/ss/usermodel/BaseTestBugzillaIssues.java?revision=1704124&view=markup#l140