From bfd5ebe008584a3946cae39624d2e190f2d933ac Mon Sep 17 00:00:00 2001 From: Nick Burch Date: Mon, 28 Jan 2008 12:18:12 +0000 Subject: [PATCH] Mostly fix bug 42618 (really this time...) - can now open the file properly, but getCellFormula() is still playing up (bug #44306 opened for this) git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@615859 13f79535-47bb-0310-9956-ffa450edef68 --- .../poi/hssf/record/formula/ConcatPtg.java | 5 +- .../apache/poi/hssf/record/formula/Ptg.java | 16 +++-- .../poi/hssf/record/formula/RangePtg.java | 6 +- .../poi/hssf/record/formula/UnknownPtg.java | 2 +- .../poi/hssf/data/TestDataValidation.xls | Bin 21504 -> 21504 bytes .../poi/hssf/record/TestFormulaRecord.java | 56 ++++++++++++++++++ .../apache/poi/hssf/usermodel/TestBugs.java | 22 +++++-- 7 files changed, 90 insertions(+), 17 deletions(-) diff --git a/src/java/org/apache/poi/hssf/record/formula/ConcatPtg.java b/src/java/org/apache/poi/hssf/record/formula/ConcatPtg.java index b5a2de2a5..41d2de0cb 100644 --- a/src/java/org/apache/poi/hssf/record/formula/ConcatPtg.java +++ b/src/java/org/apache/poi/hssf/record/formula/ConcatPtg.java @@ -34,11 +34,10 @@ public class ConcatPtg public final static byte sid = 0x08; private final static String CONCAT = "&"; - + public ConcatPtg(RecordInputStream in) { - - // doesn't need anything + // No contents } public ConcatPtg() { diff --git a/src/java/org/apache/poi/hssf/record/formula/Ptg.java b/src/java/org/apache/poi/hssf/record/formula/Ptg.java index cc9a4236b..f006509c2 100644 --- a/src/java/org/apache/poi/hssf/record/formula/Ptg.java +++ b/src/java/org/apache/poi/hssf/record/formula/Ptg.java @@ -137,8 +137,8 @@ public abstract class Ptg break; case DividePtg.sid : // 0x06 - retval = new DividePtg(in); - break; + retval = new DividePtg(in); + break; case PowerPtg.sid : // 0x07 retval = new PowerPtg(in); @@ -208,6 +208,7 @@ public abstract class Ptg break; case AttrPtg.sid : // 0x19 + case 0x1a : retval = new AttrPtg(in); break; @@ -224,8 +225,8 @@ public abstract class Ptg break; case NumberPtg.sid : // 0x1f - retval = new NumberPtg(in); - break; + retval = new NumberPtg(in); + break; case ArrayPtg.sid : // 0x20 retval = new ArrayPtg(in); @@ -350,9 +351,12 @@ public abstract class Ptg case DeletedArea3DPtg.sid + 0x40 : // 0x7d retval = new DeletedArea3DPtg(in); break; - + + case 0x00: + retval = new UnknownPtg(); + break; + default : - //retval = new UnknownPtg(); throw new java.lang.UnsupportedOperationException(" Unknown Ptg in Formula: 0x"+ Integer.toHexString(( int ) id) + " (" + ( int ) id + ")"); diff --git a/src/java/org/apache/poi/hssf/record/formula/RangePtg.java b/src/java/org/apache/poi/hssf/record/formula/RangePtg.java index f0bd8c1c1..51df7844a 100644 --- a/src/java/org/apache/poi/hssf/record/formula/RangePtg.java +++ b/src/java/org/apache/poi/hssf/record/formula/RangePtg.java @@ -25,22 +25,22 @@ import org.apache.poi.hssf.record.RecordInputStream; */ public class RangePtg extends OperationPtg { + public final static int SIZE = 1; public final static byte sid = 0x11; - public RangePtg() { } public RangePtg(RecordInputStream in) { - // doesn't need anything + // No contents } public int getSize() { - return 1; + return SIZE; } public void writeBytes( byte[] array, int offset ) diff --git a/src/java/org/apache/poi/hssf/record/formula/UnknownPtg.java b/src/java/org/apache/poi/hssf/record/formula/UnknownPtg.java index 9cc515832..1badf5197 100644 --- a/src/java/org/apache/poi/hssf/record/formula/UnknownPtg.java +++ b/src/java/org/apache/poi/hssf/record/formula/UnknownPtg.java @@ -28,7 +28,7 @@ import org.apache.poi.hssf.record.RecordInputStream; public class UnknownPtg extends Ptg { - private short size; + private short size = 1; /** Creates new UnknownPtg */ diff --git a/src/testcases/org/apache/poi/hssf/data/TestDataValidation.xls b/src/testcases/org/apache/poi/hssf/data/TestDataValidation.xls index 556ea8192758d22c8b5427a379673771298ee08d..524136921295c334de8c0ac65bbb96c77dc9e63c 100644 GIT binary patch delta 294 zcmZoz!Pu~Zal=Fo2^V_?AmC;QNv$YR$VtshFUe?S;A7ajc_oLrKI6p656!D4FOX%G zNSVS6R411)g^_^;2ti;U<3t9P$sgTnrH~|98Cao`eGDQD8j}m9tHo2MfP@$s*nkiO z_A!Fw3_xs zWlEc}d65kpE0crUW@&F8#?9NDk~x?@dILF6StcgfObYnH)Z{ZcFwkwYL*M}>07!d6 AE&u=k delta 381 zcmZoz!Pu~Zal=Foi3WQHAmC;QNv$YR$VtshFUe?S;A8l>c_oLrKI7cU56!D42k5b= z%Y&3P^gMFl1kzv*L+>L8Mg|rj1c7~wa~V_^WF{9%m*G{<%D@U$KZQYrK?9~<4sJe3 zmXU!C2ti;UBS@J6OqmRtGIj=bgtEyG>YG-I2l0Rf>_5g zIbRxV4!R;P1}-E;j51(FoM6ii00U-onhhH(Q;pl^`(8YZo2#6XIhb^PfSji+lLCG) Q?em!&80fayA@Beb03>8oW&i*H diff --git a/src/testcases/org/apache/poi/hssf/record/TestFormulaRecord.java b/src/testcases/org/apache/poi/hssf/record/TestFormulaRecord.java index 99026bb80..9cf746ce0 100644 --- a/src/testcases/org/apache/poi/hssf/record/TestFormulaRecord.java +++ b/src/testcases/org/apache/poi/hssf/record/TestFormulaRecord.java @@ -21,6 +21,16 @@ package org.apache.poi.hssf.record; +import java.io.ByteArrayInputStream; + +import org.apache.poi.hssf.record.formula.AttrPtg; +import org.apache.poi.hssf.record.formula.ConcatPtg; +import org.apache.poi.hssf.record.formula.FuncVarPtg; +import org.apache.poi.hssf.record.formula.IntPtg; +import org.apache.poi.hssf.record.formula.RangePtg; +import org.apache.poi.hssf.record.formula.ReferencePtg; +import org.apache.poi.hssf.record.formula.UnknownPtg; + import junit.framework.TestCase; /** @@ -108,6 +118,52 @@ public class TestFormulaRecord assertEquals("Offset 22", 1, output[26]); } + public void testWithConcat() throws Exception { + // =CHOOSE(2,A2,A3,A4) + byte[] data = new byte[] { + 6, 0, 68, 0, + 1, 0, 1, 0, 15, 0, 0, 0, 0, 0, 0, 0, 57, + 64, 0, 0, 12, 0, 12, -4, 46, 0, + 30, 2, 0, // Int - 2 + 25, 4, 3, 0, // Attr + 8, 0, // Concat + 17, 0, // Range + 26, 0, 35, 0, // Bit like an attr + 36, 1, 0, 0, -64, // Ref - A2 + 25, 8, 21, 0, // Attr + 36, 2, 0, 0, -64, // Ref - A3 + 25, 8, 12, 0, // Attr + 36, 3, 0, 0, -64, // Ref - A4 + 25, 8, 3, 0, // Attr + 66, 4, 100, 0 // CHOOSE + }; + RecordInputStream inp = new RecordInputStream( + new ByteArrayInputStream(data) + ); + inp.nextRecord(); + + FormulaRecord fr = new FormulaRecord(inp); + + assertEquals(14, fr.getNumberOfExpressionTokens()); + assertEquals(IntPtg.class, fr.getParsedExpression().get(0).getClass()); + assertEquals(AttrPtg.class, fr.getParsedExpression().get(1).getClass()); + assertEquals(ConcatPtg.class, fr.getParsedExpression().get(2).getClass()); + assertEquals(UnknownPtg.class, fr.getParsedExpression().get(3).getClass()); + assertEquals(RangePtg.class, fr.getParsedExpression().get(4).getClass()); + assertEquals(UnknownPtg.class, fr.getParsedExpression().get(5).getClass()); + assertEquals(AttrPtg.class, fr.getParsedExpression().get(6).getClass()); + assertEquals(ReferencePtg.class, fr.getParsedExpression().get(7).getClass()); + assertEquals(AttrPtg.class, fr.getParsedExpression().get(8).getClass()); + assertEquals(ReferencePtg.class, fr.getParsedExpression().get(9).getClass()); + assertEquals(AttrPtg.class, fr.getParsedExpression().get(10).getClass()); + assertEquals(ReferencePtg.class, fr.getParsedExpression().get(11).getClass()); + assertEquals(AttrPtg.class, fr.getParsedExpression().get(12).getClass()); + assertEquals(FuncVarPtg.class, fr.getParsedExpression().get(13).getClass()); + + FuncVarPtg choose = (FuncVarPtg)fr.getParsedExpression().get(13); + assertEquals("CHOOSE", choose.getName()); + } + public static void main(String [] ignored_args) { diff --git a/src/testcases/org/apache/poi/hssf/usermodel/TestBugs.java b/src/testcases/org/apache/poi/hssf/usermodel/TestBugs.java index 3c584f33a..590ec4943 100644 --- a/src/testcases/org/apache/poi/hssf/usermodel/TestBugs.java +++ b/src/testcases/org/apache/poi/hssf/usermodel/TestBugs.java @@ -1013,11 +1013,9 @@ extends TestCase { /** * Bug 42618: RecordFormatException reading a file containing * =CHOOSE(2,A2,A3,A4) + * TODO - support getCellFormula too! */ public void test42618() throws Exception { - //Comment the test until we are sure it passes. - // Yegor, January 25, 2008 - /* FileInputStream in = new FileInputStream(new File(cwd, "SimpleWithChoose.xls")); HSSFWorkbook wb = new HSSFWorkbook(in); in.close(); @@ -1031,7 +1029,23 @@ extends TestCase { wb = new HSSFWorkbook(new ByteArrayInputStream(out.toByteArray())); assertTrue("No Exceptions while reading file", true); - */ + + // Check we detect the string properly too + HSSFSheet s = wb.getSheetAt(0); + + // Textual value + HSSFRow r1 = s.getRow(0); + HSSFCell c1 = r1.getCell((short)1); + assertEquals("=CHOOSE(2,A2,A3,A4)", c1.getRichStringCellValue().toString()); + + // Formula Value + HSSFRow r2 = s.getRow(1); + HSSFCell c2 = r2.getCell((short)1); + assertEquals(25, (int)c2.getNumericCellValue()); + + // This will blow up with a + // "EmptyStackException" + //assertEquals("=CHOOSE(2,A2,A3,A4)", c2.getCellFormula()); } }