Added the ability to set the default style for an individual column in a sheet.

git-svn-id: https://svn.apache.org/repos/asf/jakarta/poi/trunk@371080 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Shawn Laubach 2006-01-21 16:36:18 +00:00
parent 33484b31f9
commit 315e9657d8
4 changed files with 44 additions and 13 deletions

View File

@ -44,7 +44,7 @@ import java.util.List; // normally I don't do this, buy we literally mean ALL
* <P>
* @author Andrew C. Oliver (acoliver at apache dot org)
* @author Glen Stampoultzis (glens at apache.org)
* @author Shawn Laubach (slaubach at apache dot org) Gridlines, Headers, Footers, and PrintSetup
* @author Shawn Laubach (slaubach at apache dot org) Gridlines, Headers, Footers, PrintSetup, and Setting Default Column Styles
* @author Jason Height (jheight at chariot dot net dot au) Clone support. DBCell & Index Record writing support
* @author Brian Sanders (kestrel at burdell dot org) Active Cell support
*
@ -1848,9 +1848,18 @@ public class Sheet implements Model
if (columns == null)
columns = new ColumnInfoRecordsAggregate();
columns.setColumn( column, width, level, hidden, collapsed );
columns.setColumn( column, null, width, level, hidden, collapsed );
}
public void setColumn(short column, Short xfStyle, Short width, Integer level, Boolean hidden, Boolean collapsed)
{
if (columns == null)
columns = new ColumnInfoRecordsAggregate();
columns.setColumn( column, xfStyle, width, level, hidden, collapsed );
}
/**
* Creates an outline group for the specified columns.
* @param fromColumn group from this column (inclusive)

View File

@ -269,7 +269,7 @@ public class ColumnInfoRecordsAggregate
columnInfo = writeHidden( columnInfo, idx, true );
// Write collapse field
setColumn( (short) ( columnInfo.getLastColumn() + 1 ), null, null, null, Boolean.TRUE);
setColumn( (short) ( columnInfo.getLastColumn() + 1 ), null, null, null, null, Boolean.TRUE);
}
public void expandColumn( short columnNumber )
@ -307,7 +307,7 @@ public class ColumnInfoRecordsAggregate
}
// Write collapse field
setColumn( (short) ( columnInfo.getLastColumn() + 1 ), null, null, null, Boolean.FALSE);
setColumn( (short) ( columnInfo.getLastColumn() + 1 ), null, null, null, null, Boolean.FALSE);
}
/**
@ -327,7 +327,7 @@ public class ColumnInfoRecordsAggregate
}
public void setColumn(short column, Short width, Integer level, Boolean hidden, Boolean collapsed)
public void setColumn(short column, Short xfIndex, Short width, Integer level, Boolean hidden, Boolean collapsed)
{
ColumnInfoRecord ci = null;
int k = 0;
@ -345,11 +345,12 @@ public class ColumnInfoRecordsAggregate
if (ci != null)
{
boolean styleChanged = xfIndex != null && ci.getXFIndex() != xfIndex.shortValue();
boolean widthChanged = width != null && ci.getColumnWidth() != width.shortValue();
boolean levelChanged = level != null && ci.getOutlineLevel() != level.intValue();
boolean hiddenChanged = hidden != null && ci.getHidden() != hidden.booleanValue();
boolean collapsedChanged = collapsed != null && ci.getCollapsed() != collapsed.booleanValue();
boolean columnChanged = widthChanged || levelChanged || hiddenChanged || collapsedChanged;
boolean columnChanged = styleChanged || widthChanged || levelChanged || hiddenChanged || collapsedChanged;
if (!columnChanged)
{
// do nothing...nothing changed.
@ -357,7 +358,7 @@ public class ColumnInfoRecordsAggregate
else if ((ci.getFirstColumn() == column)
&& (ci.getLastColumn() == column))
{ // if its only for this cell then
setColumnInfoFields( ci, width, level, hidden, collapsed );
setColumnInfoFields( ci, xfIndex, width, level, hidden, collapsed );
}
else if ((ci.getFirstColumn() == column)
|| (ci.getLastColumn() == column))
@ -378,7 +379,7 @@ public class ColumnInfoRecordsAggregate
nci.setLastColumn(column);
nci.setOptions(ci.getOptions());
nci.setXFIndex(ci.getXFIndex());
setColumnInfoFields( nci, width, level, hidden, collapsed );
setColumnInfoFields( nci, xfIndex, width, level, hidden, collapsed );
insertColumn(k, nci);
}
@ -393,7 +394,7 @@ public class ColumnInfoRecordsAggregate
nci.setLastColumn(column);
nci.setOptions(ci.getOptions());
nci.setXFIndex(ci.getXFIndex());
setColumnInfoFields( nci, width, level, hidden, collapsed );
setColumnInfoFields( nci, xfIndex, width, level, hidden, collapsed );
insertColumn(++k, nci);
nci = ( ColumnInfoRecord ) createColInfo();
@ -413,7 +414,7 @@ public class ColumnInfoRecordsAggregate
nci.setFirstColumn(column);
nci.setLastColumn(column);
setColumnInfoFields( nci, width, level, hidden, collapsed );
setColumnInfoFields( nci, xfIndex, width, level, hidden, collapsed );
insertColumn(k, nci);
}
}
@ -421,8 +422,10 @@ public class ColumnInfoRecordsAggregate
/**
* Sets all non null fields into the <code>ci</code> parameter.
*/
private void setColumnInfoFields( ColumnInfoRecord ci, Short width, Integer level, Boolean hidden, Boolean collapsed )
private void setColumnInfoFields( ColumnInfoRecord ci, Short xfStyle, Short width, Integer level, Boolean hidden, Boolean collapsed )
{
if (xfStyle != null)
ci.setXFIndex(xfStyle.shortValue());
if (width != null)
ci.setColumnWidth(width.shortValue());
if (level != null)
@ -500,7 +503,7 @@ public class ColumnInfoRecordsAggregate
level = Math.min(7, level);
fromIdx = columnIdx - 1; // subtract 1 just in case this column is collapsed later.
}
setColumn((short)i, null, new Integer(level), null, null);
setColumn((short)i, null, null, new Integer(level), null, null);
columnIdx = findColumnIdx( i, Math.max(0, fromIdx ) );
collapseColInfoRecords( columnIdx );
}

View File

@ -1331,5 +1331,13 @@ public class HSSFSheet
sheet.setRowGroupCollapsed( row, collapse );
}
/**
* Sets the default column style for a given column. POI will only apply this style to new cells added to the sheet.
*
* @param fromCol the starting column index
* @param column the column index
*/
public void setDefaultColumnStyle(short column, HSSFCellStyle style) {
sheet.setColumn(column, new Short(style.getIndex()), null, null, null, null);
}
}

View File

@ -469,6 +469,17 @@ public class TestHSSFSheet
assertNotNull(r.getCell((short)1));
assertEquals("formula", r.getCell((short)1).getCellFormula(), "A1*2");
}
/** test that new default column styles get applied */
public void testDefaultColumnStyle() {
HSSFWorkbook wb = new HSSFWorkbook();
HSSFCellStyle style = wb.createCellStyle();
HSSFSheet s = wb.createSheet();
s.setDefaultColumnStyle((short)0, style);
HSSFRow r = s.createRow(0);
HSSFCell c = r.createCell((short)0);
assertEquals("style should match", style.getIndex(), c.getCellStyle().getIndex());
}
public static void main(java.lang.String[] args) {