Bug 46009 - Cloning sheet with merged region causes ArrayStoreException when saving.
Summary: Cloning sheet with merged region causes ArrayStoreException when saving.
Status: RESOLVED FIXED
Alias: None
Product: POI
Classification: Unclassified
Component: HSSF (show other bugs)
Version: 3.5-dev
Hardware: PC Mac OS X 10.4
: P2 normal (vote)
Target Milestone: ---
Assignee: POI Developers List
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2008-10-14 20:34 UTC by David Agnew
Modified: 2008-10-27 14:33 UTC (History)
1 user (show)



Attachments
A test case that clones sheets with and without a merged region. (9.40 KB, application/zip)
2008-10-14 20:34 UTC, David Agnew
Details

Note You need to log in before you can comment on or make changes to this bug.
Description David Agnew 2008-10-14 20:34:00 UTC
Created attachment 22730 [details]
A test case that clones sheets with and without a merged region.

If you clone a sheet that has a merged region, you get an exception when you attempt to write the workbook:

$ run
There is 1 sheet.
There are 2 sheets.
Worked for: noMergedRegion
There is 1 sheet.
There are 2 sheets.
Failed for: hasMergedRegion
java.lang.ArrayStoreException
	at java.lang.System.arraycopy(Native Method)
	at java.util.ArrayList.toArray(ArrayList.java:304)
	at org.apache.poi.hssf.record.aggregates.MergedCellsTable.visitContainedRecords(MergedCellsTable.java:85)
	at org.apache.poi.hssf.model.Sheet.visitContainedRecords(Sheet.java:577)
	at org.apache.poi.hssf.usermodel.HSSFWorkbook.getBytes(HSSFWorkbook.java:1319)
	at org.apache.poi.hssf.usermodel.HSSFWorkbook.write(HSSFWorkbook.java:1229)
	at TestCase.test(TestCase.java:30)
	at TestCase.main(TestCase.java:10)

Code to reproduced the above is attached.

Workaround: Remove all the merged regions from the cloned sheet. Then loop over the merged regions in the original sheet and add each one found to the cloned sheet. The resulting workbook saves without a problem.
Comment 1 David Agnew 2008-10-15 10:51:21 UTC
If you clone a sheet with a merged region and then call getMergedRegion on the clone, it falls over at line 111 in MergedCellsTable with a ClassCastException trying to return a value retrieved from an internal ArrayList as a CellRangeAddress.

java.lang.ClassCastException: org.apache.poi.ss.util.CellRangeAddress
org/apache/poi/hssf/record/aggregates/MergedCellsTable.java:111:in `get'
org/apache/poi/hssf/model/Sheet.java:506:in `getMergedRegionAt'
org/apache/poi/hssf/usermodel/HSSFSheet.java:674:in `getMergedRegion'
Comment 2 Josh Micich 2008-10-27 14:33:34 UTC
fixed in svn r708325.

Junit added.