mirror of https://github.com/apache/poi.git
Changed merged region tracking to use object pointers rather than
index pointers so we don't have to make sure the indices are up to date. PR: 22922 Submitted by: xuemin@appresso.com (Xuemin Guan) git-svn-id: https://svn.apache.org/repos/asf/jakarta/poi/trunk@353344 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
53a4992600
commit
6be7d56b1a
|
@ -112,7 +112,6 @@ public class Sheet implements Model
|
||||||
protected MergeCellsRecord merged = null;
|
protected MergeCellsRecord merged = null;
|
||||||
protected Margin margins[] = null;
|
protected Margin margins[] = null;
|
||||||
protected ArrayList mergedRecords = new ArrayList();
|
protected ArrayList mergedRecords = new ArrayList();
|
||||||
protected ArrayList mergedLocs = new ArrayList();
|
|
||||||
protected int numMergedRegions = 0;
|
protected int numMergedRegions = 0;
|
||||||
protected SelectionRecord selection = null;
|
protected SelectionRecord selection = null;
|
||||||
private static POILogger log = POILogFactory.getLogger(Sheet.class);
|
private static POILogger log = POILogFactory.getLogger(Sheet.class);
|
||||||
|
@ -198,7 +197,6 @@ public class Sheet implements Model
|
||||||
{
|
{
|
||||||
retval.mergedRecords.add(rec);
|
retval.mergedRecords.add(rec);
|
||||||
retval.merged = ( MergeCellsRecord ) rec;
|
retval.merged = ( MergeCellsRecord ) rec;
|
||||||
retval.mergedLocs.add(new Integer(k - offset));
|
|
||||||
retval.numMergedRegions += retval.merged.getNumAreas();
|
retval.numMergedRegions += retval.merged.getNumAreas();
|
||||||
}
|
}
|
||||||
else if (rec.getSid() == ColumnInfoRecord.sid)
|
else if (rec.getSid() == ColumnInfoRecord.sid)
|
||||||
|
@ -455,68 +453,60 @@ public class Sheet implements Model
|
||||||
{
|
{
|
||||||
merged = ( MergeCellsRecord ) createMergedCells();
|
merged = ( MergeCellsRecord ) createMergedCells();
|
||||||
mergedRecords.add(merged);
|
mergedRecords.add(merged);
|
||||||
mergedLocs.add(new Integer(records.size() - 1));
|
|
||||||
records.add(records.size() - 1, merged);
|
records.add(records.size() - 1, merged);
|
||||||
}
|
}
|
||||||
merged.addArea(rowFrom, colFrom, rowTo, colTo);
|
merged.addArea(rowFrom, colFrom, rowTo, colTo);
|
||||||
return numMergedRegions++;
|
return numMergedRegions++;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void removeMergedRegion(int index)
|
public void removeMergedRegion(int index)
|
||||||
{
|
{
|
||||||
//safety checks
|
//safety checks
|
||||||
if (index >= numMergedRegions || mergedRecords.size() == 0)
|
if (index >= numMergedRegions || mergedRecords.size() == 0)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
int pos = 0;
|
int pos = 0;
|
||||||
int startNumRegions = 0;
|
int startNumRegions = 0;
|
||||||
|
|
||||||
//optimisation for current record
|
//optimisation for current record
|
||||||
if (numMergedRegions - index < merged.getNumAreas())
|
if (numMergedRegions - index < merged.getNumAreas())
|
||||||
{
|
{
|
||||||
pos = mergedRecords.size() - 1;
|
pos = mergedRecords.size() - 1;
|
||||||
startNumRegions = numMergedRegions - merged.getNumAreas();
|
startNumRegions = numMergedRegions - merged.getNumAreas();
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
for (int n = 0; n < mergedRecords.size(); n++)
|
for (int n = 0; n < mergedRecords.size(); n++)
|
||||||
{
|
{
|
||||||
MergeCellsRecord record = (MergeCellsRecord) mergedRecords.get(n);
|
MergeCellsRecord record = (MergeCellsRecord) mergedRecords.get(n);
|
||||||
if (startNumRegions + record.getNumAreas() > index)
|
if (startNumRegions + record.getNumAreas() > index)
|
||||||
{
|
{
|
||||||
pos = n;
|
pos = n;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
startNumRegions += record.getNumAreas();
|
startNumRegions += record.getNumAreas();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
MergeCellsRecord rec = (MergeCellsRecord) mergedRecords.get(pos);
|
MergeCellsRecord rec = (MergeCellsRecord) mergedRecords.get(pos);
|
||||||
rec.removeAreaAt(index - startNumRegions);
|
rec.removeAreaAt(index - startNumRegions);
|
||||||
numMergedRegions--;
|
numMergedRegions--;
|
||||||
if (rec.getNumAreas() == 0)
|
if (rec.getNumAreas() == 0)
|
||||||
{
|
{
|
||||||
mergedRecords.remove(pos);
|
mergedRecords.remove(pos);
|
||||||
if (merged == rec) {
|
//get rid of the record from the sheet
|
||||||
//pull up the LAST record for operations when we finally
|
records.remove(merged);
|
||||||
//support continue records for mergedRegions
|
if (merged == rec) {
|
||||||
if (mergedRecords.size() > 0) {
|
//pull up the LAST record for operations when we finally
|
||||||
merged = (MergeCellsRecord) mergedRecords.get(mergedRecords.size() - 1);
|
//support continue records for mergedRegions
|
||||||
} else {
|
if (mergedRecords.size() > 0) {
|
||||||
merged = null;
|
merged = (MergeCellsRecord) mergedRecords.get(mergedRecords.size() - 1);
|
||||||
}
|
} else {
|
||||||
}
|
merged = null;
|
||||||
|
}
|
||||||
int removePos = ((Integer) mergedLocs.get(pos)).intValue();
|
}
|
||||||
records.remove(removePos);
|
}
|
||||||
|
}
|
||||||
mergedLocs.remove(pos);
|
|
||||||
|
|
||||||
//if we're not tracking merged records, kill the pointer to reset the state
|
|
||||||
if (mergedRecords.size() == 0) merged = null;
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public MergeCellsRecord.MergedRegion getMergedRegionAt(int index)
|
public MergeCellsRecord.MergedRegion getMergedRegionAt(int index)
|
||||||
{
|
{
|
||||||
|
|
|
@ -12,6 +12,7 @@ import org.apache.poi.hssf.record.ColumnInfoRecord;
|
||||||
import org.apache.poi.hssf.record.EOFRecord;
|
import org.apache.poi.hssf.record.EOFRecord;
|
||||||
import org.apache.poi.hssf.record.FormulaRecord;
|
import org.apache.poi.hssf.record.FormulaRecord;
|
||||||
import org.apache.poi.hssf.record.LabelSSTRecord;
|
import org.apache.poi.hssf.record.LabelSSTRecord;
|
||||||
|
import org.apache.poi.hssf.record.MergeCellsRecord;
|
||||||
import org.apache.poi.hssf.record.RowRecord;
|
import org.apache.poi.hssf.record.RowRecord;
|
||||||
import org.apache.poi.hssf.record.SharedFormulaRecord;
|
import org.apache.poi.hssf.record.SharedFormulaRecord;
|
||||||
import org.apache.poi.hssf.record.StringRecord;
|
import org.apache.poi.hssf.record.StringRecord;
|
||||||
|
@ -80,6 +81,31 @@ public class SheetTest extends TestCase
|
||||||
assertTrue("Expected " + recordsRemoved + " record to be removed from the starting " + records + ". Currently there are " + sheet.getRecords().size() + " records", records - sheet.getRecords().size() == recordsRemoved);
|
assertTrue("Expected " + recordsRemoved + " record to be removed from the starting " + records + ". Currently there are " + sheet.getRecords().size() + " records", records - sheet.getRecords().size() == recordsRemoved);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Bug: 22922 (Reported by Xuemin Guan)
|
||||||
|
* <p>
|
||||||
|
* Remove mergedregion fails when a sheet loses records after an initial CreateSheet
|
||||||
|
* fills up the records.
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public void testMovingMergedRegion() {
|
||||||
|
List records = new ArrayList();
|
||||||
|
|
||||||
|
MergeCellsRecord merged = new MergeCellsRecord();
|
||||||
|
merged.addArea(0, (short)0, 1, (short)2);
|
||||||
|
records.add(new RowRecord());
|
||||||
|
records.add(new RowRecord());
|
||||||
|
records.add(new RowRecord());
|
||||||
|
records.add(merged);
|
||||||
|
|
||||||
|
Sheet sheet = Sheet.createSheet(records, 0);
|
||||||
|
sheet.records.remove(0);
|
||||||
|
|
||||||
|
//stub object to throw off list INDEX operations
|
||||||
|
sheet.removeMergedRegion(0);
|
||||||
|
assertEquals("Should be no more merged regions", 0, sheet.getNumMergedRegions());
|
||||||
|
}
|
||||||
|
|
||||||
public void testGetMergedRegionAt()
|
public void testGetMergedRegionAt()
|
||||||
{
|
{
|
||||||
//TODO
|
//TODO
|
||||||
|
@ -231,11 +257,6 @@ public class SheetTest extends TestCase
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void main(String [] args) {
|
|
||||||
System.out
|
|
||||||
.println("Testing : "+SheetTest.class.getName());
|
|
||||||
junit.textui.TestRunner.run(SheetTest.class);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue