numxfs++;
return xf;
}
+
+ /**
+ * Returns the StyleRecord for the given
+ * xfIndex, or null if that ExtendedFormat doesn't
+ * have a Style set.
+ */
+ public StyleRecord getStyleRecord(int xfIndex) {
+ // Style records always follow after
+ // the ExtendedFormat records
+ boolean done = false;
+ for(int i=records.getXfpos(); i<records.size() &&
+ !done; i++) {
+ Record r = records.get(i);
+ if(r instanceof ExtendedFormatRecord) {
+ } else if(r instanceof StyleRecord) {
+ StyleRecord sr = (StyleRecord)r;
+ if(sr.getIndex() == xfIndex) {
+ return sr;
+ }
+ } else {
+ done = true;
+ }
+ }
+ return null;
+ }
+ /**
+ * Creates a new StyleRecord, for the given Extended
+ * Format index, and adds it onto the end of the
+ * records collection
+ */
+ public StyleRecord createStyleRecord(int xfIndex) {
+ // Style records always follow after
+ // the ExtendedFormat records
+ StyleRecord newSR = new StyleRecord();
+ newSR.setIndex((short)xfIndex);
+
+ // Find the spot
+ int addAt = -1;
+ for(int i=records.getXfpos(); i<records.size() &&
+ addAt == -1; i++) {
+ Record r = records.get(i);
+ if(r instanceof ExtendedFormatRecord ||
+ r instanceof StyleRecord) {
+ // Keep going
+ } else {
+ addAt = i;
+ }
+ }
+ if(addAt == -1) {
+ throw new IllegalStateException("No XF Records found!");
+ }
+ records.add(addAt, newSR);
+
+ return newSR;
+ }
/**
* Adds a string to the SST table and returns its index (if its a duplicate
public void setName(String name)
{
field_4_name = name;
- //TODO set name length and string options
+
+ // Fix up the length
+ field_2_name_length = (short)name.length();
+ //TODO set name string options
}
// end user defined
import org.apache.poi.hssf.model.Workbook;
import org.apache.poi.hssf.record.ExtendedFormatRecord;
import org.apache.poi.hssf.record.FontRecord;
+import org.apache.poi.hssf.record.StyleRecord;
import org.apache.poi.hssf.util.HSSFColor;
/**
{
return format.getFillForeground();
}
+
+ /**
+ * Gets the name of the user defined style.
+ * Returns null for built in styles, and
+ * styles where no name has been defined
+ */
+ public String getUserStyleName() {
+ StyleRecord sr = workbook.getStyleRecord(index);
+ if(sr == null) {
+ return null;
+ }
+ if(sr.getType() == StyleRecord.STYLE_BUILT_IN) {
+ return null;
+ }
+ return sr.getName();
+ }
+
+ /**
+ * Sets the name of the user defined style.
+ * Will complain if you try this on a built in style.
+ */
+ public void setUserStyleName(String styleName) {
+ StyleRecord sr = workbook.getStyleRecord(index);
+ if(sr == null) {
+ sr = workbook.createStyleRecord(index);
+ }
+ if(sr.getType() == StyleRecord.STYLE_BUILT_IN) {
+ throw new IllegalArgumentException("Unable to set user specified style names for built in styles!");
+ }
+ sr.setName(styleName);
+ }
/**
* Verifies that this style belongs to the supplied Workbook.
import java.util.*;
import junit.framework.*;
+
+import org.apache.poi.hssf.HSSFTestDataSamples;
import org.apache.poi.util.TempFile;
/**
extends TestCase
{
+ private static HSSFWorkbook openSample(String sampleFileName) {
+ return HSSFTestDataSamples.openSampleWorkbook(sampleFileName);
+ }
+
/** Creates a new instance of TestCellStyle */
public TestCellStyle(String name)
assertFalse(fmtClone.getFormat("Test##") == fmt.getFormat("Test##"));
assertEquals(5, wbClone.getNumberOfFonts());
}
+
+ public void testStyleNames() throws Exception {
+ HSSFWorkbook wb = openSample("WithExtendedStyles.xls");
+ HSSFSheet s = wb.getSheetAt(0);
+ HSSFCell c1 = s.getRow(0).getCell(0);
+ HSSFCell c2 = s.getRow(1).getCell(0);
+ HSSFCell c3 = s.getRow(2).getCell(0);
+
+ HSSFCellStyle cs1 = c1.getCellStyle();
+ HSSFCellStyle cs2 = c2.getCellStyle();
+ HSSFCellStyle cs3 = c3.getCellStyle();
+
+ assertNotNull(cs1);
+ assertNotNull(cs2);
+ assertNotNull(cs3);
+
+ // Check we got the styles we'd expect
+ assertEquals(10, cs1.getFont(wb).getFontHeightInPoints());
+ assertEquals(9, cs2.getFont(wb).getFontHeightInPoints());
+ assertEquals(12, cs3.getFont(wb).getFontHeightInPoints());
+
+ assertEquals(15, cs1.getIndex());
+ assertEquals(23, cs2.getIndex());
+ assertEquals(24, cs3.getIndex());
+
+ // Now check the style names
+// assertEquals(null, cs1.getUserStyleName());
+// assertEquals("style1", cs2.getUserStyleName());
+// assertEquals("style2", cs3.getUserStyleName());
+ }
public static void main(String [] ignored_args)
{