diff --git a/src/java/org/apache/poi/hssf/model/Sheet.java b/src/java/org/apache/poi/hssf/model/Sheet.java index 9a7abadc3f..1dbafd7bc1 100644 --- a/src/java/org/apache/poi/hssf/model/Sheet.java +++ b/src/java/org/apache/poi/hssf/model/Sheet.java @@ -469,11 +469,12 @@ public class Sheet implements Model numMergedRegions--; if (rec.getNumAreas() == 0) { - mergedRecords.remove(pos); + if (merged == rec) merged = (MergeCellsRecord) mergedRecords.get(mergedRecords.size() - 1); int removePos = ((Integer) mergedLocs.get(pos)).intValue(); records.remove(removePos); + mergedRecords.remove(pos); mergedLocs.remove(pos); } } diff --git a/src/testcases/org/apache/poi/hssf/usermodel/TestHSSFSheet.java b/src/testcases/org/apache/poi/hssf/usermodel/TestHSSFSheet.java index 8e58186b55..66d475edc9 100644 --- a/src/testcases/org/apache/poi/hssf/usermodel/TestHSSFSheet.java +++ b/src/testcases/org/apache/poi/hssf/usermodel/TestHSSFSheet.java @@ -54,18 +54,20 @@ package org.apache.poi.hssf.usermodel; -import junit.framework.TestCase; -import org.apache.poi.hssf.model.Sheet; -import org.apache.poi.hssf.record.HCenterRecord; -import org.apache.poi.hssf.record.VCenterRecord; -import org.apache.poi.hssf.record.WSBoolRecord; -import org.apache.poi.hssf.record.SCLRecord; -import org.apache.poi.hssf.record.WindowTwoRecord; - import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; +import junit.framework.TestCase; + +import org.apache.poi.hssf.model.Sheet; +import org.apache.poi.hssf.record.HCenterRecord; +import org.apache.poi.hssf.record.SCLRecord; +import org.apache.poi.hssf.record.VCenterRecord; +import org.apache.poi.hssf.record.WSBoolRecord; +import org.apache.poi.hssf.record.WindowTwoRecord; +import org.apache.poi.hssf.util.Region; + /** * Tests HSSFSheet. This test case is very incomplete at the moment. * @@ -254,4 +256,30 @@ public class TestHSSFSheet assertTrue(sclLoc == window2Loc + 1); } + + + /** + * When removing one merged region, it would break + * + */ + public void testRemoveMerged() { + HSSFWorkbook wb = new HSSFWorkbook(); + HSSFSheet sheet = wb.createSheet(); + Region region = new Region(0, (short)0, 1, (short)1); + sheet.addMergedRegion(region); + region = new Region(1, (short)0, 2, (short)1); + sheet.addMergedRegion(region); + + sheet.removeMergedRegion(0); + + region = sheet.getMergedRegionAt(0); + assertEquals("Left over region should be starting at row 1", 1, region.getRowFrom()); + + sheet.removeMergedRegion(0); + } + + public static void main(java.lang.String[] args) { + junit.textui.TestRunner.run(TestHSSFSheet.class); + } + }