From 37ff360b46c689631bcb4c604a2083694f4ced65 Mon Sep 17 00:00:00 2001 From: Avik Sengupta Date: Wed, 25 Dec 2002 17:36:05 +0000 Subject: [PATCH] PR:15324- styles in formula cells; submitted by Danny Mui git-svn-id: https://svn.apache.org/repos/asf/jakarta/poi/trunk@352961 13f79535-47bb-0310-9956-ffa450edef68 --- .../apache/poi/hssf/usermodel/HSSFCell.java | 4 +- .../poi/hssf/usermodel/TestHSSFCell.java | 56 +++++++++++++++++++ 2 files changed, 59 insertions(+), 1 deletion(-) diff --git a/src/java/org/apache/poi/hssf/usermodel/HSSFCell.java b/src/java/org/apache/poi/hssf/usermodel/HSSFCell.java index 0bde7c22ef..46aeb29c25 100644 --- a/src/java/org/apache/poi/hssf/usermodel/HSSFCell.java +++ b/src/java/org/apache/poi/hssf/usermodel/HSSFCell.java @@ -707,7 +707,9 @@ public class HSSFCell FormulaRecordAggregate rec = (FormulaRecordAggregate) record; rec.getFormulaRecord().setOptions(( short ) 2); rec.getFormulaRecord().setValue(0); - rec.setXFIndex(( short ) 0x0f); + + //only set to default if there is no extended format index already set + if (rec.getXFIndex() == (short)0) rec.setXFIndex(( short ) 0x0f); FormulaParser fp = new FormulaParser(formula+";",book); fp.parse(); Ptg[] ptg = fp.getRPNPtg(); diff --git a/src/testcases/org/apache/poi/hssf/usermodel/TestHSSFCell.java b/src/testcases/org/apache/poi/hssf/usermodel/TestHSSFCell.java index 3484c0b424..5764732baa 100644 --- a/src/testcases/org/apache/poi/hssf/usermodel/TestHSSFCell.java +++ b/src/testcases/org/apache/poi/hssf/usermodel/TestHSSFCell.java @@ -63,6 +63,7 @@ import org.apache.poi.hssf.record.Record; import org.apache.poi.hssf.record.BOFRecord; import org.apache.poi.hssf.record.EOFRecord; import org.apache.poi.hssf.util.CellReference; +import org.apache.poi.hssf.util.HSSFColor; import java.io.File; import java.io.FileInputStream; @@ -225,6 +226,61 @@ extends TestCase { 3, s.getActiveCellRow()); } + /** + * test that Cell Styles being applied to formulas remain intact + */ + public void testFormulaStyle() + throws java.io.IOException { + String readFilename = System.getProperty("HSSF.testdata.path"); + + File file = File.createTempFile("testBoolErr",".xls"); + FileOutputStream out = new FileOutputStream(file); + HSSFWorkbook wb = new HSSFWorkbook(); + HSSFSheet s = wb.createSheet("Sheet1"); + HSSFRow r = null; + HSSFCell c = null; + HSSFCellStyle cs = wb.createCellStyle(); + HSSFFont f = wb.createFont(); + f.setFontHeightInPoints((short) 20); + f.setColor((short) HSSFColor.RED.index); + f.setBoldweight(f.BOLDWEIGHT_BOLD); + f.setFontName("Arial Unicode MS"); + cs.setFillBackgroundColor((short)3); + cs.setFont(f); + cs.setBorderTop((short)1); + cs.setBorderRight((short)1); + cs.setBorderLeft((short)1); + cs.setBorderBottom((short)1); + + r = s.createRow((short)0); + c=r.createCell((short)0); + c.setCellStyle(cs); + c.setCellFormula("2*3"); + + wb.write(out); + out.close(); + + assertTrue("file exists",file.exists()); + + FileInputStream in = new FileInputStream(file); + wb = new HSSFWorkbook(in); + s = wb.getSheetAt(0); + r = s.getRow(0); + c = r.getCell((short)0); + + assertTrue("Formula Cell at 0,0", (c.getCellType()==c.CELL_TYPE_FORMULA)); + cs = c.getCellStyle(); + + assertNotNull("Formula Cell Style", cs); + assertTrue("Font Index Matches", (cs.getFontIndex() == f.getIndex())); + assertTrue("Top Border", (cs.getBorderTop() == (short)1)); + assertTrue("Left Border", (cs.getBorderLeft() == (short)1)); + assertTrue("Right Border", (cs.getBorderRight() == (short)1)); + assertTrue("Bottom Border", (cs.getBorderBottom() == (short)1)); + + in.close(); + } + public static void main(String [] args) { System.out .println("Testing org.apache.poi.hssf.usermodel.TestHSSFCell"); -- 2.39.5