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));
}
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
}
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) {
}
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]);
}
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();
}
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");
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 ] + ")";
}
return "CHOOSE";
}
if(optGoto.isSet(field_1_options)) {
- return "GOTO";
+ return "";
}
if(sum.isSet(field_1_options)) {
return "SUM";
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();