FileDocCategorySizeDatePackage
MergeCellsRecord.javaAPI DocApache Poi 3.0.18846Mon Jan 01 12:39:40 GMT 2007org.apache.poi.hssf.record

MergeCellsRecord

public class MergeCellsRecord extends Record
Title: Merged Cells Record
Description: Optional record defining a square area of cells to "merged" into one cell.
REFERENCE: NONE (UNDOCUMENTED PRESENTLY)
author
Andrew C. Oliver (acoliver at apache dot org)
version
2.0-pre

Fields Summary
public static final short
sid
private ArrayList
field_2_regions
Constructors Summary
public MergeCellsRecord()


     
    
    
public MergeCellsRecord(RecordInputStream in)
Constructs a MergedCellsRecord and sets its fields appropriately

param
sid id must be 0xe5 or an exception will be throw upon validation
param
size the size of the data area of the record
param
data data of the record (should not contain sid/len)

        super(in);
    
Methods Summary
public intaddArea(int rowfrom, short colfrom, int rowto, short colto)
Add an area to consider a merged cell. The index returned is only gauranteed to be correct provided you do not add ahead of or remove ahead of it (in which case you should increment or decrement appropriately....in other words its an arrayList)

param
rowfrom - the upper left hand corner's row
param
colfrom - the upper left hand corner's col
param
rowto - the lower right hand corner's row
param
colto - the lower right hand corner's col
return
new index of said area (don't depend on it if you add/remove)

        if (field_2_regions == null)
        {
            field_2_regions = new ArrayList(10);
        }
        MergedRegion region = new MergedRegion(rowfrom, rowto, colfrom,
                                               colto);

        field_2_regions.add(region);
        return field_2_regions.size() - 1;
    
public java.lang.Objectclone()

        MergeCellsRecord rec = new MergeCellsRecord();        
        rec.field_2_regions = new ArrayList();
        Iterator iterator = field_2_regions.iterator();
        while (iterator.hasNext()) {
           MergedRegion oldRegion = (MergedRegion)iterator.next();
           rec.addArea(oldRegion.row_from, oldRegion.col_from, oldRegion.row_to, oldRegion.col_to);
        }
        
        return rec;
    
protected voidfillFields(org.apache.poi.hssf.record.RecordInputStream in)

        short numAreas    = in.readShort();
        field_2_regions   = new ArrayList(numAreas + 10);

        for (int k = 0; k < numAreas; k++)
        {
            MergedRegion region =
                new MergedRegion(in.readShort(), in.readShort(),
                                 in.readShort(), in.readShort());

            field_2_regions.add(region);
        }
    
public org.apache.poi.hssf.record.MergeCellsRecord$MergedRegiongetAreaAt(int index)
return the MergedRegion at the given index.

return
MergedRegion representing the area that is Merged (r1,c1 - r2,c2)

        return ( MergedRegion ) field_2_regions.get(index);
    
public shortgetNumAreas()
get the number of merged areas. If this drops down to 0 you should just go ahead and delete the record.

return
number of areas

        //if the array size is larger than a short (65536), the record can't hold that many merges anyway
        if (field_2_regions == null) return 0;
        return (short)field_2_regions.size();
    
public intgetRecordSize()

        int retValue;

        retValue = 6 + (8 * field_2_regions.size());
        return retValue;
    
public shortgetSid()

        return sid;
    
public voidremoveAreaAt(int area)
essentially unmerge the cells in the "area" stored at the passed in index

param
area index

        field_2_regions.remove(area);
    
public intserialize(int offset, byte[] data)

        int recordsize = getRecordSize();
        int pos        = 6;

        LittleEndian.putShort(data, offset + 0, sid);
        LittleEndian.putShort(data, offset + 2, ( short ) (recordsize - 4));
        LittleEndian.putShort(data, offset + 4, getNumAreas());
        for (int k = 0; k < getNumAreas(); k++)
        {
            MergedRegion region = getAreaAt(k);

            //LittleEndian.putShort(data, offset + pos, region.row_from);
            LittleEndian.putShort(data, offset + pos, ( short ) region.row_from);
            pos += 2;
            //LittleEndian.putShort(data, offset + pos, region.row_to);
            LittleEndian.putShort(data, offset + pos, ( short ) region.row_to);
            pos += 2;
            LittleEndian.putShort(data, offset + pos, region.col_from);
            pos += 2;
            LittleEndian.putShort(data, offset + pos, region.col_to);
            pos += 2;
        }
        return recordsize;
    
public voidsetNumAreas(short numareas)
set the number of merged areas. You do not need to call this if you use addArea, it will be incremented automatically or decremented when an area is removed. If you are setting this to 0 then you are a terrible person. Just remove the record. (just kidding about you being a terrible person..hehe)

deprecated
We now link the size to the actual array of merged regions
see
#getNumAreas()
param
numareas number of areas

        
    
public java.lang.StringtoString()

        StringBuffer retval = new StringBuffer();

        retval.append("[MERGEDCELLS]").append("\n");
        retval.append("     .sid        =").append(sid).append("\n");
        retval.append("     .numregions =").append(getNumAreas())
            .append("\n");
        for (int k = 0; k < getNumAreas(); k++)
        {
            MergedRegion region = ( MergedRegion ) field_2_regions.get(k);

            retval.append("     .rowfrom    =").append(region.row_from)
                .append("\n");
            retval.append("     .colfrom    =").append(region.col_from)
                .append("\n");
            retval.append("     .rowto      =").append(region.row_to)
                .append("\n");
            retval.append("     .colto      =").append(region.col_to)
                .append("\n");
        }
        retval.append("[MERGEDCELLS]").append("\n");
        return retval.toString();
    
protected voidvalidateSid(short id)

        if (id != sid)
        {
            throw new RecordFormatException("NOT A MERGEDCELLS RECORD!! "
                                            + id);
        }