acoliver 2002/09/05 20:56:47
Modified: src/java/org/apache/poi/hssf/model FormulaParser.java
src/java/org/apache/poi/hssf/record/formula
AbstractFunctionPtg.java AttrPtg.java
src/testcases/org/apache/poi/hssf/usermodel
TestFormulas.java
Log:
*read only* support for optimized ifs. meaning "if(A1=A3,A1,A2)" and stuff.
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.
Revision Changes Path
1.5 +29 -7
jakarta-poi/src/java/org/apache/poi/hssf/model/FormulaParser.java
Index: FormulaParser.java
===================================================================
RCS file:
/home/cvs/jakarta-poi/src/java/org/apache/poi/hssf/model/FormulaParser.java,v
retrieving revision 1.4
retrieving revision 1.5
diff -u -r1.4 -r1.5
--- FormulaParser.java 2 Sep 2002 21:16:29 -0000 1.4
+++ FormulaParser.java 6 Sep 2002 03:56:46 -0000 1.5
@@ -603,20 +603,42 @@
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));
}
1.6 +12 -13
jakarta-poi/src/java/org/apache/poi/hssf/record/formula/AbstractFunctionPtg.java
Index: AbstractFunctionPtg.java
===================================================================
RCS file:
/home/cvs/jakarta-poi/src/java/org/apache/poi/hssf/record/formula/AbstractFunctionPtg.java,v
retrieving revision 1.5
retrieving revision 1.6
diff -u -r1.5 -r1.6
--- AbstractFunctionPtg.java 2 Sep 2002 21:16:29 -0000 1.5
+++ AbstractFunctionPtg.java 6 Sep 2002 03:56:47 -0000 1.6
@@ -3,6 +3,8 @@
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 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 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 @@
}
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 @@
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");
1.13 +3 -1
jakarta-poi/src/java/org/apache/poi/hssf/record/formula/AttrPtg.java
Index: AttrPtg.java
===================================================================
RCS file:
/home/cvs/jakarta-poi/src/java/org/apache/poi/hssf/record/formula/AttrPtg.java,v
retrieving revision 1.12
retrieving revision 1.13
diff -u -r1.12 -r1.13
--- AttrPtg.java 5 Sep 2002 00:26:26 -0000 1.12
+++ AttrPtg.java 6 Sep 2002 03:56:47 -0000 1.13
@@ -203,6 +203,8 @@
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 @@
return "CHOOSE";
}
if(optGoto.isSet(field_1_options)) {
- return "GOTO";
+ return "";
}
if(sum.isSet(field_1_options)) {
return "SUM";
1.24 +13 -3
jakarta-poi/src/testcases/org/apache/poi/hssf/usermodel/TestFormulas.java
Index: TestFormulas.java
===================================================================
RCS file:
/home/cvs/jakarta-poi/src/testcases/org/apache/poi/hssf/usermodel/TestFormulas.java,v
retrieving revision 1.23
retrieving revision 1.24
diff -u -r1.23 -r1.24
--- TestFormulas.java 2 Sep 2002 16:13:48 -0000 1.23
+++ TestFormulas.java 6 Sep 2002 03:56:47 -0000 1.24
@@ -878,14 +878,24 @@
wb.write(out);
out.close();
- assertTrue("file exists",file.exists());
+ assertTrue("file exists",file.exists());
- FileInputStream in = new
FileInputStream(readFilename+File.separator+"IfFormulaTest.xls");
+ 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();
+
+
+ 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();
--
To unsubscribe, e-mail: <mailto:[EMAIL PROTECTED]>
For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>