]> source.dussan.org Git - poi.git/commitdiff
writing string formulas now work
authorAvik Sengupta <avik@apache.org>
Sat, 3 Aug 2002 18:16:54 +0000 (18:16 +0000)
committerAvik Sengupta <avik@apache.org>
Sat, 3 Aug 2002 18:16:54 +0000 (18:16 +0000)
git-svn-id: https://svn.apache.org/repos/asf/jakarta/poi/trunk@352822 13f79535-47bb-0310-9956-ffa450edef68

src/java/org/apache/poi/hssf/model/FormulaParser.java
src/java/org/apache/poi/hssf/record/formula/StringPtg.java
src/testcases/org/apache/poi/hssf/data/StringFormulas.xls [new file with mode: 0644]
src/testcases/org/apache/poi/hssf/usermodel/TestFormulas.java

index e56a4c61cab60ca51f71c01a4d12bcf1fdbb70e3..a021803b6c5dfe6632d4a2c04956cee4b900d54e 100644 (file)
@@ -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));
     }
index 30833c605a27cd49c87ae9c607855367aea1f4ec..2a5e5b764a37080ee341d701a12bd7cd569466ba 100644 (file)
@@ -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 (file)
index 0000000..ebf28b0
Binary files /dev/null and b/src/testcases/org/apache/poi/hssf/data/StringFormulas.xls differ
index 447191b3053b49e7e3212b7dc0c35032e18f82c4..6ca51ddb5f073753a5ca0a531a8fd70eabfcb7f2 100644 (file)
@@ -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");