From e732f177ec448d35d0ad56376b324e8c3f8cb056 Mon Sep 17 00:00:00 2001 From: "Andrew C. Oliver" Date: Mon, 2 Sep 2002 21:16:29 +0000 Subject: [PATCH] preliminary support for "if" formulas. Doesn't work yet because of this strange "aggregate this stuff" function PR: Obtained from: Submitted by: Reviewed by: git-svn-id: https://svn.apache.org/repos/asf/jakarta/poi/trunk@352834 13f79535-47bb-0310-9956-ffa450edef68 --- .../apache/poi/hssf/model/FormulaParser.java | 22 +++++++++----- .../record/formula/AbstractFunctionPtg.java | 29 ++++++++++++------- .../poi/hssf/record/formula/AttrPtg.java | 9 ++++-- .../apache/poi/hssf/record/formula/Ptg.java | 4 +++ 4 files changed, 44 insertions(+), 20 deletions(-) diff --git a/src/java/org/apache/poi/hssf/model/FormulaParser.java b/src/java/org/apache/poi/hssf/model/FormulaParser.java index a021803b6c..78bd113d42 100644 --- a/src/java/org/apache/poi/hssf/model/FormulaParser.java +++ b/src/java/org/apache/poi/hssf/model/FormulaParser.java @@ -307,15 +307,15 @@ public class FormulaParser { private Ptg getFunction(String name,byte numArgs) { Ptg retval = null; - retval = new FuncVarPtg(name,numArgs); - /** if (numArgs == 1 && name.equals("SUM")) { + //retval = new FuncVarPtg(name,numArgs); + if (name.equals("IF")) { AttrPtg ptg = new AttrPtg(); - ptg.setData((short)1); //sums don't care but this is what excel does. - ptg.setSum(true); + ptg.setData((short)6); //sums don't care but this is what excel does. + ptg.setOptimizedIf(true); retval = ptg; } else { retval = new FuncVarPtg(name,numArgs); - }*/ + } return retval; } @@ -394,12 +394,13 @@ public class FormulaParser { /** Parse and Translate a Math Term */ private void Term(){ Factor(); - while (Look == '*' || Look == '/' || Look == '^' || Look == '&') { + while (Look == '*' || Look == '/' || Look == '^' || Look == '&' || Look == '=' ) { ///TODO do we need to do anything here?? if (Look == '*') Multiply(); if (Look == '/') Divide(); if (Look == '^') Power(); if (Look == '&') Concat(); + if (Look == '=') Equal(); } } @@ -410,14 +411,19 @@ public class FormulaParser { tokens.add(new AddPtg()); } - /** Recognize and Translate an Add */ + /** Recognize and Translate a Concatination */ private void Concat() { Match('&'); Term(); tokens.add(new ConcatPtg()); } - + /** Recognize and Translate a test for Equality */ + private void Equal() { + Match('='); + Term(); + tokens.add(new EqualPtg()); + } /** Recognize and Translate a Subtract */ private void Subtract() { diff --git a/src/java/org/apache/poi/hssf/record/formula/AbstractFunctionPtg.java b/src/java/org/apache/poi/hssf/record/formula/AbstractFunctionPtg.java index 58eb980847..007d97ed38 100644 --- a/src/java/org/apache/poi/hssf/record/formula/AbstractFunctionPtg.java +++ b/src/java/org/apache/poi/hssf/record/formula/AbstractFunctionPtg.java @@ -21,8 +21,7 @@ public abstract class AbstractFunctionPtg extends OperationPtg { protected byte field_1_num_args; protected short field_2_fnc_index; - - + public String toString() { StringBuffer buffer = new StringBuffer(); buffer @@ -45,7 +44,11 @@ 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) { @@ -54,15 +57,21 @@ public abstract class AbstractFunctionPtg extends OperationPtg { public String toFormulaString(String[] operands) { StringBuffer buf = new StringBuffer(); - buf.append(getName()+"("); - if (operands.length >0) { - for (int i=0;i0) { + for (int i=0;i