You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@poi.apache.org by ye...@apache.org on 2011/03/11 18:33:36 UTC
svn commit: r1080689 - in /poi/trunk/src:
documentation/content/xdocs/status.xml
java/org/apache/poi/hssf/usermodel/HSSFCell.java
java/org/apache/poi/hssf/usermodel/HSSFCellStyle.java
testcases/org/apache/poi/hssf/usermodel/TestCellStyle.java
Author: yegor
Date: Fri Mar 11 17:33:36 2011
New Revision: 1080689
URL: http://svn.apache.org/viewvc?rev=1080689&view=rev
Log:
fixed setting named styles to HSSFCells. see Bugzilla 50912
Modified:
poi/trunk/src/documentation/content/xdocs/status.xml
poi/trunk/src/java/org/apache/poi/hssf/usermodel/HSSFCell.java
poi/trunk/src/java/org/apache/poi/hssf/usermodel/HSSFCellStyle.java
poi/trunk/src/testcases/org/apache/poi/hssf/usermodel/TestCellStyle.java
Modified: poi/trunk/src/documentation/content/xdocs/status.xml
URL: http://svn.apache.org/viewvc/poi/trunk/src/documentation/content/xdocs/status.xml?rev=1080689&r1=1080688&r2=1080689&view=diff
==============================================================================
--- poi/trunk/src/documentation/content/xdocs/status.xml (original)
+++ poi/trunk/src/documentation/content/xdocs/status.xml Fri Mar 11 17:33:36 2011
@@ -34,6 +34,7 @@
<changes>
<release version="3.8-beta2" date="2011-??-??">
+ <action dev="poi-developers" type="fix">50912 - fixed setting named styles to HSSFCells</action>
<action dev="poi-developers" type="fix">50779 - fixed RecordFormatException when reading unicode strings with photenic data</action>
<action dev="poi-developers" type="fix">50718 - More helpful error message when you try to create a CellReference with #REF!</action>
<action dev="poi-developers" type="fix">50784 - XSSFColors return by XSSFFont now have theme information applied to them</action>
Modified: poi/trunk/src/java/org/apache/poi/hssf/usermodel/HSSFCell.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/java/org/apache/poi/hssf/usermodel/HSSFCell.java?rev=1080689&r1=1080688&r2=1080689&view=diff
==============================================================================
--- poi/trunk/src/java/org/apache/poi/hssf/usermodel/HSSFCell.java (original)
+++ poi/trunk/src/java/org/apache/poi/hssf/usermodel/HSSFCell.java Fri Mar 11 17:33:36 2011
@@ -907,8 +907,15 @@ public class HSSFCell implements Cell {
// Verify it really does belong to our workbook
style.verifyBelongsToWorkbook(_book);
+ short styleIndex;
+ if(style.getUserStyleName() != null) {
+ styleIndex = applyUserCellStyle(style);
+ } else {
+ styleIndex = style.getIndex();
+ }
+
// Change our cell record to use this style
- _record.setXFIndex(style.getIndex());
+ _record.setXFIndex(styleIndex);
}
/**
@@ -1245,4 +1252,49 @@ public class HSSFCell implements Cell {
notifyArrayFormulaChanging(msg);
}
+ /**
+ * Applying a user-defined style (UDS) is special. Excel does not directly reference user-defined styles, but
+ * instead create a 'proxy' ExtendedFormatRecord referencing the UDS as parent.
+ *
+ * The proceudre to apply a UDS is as follows:
+ *
+ * 1. search for a ExtendedFormatRecord with parentIndex == style.getIndex()
+ * and xfType == ExtendedFormatRecord.XF_CELL.
+ * 2. if not found then create a new ExtendedFormatRecord and copy all attributes from the user-defined style
+ * and set the parentIndex to be style.getIndex()
+ * 3. return the index of the ExtendedFormatRecord, this will be assigned to the parent cell record
+ *
+ * @param style the user style to apply
+ *
+ * @return the index of a ExtendedFormatRecord record that will be referenced by the cell
+ */
+ private short applyUserCellStyle(HSSFCellStyle style){
+ if(style.getUserStyleName() == null) {
+ throw new IllegalArgumentException("Expected user-defined style");
+ }
+
+ InternalWorkbook iwb = _book.getWorkbook();
+ short userXf = -1;
+ int numfmt = iwb.getNumExFormats();
+ for(short i = 0; i < numfmt; i++){
+ ExtendedFormatRecord xf = iwb.getExFormatAt(i);
+ if(xf.getXFType() == ExtendedFormatRecord.XF_CELL && xf.getParentIndex() == style.getIndex() ){
+ userXf = i;
+ break;
+ }
+ }
+ short styleIndex;
+ if (userXf == -1){
+ ExtendedFormatRecord xfr = iwb.createCellXF();
+ xfr.cloneStyleFrom(iwb.getExFormatAt(style.getIndex()));
+ xfr.setIndentionOptions((short)0);
+ xfr.setXFType(ExtendedFormatRecord.XF_CELL);
+ xfr.setParentIndex(style.getIndex());
+ styleIndex = (short)numfmt;
+ } else {
+ styleIndex = userXf;
+ }
+
+ return styleIndex;
+ }
}
Modified: poi/trunk/src/java/org/apache/poi/hssf/usermodel/HSSFCellStyle.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/java/org/apache/poi/hssf/usermodel/HSSFCellStyle.java?rev=1080689&r1=1080688&r2=1080689&view=diff
==============================================================================
--- poi/trunk/src/java/org/apache/poi/hssf/usermodel/HSSFCellStyle.java (original)
+++ poi/trunk/src/java/org/apache/poi/hssf/usermodel/HSSFCellStyle.java Fri Mar 11 17:33:36 2011
@@ -69,12 +69,14 @@ public final class HSSFCellStyle impleme
* cases there'll be a fully defined parent.
*/
public HSSFCellStyle getParentStyle() {
- if(_format.getParentIndex() == 0) {
+ short parentIndex = _format.getParentIndex();
+ // parentIndex equal 0xFFF indicates no inheritance from a cell style XF (See 2.4.353 XF)
+ if(parentIndex == 0 || parentIndex == 0xFFF) {
return null;
}
return new HSSFCellStyle(
- _format.getParentIndex(),
- _workbook.getExFormatAt(_format.getParentIndex()),
+ parentIndex,
+ _workbook.getExFormatAt(parentIndex),
_workbook
);
}
Modified: poi/trunk/src/testcases/org/apache/poi/hssf/usermodel/TestCellStyle.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/testcases/org/apache/poi/hssf/usermodel/TestCellStyle.java?rev=1080689&r1=1080688&r2=1080689&view=diff
==============================================================================
--- poi/trunk/src/testcases/org/apache/poi/hssf/usermodel/TestCellStyle.java (original)
+++ poi/trunk/src/testcases/org/apache/poi/hssf/usermodel/TestCellStyle.java Fri Mar 11 17:33:36 2011
@@ -17,17 +17,16 @@
package org.apache.poi.hssf.usermodel;
+import junit.framework.TestCase;
+import org.apache.poi.hssf.HSSFTestDataSamples;
+import org.apache.poi.util.TempFile;
+
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Calendar;
import java.util.Date;
-import junit.framework.TestCase;
-
-import org.apache.poi.hssf.HSSFTestDataSamples;
-import org.apache.poi.util.TempFile;
-
/**
* Class to test cell styling functionality
*
@@ -326,5 +325,11 @@ public final class TestCellStyle extends
assertEquals(null, cs3.getUserStyleName());
assertEquals("style1", cs2.getParentStyle().getUserStyleName());
assertEquals("style2", cs3.getParentStyle().getUserStyleName());
+
+ // now apply a named style to a new cell
+ HSSFCell c4 = s.getRow(0).createCell(1);
+ c4.setCellStyle(cs2);
+ assertEquals("style1", c4.getCellStyle().getParentStyle().getUserStyleName());
}
+
}
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@poi.apache.org
For additional commands, e-mail: commits-help@poi.apache.org