]> source.dussan.org Git - poi.git/commitdiff
*read only* support for optimized ifs. meaning "if(A1=A3,A1,A2)" and stuff.
authorAndrew C. Oliver <acoliver@apache.org>
Fri, 6 Sep 2002 03:56:47 +0000 (03:56 +0000)
committerAndrew C. Oliver <acoliver@apache.org>
Fri, 6 Sep 2002 03:56:47 +0000 (03:56 +0000)
This optimized if has the conceptual clarity of a featherweight elephant
carrier used as a pizza topping.  This concludes my therapy session.  I love
this project :-).  Next week I'll try and get write support underway unless
someone beats me to it.
PR:
Obtained from:
Submitted by:
Reviewed by:

git-svn-id: https://svn.apache.org/repos/asf/jakarta/poi/trunk@352839 13f79535-47bb-0310-9956-ffa450edef68

src/java/org/apache/poi/hssf/model/FormulaParser.java
src/java/org/apache/poi/hssf/record/formula/AbstractFunctionPtg.java
src/java/org/apache/poi/hssf/record/formula/AttrPtg.java
src/testcases/org/apache/poi/hssf/usermodel/TestFormulas.java

index 78bd113d423008984667cd1d2271d78ddbb94d01..b84aabbb75ed777f6127ea6580e5ea4d19210773 100644 (file)
@@ -603,20 +603,42 @@ end;
         int numPtgs = ptgs.length;
         OperationPtg o;
         int numOperands;
+        String result=null;
         String[] operands;
+        AttrPtg ifptg = null;
         for (int i=0;i<numPtgs;i++) {
            // Excel allows to have AttrPtg at position 0 (such as Blanks) which
            // do not have any operands. Skip them.
             if (ptgs[i] instanceof OperationPtg && i>0) {
                   o = (OperationPtg) ptgs[i];
-                  numOperands = o.getNumberOfOperands();
-                  operands = new String[numOperands];
-                  for (int j=0;j<numOperands;j++) {
-                      operands[numOperands-j-1] = (String) stack.pop(); //TODO: catch stack underflow and throw parse exception. 
+                  
+                  if (o instanceof AttrPtg && ((AttrPtg)o).isOptimizedIf()) {
+                        ifptg=(AttrPtg)o;
+                  } else {
                       
-                  }  
-                  String result = o.toFormulaString(operands);
-                  stack.push(result);
+                      numOperands = o.getNumberOfOperands();
+                      operands = new String[numOperands];
+                      
+                      for (int j=0;j<numOperands;j++) {
+                          operands[numOperands-j-1] = (String) stack.pop(); //TODO: catch stack underflow and throw parse exception. 
+                      }  
+
+                      if ( (o instanceof AbstractFunctionPtg) && 
+                            ((AbstractFunctionPtg)o).getName().equals("specialflag") &&
+                            ifptg != null
+                            ) {
+                             // this special case will be way different.
+                             result = ifptg.toFormulaString(
+                                  new String[] {(o.toFormulaString(operands))}
+                                                           );
+                             ifptg = null;
+                      } else {                      
+                        result = o.toFormulaString(operands);                                              
+                      }
+                      stack.push(result);                                        
+                  }
+                      
+                  
             } else {
                 stack.push(ptgs[i].toFormulaString(refs));
             }
index 007d97ed38bc09a1b590b88a3b8f77ca418c1e64..3f7991da71c08b1d106d9815a24b8aa3cddb8188 100644 (file)
@@ -3,6 +3,8 @@ package org.apache.poi.hssf.record.formula;
 import org.apache.poi.util.BinaryTree;
 import org.apache.poi.hssf.util.SheetReferences;
 
+import java.util.Stack;
+
 /**
  * This class provides the base functionality for Excel sheet functions 
  * There are two kinds of function Ptgs - tFunc and tFuncVar
@@ -44,11 +46,7 @@ public abstract class AbstractFunctionPtg extends OperationPtg {
     }
     
     public String getName() {
-       if(field_2_fnc_index != 1) {
         return lookupName(field_2_fnc_index);
-       } else {
-        return "Funky case of formula recombinating";
-       }
     }
     
     public String toFormulaString(SheetReferences refs) {
@@ -56,9 +54,12 @@ public abstract class AbstractFunctionPtg extends OperationPtg {
     }
     
     public String toFormulaString(String[] operands) {
-        StringBuffer buf = new StringBuffer();
-        if (field_2_fnc_index != 1) {
-          buf.append(getName()+"(");
+        StringBuffer buf = new StringBuffer();        
+          
+          if (field_2_fnc_index != 1) {
+              buf.append(getName());
+              buf.append('(');
+          }
           if (operands.length >0) {
               for (int i=0;i<operands.length;i++) {
                   buf.append(operands[i]);
@@ -66,12 +67,9 @@ public abstract class AbstractFunctionPtg extends OperationPtg {
               }
               buf.deleteCharAt(buf.length()-1);
           }
-        buf.append(")");
-        } else {
-         throw new RuntimeException("FUNKY CASE OF FORMULA RECOMBINATION NOT "+
-         "YET IMPLEMENTED");
-         
-        }
+          if (field_2_fnc_index != 1) {
+            buf.append(")");
+          }
         return buf.toString();
     }
     
@@ -98,6 +96,7 @@ public abstract class AbstractFunctionPtg extends OperationPtg {
         BinaryTree dmap = new BinaryTree();
 
         dmap.put(new Integer(0),"COUNT");
+        dmap.put(new Integer(1),"specialflag");
         dmap.put(new Integer(2),"ISNA");
         dmap.put(new Integer(3),"ISERROR");
         dmap.put(new Integer(4),"SUM");
index 91b8b997ab3746bd97a850f7925c1964064c6580..9cac2b21e98f706f98a47a4608066a664e621664 100644 (file)
@@ -203,6 +203,8 @@ public class AttrPtg
             return operands[ 0 ];
         } else if (optiIf.isSet(field_1_options)) {
             return toFormulaString((SheetReferences)null) + "(" + operands[ 0 ]             +")"; 
+        } else if (optGoto.isSet(field_1_options)) {
+            return toFormulaString((SheetReferences)null) + operands[0];   //goto isn't a real formula element should not show up
         } else {
             return toFormulaString((SheetReferences)null) + "(" + operands[ 0 ] + ")";
         }
@@ -230,7 +232,7 @@ public class AttrPtg
         return "CHOOSE";
       }
       if(optGoto.isSet(field_1_options)) {
-        return "GOTO";
+        return "";
       }
       if(sum.isSet(field_1_options)) {
         return "SUM";
index c15c54843c41aba6366a23acfa67c7ba698dae5f..1aebc890283630d85d71d7eaadb71f0a55e62a00 100644 (file)
@@ -878,14 +878,24 @@ extends TestCase {
             wb.write(out);
             out.close();
             
-             assertTrue("file exists",file.exists());
+            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)4);
+            
+            assertTrue("expected: IF(A1=D1,\"A1\",\"B1\") got "+c.getCellFormula(), ("IF(A1=D1,\"A1\",\"B1\")").equals(c.getCellFormula()));            
+            in.close();
+            
             
-            FileInputStream in = new FileInputStream(readFilename+File.separator+"IfFormulaTest.xls");
+            in = new FileInputStream(readFilename+File.separator+"IfFormulaTest.xls");
             wb = new HSSFWorkbook(in);
             s = wb.getSheetAt(0);
             r = s.getRow(3);
             c = r.getCell((short)0);
-            assertTrue("expected: IF(A3=A1,\"A1\",\"B1\") got "+c.getCellFormula(), ("IF(A3=A1,\"A1\",\"B1\")").equals(c.getCellFormula()));
+            assertTrue("expected: IF(A3=A1,\"A1\",\"A2\") got "+c.getCellFormula(), ("IF(A3=A1,\"A1\",\"A2\")").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();