From 85e1c985ce0fbf62a608813606d164c981c3bcca Mon Sep 17 00:00:00 2001 From: Avik Sengupta Date: Sat, 3 Aug 2002 18:16:54 +0000 Subject: [PATCH] writing string formulas now work git-svn-id: https://svn.apache.org/repos/asf/jakarta/poi/trunk@352822 13f79535-47bb-0310-9956-ffa450edef68 --- .../apache/poi/hssf/model/FormulaParser.java | 17 +++++++-- .../poi/hssf/record/formula/StringPtg.java | 2 +- .../apache/poi/hssf/data/StringFormulas.xls | Bin 0 -> 13824 bytes .../poi/hssf/usermodel/TestFormulas.java | 34 ++++++++++++++++++ 4 files changed, 50 insertions(+), 3 deletions(-) create mode 100644 src/testcases/org/apache/poi/hssf/data/StringFormulas.xls diff --git a/src/java/org/apache/poi/hssf/model/FormulaParser.java b/src/java/org/apache/poi/hssf/model/FormulaParser.java index e56a4c61ca..a021803b6c 100644 --- a/src/java/org/apache/poi/hssf/model/FormulaParser.java +++ b/src/java/org/apache/poi/hssf/model/FormulaParser.java @@ -210,7 +210,6 @@ public class FormulaParser { } } - /** Get an Identifier */ private String GetName() { StringBuffer Token = new StringBuffer(); @@ -225,6 +224,20 @@ public class FormulaParser { return Token.toString(); } + /**Get an Identifier AS IS, without stripping white spaces or + converting to uppercase; used for literals */ + private String GetNameAsIs() { + StringBuffer Token = new StringBuffer(); + if (!IsAlpha(Look)) { + Expected("Name"); + } + while (IsAlNum(Look) || IsWhite(Look)) { + Token = Token.append(Look); + GetChar(); + } + return Token.toString(); + } + /** Get a Number */ private String GetNum() { @@ -355,7 +368,7 @@ public class FormulaParser { private void StringLiteral() { Match('"'); - String name= GetName(); + String name= GetNameAsIs(); Match('"'); tokens.add(new StringPtg(name)); } diff --git a/src/java/org/apache/poi/hssf/record/formula/StringPtg.java b/src/java/org/apache/poi/hssf/record/formula/StringPtg.java index 30833c605a..2a5e5b764a 100644 --- a/src/java/org/apache/poi/hssf/record/formula/StringPtg.java +++ b/src/java/org/apache/poi/hssf/record/formula/StringPtg.java @@ -114,7 +114,7 @@ public class StringPtg public String toFormulaString(SheetReferences refs) { - return getValue(); + return "\""+getValue()+"\""; } public byte getDefaultOperandClass() { return Ptg.CLASS_VALUE; diff --git a/src/testcases/org/apache/poi/hssf/data/StringFormulas.xls b/src/testcases/org/apache/poi/hssf/data/StringFormulas.xls new file mode 100644 index 0000000000000000000000000000000000000000..ebf28b0793ade23b8cf94e7b27307a378513559e GIT binary patch literal 13824 zcmeHOUuc_E6#so`k|wTe({^sPy7-Y!=GtU-o8nmMnv6{p9gY=Q)k@m6nJcZ$mS)bUwmkN@}(%^5cQ=KpA^xFf`i)lJLk)1{PBmL0TjaN46j3|ivD zEPd*>{IDpi!8!7J&O@nbQr@imJ=Oh4c{gh&5yK;0)0=iu4{Y1#iL2hL$|K6_$t`@S zZN65?Hnids>gQjamg&yqvl7T)1cMmEjjIiNTi`=85ZM%Ii?+3OcJ;^HV%WQp_aW~t zSJ3t3!PM|mr3zux%B|@tZ0#&<&#SPv4FQD9H7dSE)$T((I)S7ZZ@1NxWX$oUcv+~8 z#3HOq#^T9X*c-(bY?g*awf>T_$jkJ*5e0{))DRu0u;oaK=6ZUH2Jb}&?kO?owl3Ir zo2uDjy9KP%QT;7%YwoaFTjVm}D*x53T3#+Q@Ot{y(ph~&WvlNR zk26C>TV~`gTALxQ)wQaQd>9Q{ZX^dn{I12fuYSJ&@PVGB%G*`mq5Nf)Vh|f@FnRL%#BtPm{9N48_xKZEQaz#SyFE>~q+rMO+F={neQHW_ ztO7r=32;BDI;xIW(n``4N9^W8G;zEfQO8f=3WHw8l{@S|fnkOtxXNKDfy@*Q0tNwt zfI+|@U=UCQmbgjt>8`IF5ZPHRoH_r?wMUPJ-gzy6j?Eu@#kdd`NwV3M`Z!5;hYylu zcRN68B2AL&NYf;q8!%#rM7DT~BsD_T zHKsn~E}iZFe%HrVob|a=S6hByxv)W`#3X}&LBJqj5HJWB1PlTO0fT@+z#w1{FbEg~ zmW%&Am*+2^+j@Iw<}Bm?=(Tqx{{NX2BuV^#fFw_J`bZl{r$}o^r%4j4Pmv_yyxyBhO$?8xGWNq`sgd#L(wVHiCpA8i9!FLm`utcQR2x*S znulpz;(s}(aGu%~i`$FE9jK$CI3R}(;`pI-W^_Ux^G#@bbA~`nOfm=<1PlTO0fT@+ zz#w1{FbEg~3<3s$Re^xS@e%?{tSis*i-}~Zqm((z(J(>$->4y$QerpCpmH}PwV;r2)Ac4kL-YO zU{*OEaI$hP^TCxIqV)_$aajErb;T>r`45o4?28{OZYT#-Og{AD1fJn%Q2YK6Q8=PL zi6qPaeWWR=p6;f7xrgNbS*d+_S4I!WnE9}m7KYS^l#akZ^JnM3=)ZQo(EmRH>3%&x literal 0 HcmV?d00001 diff --git a/src/testcases/org/apache/poi/hssf/usermodel/TestFormulas.java b/src/testcases/org/apache/poi/hssf/usermodel/TestFormulas.java index 447191b305..6ca51ddb5f 100644 --- a/src/testcases/org/apache/poi/hssf/usermodel/TestFormulas.java +++ b/src/testcases/org/apache/poi/hssf/usermodel/TestFormulas.java @@ -824,6 +824,40 @@ extends TestCase { out.close(); assertTrue("file exists",file.exists()); } + + public void testStringFormulas() + throws java.io.IOException + { + String readFilename = System.getProperty("HSSF.testdata.path"); + + File file = File.createTempFile("testStringFormula",".xls"); + FileOutputStream out = new FileOutputStream(file); + HSSFWorkbook wb = new HSSFWorkbook(); + HSSFSheet s = wb.createSheet("A"); + HSSFRow r = null; + HSSFCell c = null; + r = s.createRow((short)0); + c=r.createCell((short)1); c.setCellFormula("UPPER(\"abc\")"); + c=r.createCell((short)2); c.setCellFormula("LOWER(\"ABC\")"); + c=r.createCell((short)3); c.setCellFormula("CONCATENATE(\" my \",\" name \")"); + + wb.write(out); + out.close(); + + assertTrue("file exists",file.exists()); + + FileInputStream in = new FileInputStream(readFilename+File.separator+"StringFormulas.xls"); + wb = new HSSFWorkbook(in); + s = wb.getSheetAt(0); + r = s.getRow(0); + c = r.getCell((short)0); + assertTrue("expected: UPPER(\"xyz\") got "+c.getCellFormula(), ("UPPER(\"xyz\")").equals(c.getCellFormula())); + //c = r.getCell((short)1); + //assertTrue("expected: A!A1+A!B1 got: "+c.getCellFormula(), ("A!A1+A!B1").equals(c.getCellFormula())); + in.close(); + } + + public static void main(String [] args) { System.out .println("Testing org.apache.poi.hssf.usermodel.TestFormulas"); -- 2.39.5