From: Shawn Laubach Date: Sat, 21 Jan 2006 16:36:18 +0000 (+0000) Subject: Added the ability to set the default style for an individual column in a sheet. X-Git-Tag: REL_3_0_ALPHA3~198 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=315e9657d8687c3b1b9ab466573856bb58584d34;p=poi.git 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 --- diff --git a/src/java/org/apache/poi/hssf/model/Sheet.java b/src/java/org/apache/poi/hssf/model/Sheet.java index d173ec4350..1ee2bb8fd2 100644 --- a/src/java/org/apache/poi/hssf/model/Sheet.java +++ b/src/java/org/apache/poi/hssf/model/Sheet.java @@ -44,7 +44,7 @@ import java.util.List; // normally I don't do this, buy we literally mean ALL *

* @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) diff --git a/src/java/org/apache/poi/hssf/record/aggregates/ColumnInfoRecordsAggregate.java b/src/java/org/apache/poi/hssf/record/aggregates/ColumnInfoRecordsAggregate.java index 1486db41dc..517ea4a115 100644 --- a/src/java/org/apache/poi/hssf/record/aggregates/ColumnInfoRecordsAggregate.java +++ b/src/java/org/apache/poi/hssf/record/aggregates/ColumnInfoRecordsAggregate.java @@ -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 ci 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 ); } diff --git a/src/java/org/apache/poi/hssf/usermodel/HSSFSheet.java b/src/java/org/apache/poi/hssf/usermodel/HSSFSheet.java index 7cf9ac6ede..fa207870f1 100644 --- a/src/java/org/apache/poi/hssf/usermodel/HSSFSheet.java +++ b/src/java/org/apache/poi/hssf/usermodel/HSSFSheet.java @@ -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); + } } diff --git a/src/testcases/org/apache/poi/hssf/usermodel/TestHSSFSheet.java b/src/testcases/org/apache/poi/hssf/usermodel/TestHSSFSheet.java index e21bacdfc3..346483a35f 100644 --- a/src/testcases/org/apache/poi/hssf/usermodel/TestHSSFSheet.java +++ b/src/testcases/org/apache/poi/hssf/usermodel/TestHSSFSheet.java @@ -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) {