ASF Bugzilla – Attachment 38461 Details for
Bug 48344
setRowStyle() method of HSSFRow is not working
Home
|
New
|
Browse
|
Search
|
[?]
|
Reports
|
Help
|
New Account
|
Log In
Remember
[x]
|
Forgot Password
Login:
[x]
[patch]
set cell style when cell is being created
patch.txt (text/plain), 16.25 KB, created by
lsq27
on 2023-01-11 15:53:37 UTC
(
hide
)
Description:
set cell style when cell is being created
Filename:
MIME Type:
Creator:
lsq27
Created:
2023-01-11 15:53:37 UTC
Size:
16.25 KB
patch
obsolete
>Index: poi/src/main/java/org/apache/poi/hssf/usermodel/HSSFCell.java >=================================================================== >--- poi/src/main/java/org/apache/poi/hssf/usermodel/HSSFCell.java (revision 1906599) >+++ poi/src/main/java/org/apache/poi/hssf/usermodel/HSSFCell.java (working copy) >@@ -158,13 +158,20 @@ > CellType type) > { > checkBounds(col); >- _cellType = CellType._NONE; // Force 'setCellType' to create a first Record >- _stringValue = null; >- _book = book; >- _sheet = sheet; >+ _cellType = CellType._NONE; // Force 'setCellType' to create a first Record >+ _stringValue = null; >+ _book = book; >+ _sheet = sheet; > >- short xfindex = sheet.getSheet().getXFIndexForColAt(col); >- setCellType(type,false,row,col,xfindex); >+ short xfindex; >+ HSSFRow _row = _sheet.getRow(row); >+ if (_row.isFormatted()) { >+ xfindex = _row.getRowStyle().getIndex(); >+ } else { >+ xfindex = sheet.getSheet().getXFIndexForColAt(col); >+ } >+ >+ setCellType(type, false, row, col, xfindex); > } > > /** >Index: poi/src/main/java/org/apache/poi/hssf/usermodel/HSSFRow.java >=================================================================== >--- poi/src/main/java/org/apache/poi/hssf/usermodel/HSSFRow.java (revision 1906599) >+++ poi/src/main/java/org/apache/poi/hssf/usermodel/HSSFRow.java (working copy) >@@ -35,6 +35,7 @@ > import org.apache.poi.ss.usermodel.CellType; > import org.apache.poi.ss.usermodel.Row; > import org.apache.poi.ss.usermodel.helpers.RowShifter; >+import org.apache.poi.ss.usermodel.Sheet; > import org.apache.poi.ss.util.CellRangeAddress; > import org.apache.poi.ss.util.CellUtil; > import org.apache.poi.util.Beta; >@@ -601,19 +602,31 @@ > ExtendedFormatRecord xf = book.getWorkbook().getExFormatAt(styleIndex); > return new HSSFCellStyle(styleIndex, xf, book); > } >+ > /** > * Applies a whole-row cell styling to the row. >+ * POI will only apply this style to new cells added to the sheet. >+ * Row style is prior to column style. >+ * >+ * @param style the style to set >+ * @see Sheet#setDefaultColumnStyle(int, CellStyle) > */ > public void setRowStyle(HSSFCellStyle style) { >- row.setFormatted(true); >- row.setXFIndex(style.getIndex()); >+ if (style == null) { >+ row.setFormatted(false); >+ row.setXFIndex((short) 0xf); >+ } else { >+ row.setFormatted(true); >+ row.setXFIndex(style.getIndex()); >+ } > } >+ > /** > * Applies a whole-row cell styling to the row. > */ > @Override > public void setRowStyle(CellStyle style) { >- setRowStyle((HSSFCellStyle)style); >+ setRowStyle((HSSFCellStyle) style); > } > > /** >Index: poi/src/main/java/org/apache/poi/ss/usermodel/Row.java >=================================================================== >--- poi/src/main/java/org/apache/poi/ss/usermodel/Row.java (revision 1906599) >+++ poi/src/main/java/org/apache/poi/ss/usermodel/Row.java (working copy) >@@ -204,6 +204,11 @@ > > /** > * Applies a whole-row cell styling to the row. >+ * POI will only apply this style to new cells added to the sheet. >+ * Row style is prior to column style. >+ * >+ * @param style the style to set >+ * @see Sheet#setDefaultColumnStyle(int, CellStyle) > */ > void setRowStyle(CellStyle style); > >Index: poi/src/main/java/org/apache/poi/ss/usermodel/Sheet.java >=================================================================== >--- poi/src/main/java/org/apache/poi/ss/usermodel/Sheet.java (revision 1906599) >+++ poi/src/main/java/org/apache/poi/ss/usermodel/Sheet.java (working copy) >@@ -982,9 +982,11 @@ > > /** > * Sets the default column style for a given column. POI will only apply this style to new cells added to the sheet. >+ * Row style is prior to column style. > * > * @param column the column index >- * @param style the style to set >+ * @param style the style to set >+ * @see Sheet#setDefaultColumnStyle(int, CellStyle) > */ > void setDefaultColumnStyle(int column, CellStyle style); > >Index: poi/src/test/java/org/apache/poi/ss/usermodel/BaseTestRow.java >=================================================================== >--- poi/src/test/java/org/apache/poi/ss/usermodel/BaseTestRow.java (revision 1906599) >+++ poi/src/test/java/org/apache/poi/ss/usermodel/BaseTestRow.java (working copy) >@@ -460,45 +460,60 @@ > > @Test > void testRowStyle() throws IOException { >- Workbook wb1 = _testDataProvider.createWorkbook(); >- Sheet sheet = wb1.createSheet("test"); >- Row row1 = sheet.createRow(0); >- Row row2 = sheet.createRow(1); >+ Workbook wb1 = _testDataProvider.createWorkbook(); >+ Sheet sheet = wb1.createSheet("test"); >+ Row row1 = sheet.createRow(0); >+ Row row2 = sheet.createRow(1); > >- // Won't be styled currently >+ // Won't be styled currently > assertFalse(row1.isFormatted()); > assertFalse(row2.isFormatted()); > assertNull(row1.getRowStyle()); > assertNull(row2.getRowStyle()); > >- // Style one >- CellStyle style = wb1.createCellStyle(); >- style.setDataFormat((short)4); >- row2.setRowStyle(style); >+ // Style one >+ CellStyle style = wb1.createCellStyle(); >+ style.setDataFormat((short) 4); >+ row2.setRowStyle(style); > >- // Check >+ // Check > assertFalse(row1.isFormatted()); > assertTrue(row2.isFormatted()); > assertNull(row1.getRowStyle()); >- assertEquals(style, row2.getRowStyle()); >+ assertEquals(style, row2.getRowStyle()); > >- // Save, load and re-check >- Workbook wb2 = _testDataProvider.writeOutAndReadBack(wb1); >- wb1.close(); >+ // Row style is applied to cell when cell is created >+ Cell cell21 = row2.createCell(0); >+ assertEquals(row2.getRowStyle(), cell21.getCellStyle()); >+ row2.setRowStyle(null); >+ Cell cell22 = row2.createCell(0); >+ assertNotEquals(cell21.getCellStyle(), cell22.getCellStyle()); >+ row2.setRowStyle(style); > >- sheet = wb2.getSheetAt(0); >+ // Row style is prior to column style >+ CellStyle columnStyle = wb1.createCellStyle(); >+ sheet.setDefaultColumnStyle(0, columnStyle); >+ Cell cell23 = row2.createCell(0); >+ assertEquals(row2.getRowStyle(), cell23.getCellStyle()); >+ assertNotEquals(columnStyle, cell23.getCellStyle()); > >- row1 = sheet.getRow(0); >- row2 = sheet.getRow(1); >- style = wb2.getCellStyleAt(style.getIndex()); >+ // Save, load and re-check >+ Workbook wb2 = _testDataProvider.writeOutAndReadBack(wb1); >+ wb1.close(); > >+ sheet = wb2.getSheetAt(0); >+ >+ row1 = sheet.getRow(0); >+ row2 = sheet.getRow(1); >+ style = wb2.getCellStyleAt(style.getIndex()); >+ > assertFalse(row1.isFormatted()); > assertTrue(row2.isFormatted()); > assertNull(row1.getRowStyle()); >- assertEquals(style, row2.getRowStyle()); >- assertEquals(4, style.getDataFormat()); >+ assertEquals(style, row2.getRowStyle()); >+ assertEquals(4, style.getDataFormat()); > >- wb2.close(); >+ wb2.close(); > } > > @Test >Index: poi/src/test/java/org/apache/poi/ss/usermodel/BaseTestSheet.java >=================================================================== >--- poi/src/test/java/org/apache/poi/ss/usermodel/BaseTestSheet.java (revision 1906599) >+++ poi/src/test/java/org/apache/poi/ss/usermodel/BaseTestSheet.java (working copy) >@@ -701,8 +701,7 @@ > Cell cell = r0.createCell(0); > CellStyle style2 = cell.getCellStyle(); > assertNotNull(style2); >- //current implementations mean that cells inherit column style but not row style >- assertNotEquals(style.getIndex(), style2.getIndex(), "style should not match"); >+ assertEquals(style.getIndex(), style2.getIndex(), "style should match"); > > try (Workbook wb2 = _testDataProvider.writeOutAndReadBack(wb)) { > Sheet wb2Sheet = wb2.getSheetAt(0); >Index: poi-ooxml/src/main/java/org/apache/poi/xssf/streaming/SXSSFCell.java >=================================================================== >--- poi-ooxml/src/main/java/org/apache/poi/xssf/streaming/SXSSFCell.java (revision 1906599) >+++ poi-ooxml/src/main/java/org/apache/poi/xssf/streaming/SXSSFCell.java (working copy) >@@ -71,6 +71,15 @@ > { > this(row, cellType); > _columnIndex = columnIndex; >+ // determine cell style >+ // Row style and column style are checked in order. >+ // If no style exists, fallback to the default style. >+ if (_style == null) { >+ _style = getDefaultCellStyleFromRow(); >+ } >+ if (_style == null) { >+ _style = getDefaultCellStyleFromColumn(); >+ } > } > > /** >@@ -592,16 +601,7 @@ > * @see org.apache.poi.ss.usermodel.Workbook#getCellStyleAt(int) > */ > @Override >- public CellStyle getCellStyle() >- { >- if (_style == null) { >- CellStyle style = getDefaultCellStyleFromColumn(); >- if (style == null) { >- SXSSFWorkbook wb = getSheet().getWorkbook(); >- style = wb.getCellStyleAt(0); >- } >- _style = style; >- } >+ public CellStyle getCellStyle() { > return _style; > } > >@@ -614,12 +614,20 @@ > return style; > } > >+ private CellStyle getDefaultCellStyleFromRow() { >+ CellStyle style = null; >+ Row row = getRow(); >+ if (row != null) { >+ style = row.getRowStyle(); >+ } >+ return style; >+ } >+ > /** > * {@inheritDoc} > */ > @Override >- public void setAsActiveCell() >- { >+ public void setAsActiveCell() { > getSheet().setActiveCell(getAddress()); > } > >Index: poi-ooxml/src/main/java/org/apache/poi/xssf/streaming/SheetDataWriter.java >=================================================================== >--- poi-ooxml/src/main/java/org/apache/poi/xssf/streaming/SheetDataWriter.java (revision 1906599) >+++ poi-ooxml/src/main/java/org/apache/poi/xssf/streaming/SheetDataWriter.java (working copy) >@@ -257,12 +257,10 @@ > _out.write("<c"); > writeAttribute("r", ref); > CellStyle cellStyle = cell.getCellStyle(); >- if (cellStyle.getIndex() != 0) { >- // need to convert the short to unsigned short as the indexes can be up to 64k >- // ideally we would use int for this index, but that would need changes to some more >- // APIs >- writeAttribute("s", Integer.toString(cellStyle.getIndex() & 0xffff)); >- } >+ // need to convert the short to unsigned short as the indexes can be up to 64k >+ // ideally we would use int for this index, but that would need changes to some more >+ // APIs >+ writeAttribute("s", Integer.toString(cellStyle.getIndex() & 0xffff)); > CellType cellType = cell.getCellType(); > switch (cellType) { > case BLANK: { >@@ -270,7 +268,7 @@ > break; > } > case FORMULA: { >- switch(cell.getCachedFormulaResultType()) { >+ switch (cell.getCachedFormulaResultType()) { > case NUMERIC: > writeAttribute("t", "n"); > break; >Index: poi-ooxml/src/main/java/org/apache/poi/xssf/usermodel/XSSFCell.java >=================================================================== >--- poi-ooxml/src/main/java/org/apache/poi/xssf/usermodel/XSSFCell.java (revision 1906599) >+++ poi-ooxml/src/main/java/org/apache/poi/xssf/usermodel/XSSFCell.java (working copy) >@@ -93,6 +93,10 @@ > private final XSSFRow _row; > > /** >+ * cell style >+ */ >+ private XSSFCellStyle _style; >+ /** > * 0-based column index > */ > private int _cellNum; >@@ -122,12 +126,23 @@ > _cellNum = new CellReference(rval).getCol(); > } else { > int prevNum = row.getLastCellNum(); >- if(prevNum != -1){ >- _cellNum = row.getCell(prevNum-1, MissingCellPolicy.RETURN_NULL_AND_BLANK).getColumnIndex() + 1; >+ if (prevNum != -1) { >+ _cellNum = row.getCell(prevNum - 1, MissingCellPolicy.RETURN_NULL_AND_BLANK).getColumnIndex() + 1; > } > } > _sharedStringSource = row.getSheet().getWorkbook().getSharedStringSource(); > _stylesSource = row.getSheet().getWorkbook().getStylesSource(); >+ >+ // determine cell style. >+ // Cell style, row style and column style are checked in order. >+ // If no style exists, fallback to the default style. >+ _style = getExplicitCellStyle(); >+ if (_style == null) { >+ _style = getDefaultCellStyleFromRow(); >+ } >+ if (_style == null) { >+ _style = getDefaultCellStyleFromColumn(); >+ } > } > > @Override >@@ -561,20 +576,13 @@ > } > > /** >- * Return the cell's style. Since POI v5.2.3, this returns the column style if the >- * cell has no style of its own. If no column default style is set, the row default style is checked. >- * This method has always fallen back to return the default style >- * if there is no other style to return. >+ * Return the cell's style. > * > * @return the cell's style. > */ > @Override > public XSSFCellStyle getCellStyle() { >- XSSFCellStyle style = getExplicitCellStyle(); >- if (style == null) { >- style = getDefaultCellStyleFromColumn(); >- } >- return style; >+ return _style; > } > > private XSSFCellStyle getExplicitCellStyle() { >@@ -597,17 +605,13 @@ > return style; > } > >- protected void applyDefaultCellStyleIfNecessary() { >- XSSFCellStyle style = getExplicitCellStyle(); >- if (style == null) { >- XSSFSheet sheet = getSheet(); >- if (sheet != null) { >- XSSFCellStyle defaultStyle = getDefaultCellStyleFromColumn(); >- if (defaultStyle != null) { >- setCellStyle(defaultStyle); >- } >- } >+ private XSSFCellStyle getDefaultCellStyleFromRow() { >+ XSSFCellStyle style = null; >+ XSSFRow row = getRow(); >+ if (row != null) { >+ style = row.getRowStyle(); > } >+ return style; > } > > /** >Index: poi-ooxml/src/main/java/org/apache/poi/xssf/usermodel/XSSFRow.java >=================================================================== >--- poi-ooxml/src/main/java/org/apache/poi/xssf/usermodel/XSSFRow.java (revision 1906599) >+++ poi-ooxml/src/main/java/org/apache/poi/xssf/usermodel/XSSFRow.java (working copy) >@@ -35,6 +35,7 @@ > import org.apache.poi.ss.usermodel.FormulaError; > import org.apache.poi.ss.usermodel.Row; > import org.apache.poi.ss.usermodel.helpers.RowShifter; >+import org.apache.poi.ss.usermodel.Sheet; > import org.apache.poi.ss.util.CellRangeAddress; > import org.apache.poi.ss.util.CellUtil; > import org.apache.poi.util.Beta; >@@ -476,8 +477,11 @@ > > /** > * Applies a whole-row cell styling to the row. >- * If the value is null then the style information is removed, >- * causing the cell to used the default workbook style. >+ * POI will only apply this style to new cells added to the sheet. >+ * Row style is prior to column style. >+ * >+ * @param style the style to set >+ * @see Sheet#setDefaultColumnStyle(int, CellStyle) > */ > @Override > public void setRowStyle(CellStyle style) { >@@ -562,7 +566,6 @@ > Iterator<XSSFCell> it = _cells.values().iterator(); > for (CTCell ctCell : cArrayOrig) { > XSSFCell cell = it.next(); >- cell.applyDefaultCellStyleIfNecessary(); > > // we want to compare on identity here on purpose > // as we want to ensure that both lists contain the
You cannot view the attachment while viewing its details because your browser does not support IFRAMEs.
View the attachment on a separate page
.
View Attachment As Diff
View Attachment As Raw
Actions:
View
|
Diff
Attachments on
bug 48344
:
27627
|
27628
|
27633
|
27634
|
38460
| 38461