Search in sources :

Example 6 with AttrPtg

use of org.apache.poi.ss.formula.ptg.AttrPtg in project poi by apache.

the class TestFormulaParserIf method testNestedFunctionIf.

/**
	 * Make sure the ptgs are generated properly with two functions embedded
	 *
	 */
public void testNestedFunctionIf() {
    Ptg[] ptgs = parseFormula("IF(A1=B1,AVERAGE(A1:B1),AVERAGE(A2:B2))");
    assertEquals(11, ptgs.length);
    assertTrue("IF Attr set correctly", (ptgs[3] instanceof AttrPtg));
    AttrPtg ifFunc = (AttrPtg) ptgs[3];
    assertTrue("It is not an if", ifFunc.isOptimizedIf());
    assertTrue("Average Function set correctly", (ptgs[5] instanceof FuncVarPtg));
}
Also used : IntPtg(org.apache.poi.ss.formula.ptg.IntPtg) Ptg(org.apache.poi.ss.formula.ptg.Ptg) LessEqualPtg(org.apache.poi.ss.formula.ptg.LessEqualPtg) FuncVarPtg(org.apache.poi.ss.formula.ptg.FuncVarPtg) RefPtg(org.apache.poi.ss.formula.ptg.RefPtg) NotEqualPtg(org.apache.poi.ss.formula.ptg.NotEqualPtg) FuncPtg(org.apache.poi.ss.formula.ptg.FuncPtg) AttrPtg(org.apache.poi.ss.formula.ptg.AttrPtg) AddPtg(org.apache.poi.ss.formula.ptg.AddPtg) MultiplyPtg(org.apache.poi.ss.formula.ptg.MultiplyPtg) StringPtg(org.apache.poi.ss.formula.ptg.StringPtg) LessThanPtg(org.apache.poi.ss.formula.ptg.LessThanPtg) BoolPtg(org.apache.poi.ss.formula.ptg.BoolPtg) FuncVarPtg(org.apache.poi.ss.formula.ptg.FuncVarPtg) AttrPtg(org.apache.poi.ss.formula.ptg.AttrPtg)

Example 7 with AttrPtg

use of org.apache.poi.ss.formula.ptg.AttrPtg in project poi by apache.

the class TestFormulaParserIf method testYN.

public void testYN() {
    Ptg[] ptgs = parseFormula("IF(TRUE,\"Y\",\"N\")");
    assertEquals(7, ptgs.length);
    BoolPtg flag = (BoolPtg) ptgs[0];
    AttrPtg funif = (AttrPtg) ptgs[1];
    StringPtg y = (StringPtg) ptgs[2];
    AttrPtg goto1 = (AttrPtg) ptgs[3];
    StringPtg n = (StringPtg) ptgs[4];
    assertEquals(true, flag.getValue());
    assertEquals("Y", y.getValue());
    assertEquals("N", n.getValue());
    assertEquals("IF", funif.toFormulaString());
    assertTrue("tAttrSkip ptg exists", goto1.isSkip());
}
Also used : IntPtg(org.apache.poi.ss.formula.ptg.IntPtg) Ptg(org.apache.poi.ss.formula.ptg.Ptg) LessEqualPtg(org.apache.poi.ss.formula.ptg.LessEqualPtg) FuncVarPtg(org.apache.poi.ss.formula.ptg.FuncVarPtg) RefPtg(org.apache.poi.ss.formula.ptg.RefPtg) NotEqualPtg(org.apache.poi.ss.formula.ptg.NotEqualPtg) FuncPtg(org.apache.poi.ss.formula.ptg.FuncPtg) AttrPtg(org.apache.poi.ss.formula.ptg.AttrPtg) AddPtg(org.apache.poi.ss.formula.ptg.AddPtg) MultiplyPtg(org.apache.poi.ss.formula.ptg.MultiplyPtg) StringPtg(org.apache.poi.ss.formula.ptg.StringPtg) LessThanPtg(org.apache.poi.ss.formula.ptg.LessThanPtg) BoolPtg(org.apache.poi.ss.formula.ptg.BoolPtg) BoolPtg(org.apache.poi.ss.formula.ptg.BoolPtg) StringPtg(org.apache.poi.ss.formula.ptg.StringPtg) AttrPtg(org.apache.poi.ss.formula.ptg.AttrPtg)

Example 8 with AttrPtg

use of org.apache.poi.ss.formula.ptg.AttrPtg in project poi by apache.

the class TestFormulaParserIf method testIfSingleCondition.

public void testIfSingleCondition() {
    Ptg[] ptgs = parseFormula("IF(1=1,10)");
    assertEquals(7, ptgs.length);
    assertTrue("IF Attr set correctly", (ptgs[3] instanceof AttrPtg));
    AttrPtg ifFunc = (AttrPtg) ptgs[3];
    assertTrue("It is not an if", ifFunc.isOptimizedIf());
    assertTrue("Single Value is not an IntPtg", (ptgs[4] instanceof IntPtg));
    IntPtg intPtg = (IntPtg) ptgs[4];
    assertEquals("Result", (short) 10, intPtg.getValue());
    assertTrue("Ptg is not a Variable Function", (ptgs[6] instanceof FuncVarPtg));
    FuncVarPtg funcPtg = (FuncVarPtg) ptgs[6];
    assertEquals("Arguments", 2, funcPtg.getNumberOfOperands());
}
Also used : IntPtg(org.apache.poi.ss.formula.ptg.IntPtg) IntPtg(org.apache.poi.ss.formula.ptg.IntPtg) Ptg(org.apache.poi.ss.formula.ptg.Ptg) LessEqualPtg(org.apache.poi.ss.formula.ptg.LessEqualPtg) FuncVarPtg(org.apache.poi.ss.formula.ptg.FuncVarPtg) RefPtg(org.apache.poi.ss.formula.ptg.RefPtg) NotEqualPtg(org.apache.poi.ss.formula.ptg.NotEqualPtg) FuncPtg(org.apache.poi.ss.formula.ptg.FuncPtg) AttrPtg(org.apache.poi.ss.formula.ptg.AttrPtg) AddPtg(org.apache.poi.ss.formula.ptg.AddPtg) MultiplyPtg(org.apache.poi.ss.formula.ptg.MultiplyPtg) StringPtg(org.apache.poi.ss.formula.ptg.StringPtg) LessThanPtg(org.apache.poi.ss.formula.ptg.LessThanPtg) BoolPtg(org.apache.poi.ss.formula.ptg.BoolPtg) FuncVarPtg(org.apache.poi.ss.formula.ptg.FuncVarPtg) AttrPtg(org.apache.poi.ss.formula.ptg.AttrPtg)

Example 9 with AttrPtg

use of org.apache.poi.ss.formula.ptg.AttrPtg in project poi by apache.

the class TestFormulaParser method testSpaceAtStartOfFormula.

@Test
public void testSpaceAtStartOfFormula() {
    // Simulating cell formula of "= 4" (note space)
    // The same Ptg array can be observed if an excel file is saved with that exact formula
    AttrPtg spacePtg = AttrPtg.createSpace(AttrPtg.SpaceType.SPACE_BEFORE, 1);
    Ptg[] ptgs = { spacePtg, new IntPtg(4) };
    String formulaString;
    try {
        formulaString = toFormulaString(ptgs);
    } catch (IllegalStateException e) {
        if (e.getMessage().equalsIgnoreCase("too much stuff left on the stack")) {
            fail("Identified bug 44609");
        }
        // else some unexpected error
        throw e;
    }
    // FormulaParser strips spaces anyway
    assertEquals("4", formulaString);
    ptgs = new Ptg[] { new IntPtg(3), spacePtg, new IntPtg(4), spacePtg, AddPtg.instance };
    formulaString = toFormulaString(ptgs);
    assertEquals("3+4", formulaString);
}
Also used : IntPtg(org.apache.poi.ss.formula.ptg.IntPtg) NumberPtg(org.apache.poi.ss.formula.ptg.NumberPtg) ArrayPtg(org.apache.poi.ss.formula.ptg.ArrayPtg) AttrPtg(org.apache.poi.ss.formula.ptg.AttrPtg) PercentPtg(org.apache.poi.ss.formula.ptg.PercentPtg) RangePtg(org.apache.poi.ss.formula.ptg.RangePtg) AddPtg(org.apache.poi.ss.formula.ptg.AddPtg) EqualPtg(org.apache.poi.ss.formula.ptg.EqualPtg) UnaryMinusPtg(org.apache.poi.ss.formula.ptg.UnaryMinusPtg) NameXPtg(org.apache.poi.ss.formula.ptg.NameXPtg) RefPtg(org.apache.poi.ss.formula.ptg.RefPtg) DividePtg(org.apache.poi.ss.formula.ptg.DividePtg) GreaterThanPtg(org.apache.poi.ss.formula.ptg.GreaterThanPtg) MultiplyPtg(org.apache.poi.ss.formula.ptg.MultiplyPtg) Ref3DPtg(org.apache.poi.ss.formula.ptg.Ref3DPtg) StringPtg(org.apache.poi.ss.formula.ptg.StringPtg) ErrPtg(org.apache.poi.ss.formula.ptg.ErrPtg) Ptg(org.apache.poi.ss.formula.ptg.Ptg) Area3DPtg(org.apache.poi.ss.formula.ptg.Area3DPtg) NamePtg(org.apache.poi.ss.formula.ptg.NamePtg) MemAreaPtg(org.apache.poi.ss.formula.ptg.MemAreaPtg) ConcatPtg(org.apache.poi.ss.formula.ptg.ConcatPtg) UnaryPlusPtg(org.apache.poi.ss.formula.ptg.UnaryPlusPtg) BoolPtg(org.apache.poi.ss.formula.ptg.BoolPtg) IntersectionPtg(org.apache.poi.ss.formula.ptg.IntersectionPtg) AbstractFunctionPtg(org.apache.poi.ss.formula.ptg.AbstractFunctionPtg) IntPtg(org.apache.poi.ss.formula.ptg.IntPtg) UnionPtg(org.apache.poi.ss.formula.ptg.UnionPtg) FuncVarPtg(org.apache.poi.ss.formula.ptg.FuncVarPtg) SubtractPtg(org.apache.poi.ss.formula.ptg.SubtractPtg) FuncPtg(org.apache.poi.ss.formula.ptg.FuncPtg) MissingArgPtg(org.apache.poi.ss.formula.ptg.MissingArgPtg) MemFuncPtg(org.apache.poi.ss.formula.ptg.MemFuncPtg) PowerPtg(org.apache.poi.ss.formula.ptg.PowerPtg) AreaPtg(org.apache.poi.ss.formula.ptg.AreaPtg) ParenthesisPtg(org.apache.poi.ss.formula.ptg.ParenthesisPtg) UnicodeString(org.apache.poi.hssf.record.common.UnicodeString) AttrPtg(org.apache.poi.ss.formula.ptg.AttrPtg) Test(org.junit.Test)

Aggregations

AttrPtg (org.apache.poi.ss.formula.ptg.AttrPtg)9 Ptg (org.apache.poi.ss.formula.ptg.Ptg)8 FuncPtg (org.apache.poi.ss.formula.ptg.FuncPtg)7 FuncVarPtg (org.apache.poi.ss.formula.ptg.FuncVarPtg)7 IntPtg (org.apache.poi.ss.formula.ptg.IntPtg)7 RefPtg (org.apache.poi.ss.formula.ptg.RefPtg)7 StringPtg (org.apache.poi.ss.formula.ptg.StringPtg)7 AddPtg (org.apache.poi.ss.formula.ptg.AddPtg)6 BoolPtg (org.apache.poi.ss.formula.ptg.BoolPtg)6 MultiplyPtg (org.apache.poi.ss.formula.ptg.MultiplyPtg)6 LessEqualPtg (org.apache.poi.ss.formula.ptg.LessEqualPtg)4 LessThanPtg (org.apache.poi.ss.formula.ptg.LessThanPtg)4 MemAreaPtg (org.apache.poi.ss.formula.ptg.MemAreaPtg)4 MemFuncPtg (org.apache.poi.ss.formula.ptg.MemFuncPtg)4 NotEqualPtg (org.apache.poi.ss.formula.ptg.NotEqualPtg)4 ParenthesisPtg (org.apache.poi.ss.formula.ptg.ParenthesisPtg)4 Area3DPtg (org.apache.poi.ss.formula.ptg.Area3DPtg)3 AreaPtg (org.apache.poi.ss.formula.ptg.AreaPtg)3 ErrPtg (org.apache.poi.ss.formula.ptg.ErrPtg)3 IntersectionPtg (org.apache.poi.ss.formula.ptg.IntersectionPtg)3