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));
}
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());
}
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());
}
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);
}
Aggregations