Search in sources :

Example 1 with OperationPtg

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

the class FormulaParser method isValidRangeOperand.

/**
     * @return <code>false</code> if sub-expression represented the specified ParseNode definitely
     * cannot appear on either side of the range (':') operator
     */
private static boolean isValidRangeOperand(ParseNode a) {
    Ptg tkn = a.getToken();
    // Note - order is important for these instance-of checks
    if (tkn instanceof OperandPtg) {
        // notably cell refs and area refs
        return true;
    }
    // next 2 are special cases of OperationPtg
    if (tkn instanceof AbstractFunctionPtg) {
        AbstractFunctionPtg afp = (AbstractFunctionPtg) tkn;
        byte returnClass = afp.getDefaultOperandClass();
        return Ptg.CLASS_REF == returnClass;
    }
    if (tkn instanceof ValueOperatorPtg) {
        return false;
    }
    if (tkn instanceof OperationPtg) {
        return true;
    }
    // one special case of ControlPtg
    if (tkn instanceof ParenthesisPtg) {
        // parenthesis Ptg should have only one child
        return isValidRangeOperand(a.getChildren()[0]);
    }
    // one special case of ScalarConstantPtg
    if (tkn == ErrPtg.REF_INVALID) {
        return true;
    }
    // All other ControlPtgs and ScalarConstantPtgs cannot be used with ':'
    return false;
}
Also used : OperandPtg(org.apache.poi.ss.formula.ptg.OperandPtg) 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) StringPtg(org.apache.poi.ss.formula.ptg.StringPtg) ErrPtg(org.apache.poi.ss.formula.ptg.ErrPtg) Ptg(org.apache.poi.ss.formula.ptg.Ptg) NamePtg(org.apache.poi.ss.formula.ptg.NamePtg) MemAreaPtg(org.apache.poi.ss.formula.ptg.MemAreaPtg) NotEqualPtg(org.apache.poi.ss.formula.ptg.NotEqualPtg) ValueOperatorPtg(org.apache.poi.ss.formula.ptg.ValueOperatorPtg) ConcatPtg(org.apache.poi.ss.formula.ptg.ConcatPtg) UnaryPlusPtg(org.apache.poi.ss.formula.ptg.UnaryPlusPtg) GreaterEqualPtg(org.apache.poi.ss.formula.ptg.GreaterEqualPtg) LessThanPtg(org.apache.poi.ss.formula.ptg.LessThanPtg) 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) LessEqualPtg(org.apache.poi.ss.formula.ptg.LessEqualPtg) 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) OperandPtg(org.apache.poi.ss.formula.ptg.OperandPtg) MissingArgPtg(org.apache.poi.ss.formula.ptg.MissingArgPtg) MemFuncPtg(org.apache.poi.ss.formula.ptg.MemFuncPtg) OperationPtg(org.apache.poi.ss.formula.ptg.OperationPtg) PowerPtg(org.apache.poi.ss.formula.ptg.PowerPtg) AreaPtg(org.apache.poi.ss.formula.ptg.AreaPtg) ParenthesisPtg(org.apache.poi.ss.formula.ptg.ParenthesisPtg) OperationPtg(org.apache.poi.ss.formula.ptg.OperationPtg) ParenthesisPtg(org.apache.poi.ss.formula.ptg.ParenthesisPtg) AbstractFunctionPtg(org.apache.poi.ss.formula.ptg.AbstractFunctionPtg) ValueOperatorPtg(org.apache.poi.ss.formula.ptg.ValueOperatorPtg)

Example 2 with OperationPtg

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

the class FormulaRenderer method toFormulaString.

/**
     * Static method to convert an array of {@link Ptg}s in RPN order
     * to a human readable string format in infix mode.
     * @param book  used for defined names and 3D references
     * @param ptgs  must not be <code>null</code>
     * @return a human readable String
     */
public static String toFormulaString(FormulaRenderingWorkbook book, Ptg[] ptgs) {
    if (ptgs == null || ptgs.length == 0) {
        throw new IllegalArgumentException("ptgs must not be null");
    }
    Stack<String> stack = new Stack<String>();
    for (Ptg ptg : ptgs) {
        // TODO - what about MemNoMemPtg?
        if (ptg instanceof MemAreaPtg || ptg instanceof MemFuncPtg || ptg instanceof MemErrPtg) {
            // TODO - put comment and throw exception in toFormulaString() of these classes
            continue;
        }
        if (ptg instanceof ParenthesisPtg) {
            String contents = stack.pop();
            stack.push("(" + contents + ")");
            continue;
        }
        if (ptg instanceof AttrPtg) {
            AttrPtg attrPtg = ((AttrPtg) ptg);
            if (attrPtg.isOptimizedIf() || attrPtg.isOptimizedChoose() || attrPtg.isSkip()) {
                continue;
            }
            if (attrPtg.isSpace()) {
                // POI currently doesn't render spaces in formulas
                continue;
            // but if it ever did, care must be taken:
            // tAttrSpace comes *before* the operand it applies to, which may be consistent
            // with how the formula text appears but is against the RPN ordering assumed here
            }
            if (attrPtg.isSemiVolatile()) {
                // similar to tAttrSpace - RPN is violated
                continue;
            }
            if (attrPtg.isSum()) {
                String[] operands = getOperands(stack, attrPtg.getNumberOfOperands());
                stack.push(attrPtg.toFormulaString(operands));
                continue;
            }
            throw new RuntimeException("Unexpected tAttr: " + attrPtg);
        }
        if (ptg instanceof WorkbookDependentFormula) {
            WorkbookDependentFormula optg = (WorkbookDependentFormula) ptg;
            stack.push(optg.toFormulaString(book));
            continue;
        }
        if (!(ptg instanceof OperationPtg)) {
            stack.push(ptg.toFormulaString());
            continue;
        }
        OperationPtg o = (OperationPtg) ptg;
        String[] operands = getOperands(stack, o.getNumberOfOperands());
        stack.push(o.toFormulaString(operands));
    }
    if (stack.isEmpty()) {
        // stack.push(). So this is either an internal error or impossible.
        throw new IllegalStateException("Stack underflow");
    }
    String result = stack.pop();
    if (!stack.isEmpty()) {
        // put anything on the stack
        throw new IllegalStateException("too much stuff left on the stack");
    }
    return result;
}
Also used : AttrPtg(org.apache.poi.ss.formula.ptg.AttrPtg) Ptg(org.apache.poi.ss.formula.ptg.Ptg) MemAreaPtg(org.apache.poi.ss.formula.ptg.MemAreaPtg) MemErrPtg(org.apache.poi.ss.formula.ptg.MemErrPtg) MemFuncPtg(org.apache.poi.ss.formula.ptg.MemFuncPtg) OperationPtg(org.apache.poi.ss.formula.ptg.OperationPtg) ParenthesisPtg(org.apache.poi.ss.formula.ptg.ParenthesisPtg) MemFuncPtg(org.apache.poi.ss.formula.ptg.MemFuncPtg) ParenthesisPtg(org.apache.poi.ss.formula.ptg.ParenthesisPtg) OperationPtg(org.apache.poi.ss.formula.ptg.OperationPtg) AttrPtg(org.apache.poi.ss.formula.ptg.AttrPtg) Stack(java.util.Stack) MemAreaPtg(org.apache.poi.ss.formula.ptg.MemAreaPtg) MemErrPtg(org.apache.poi.ss.formula.ptg.MemErrPtg)

Aggregations

AttrPtg (org.apache.poi.ss.formula.ptg.AttrPtg)2 MemAreaPtg (org.apache.poi.ss.formula.ptg.MemAreaPtg)2 MemFuncPtg (org.apache.poi.ss.formula.ptg.MemFuncPtg)2 OperationPtg (org.apache.poi.ss.formula.ptg.OperationPtg)2 ParenthesisPtg (org.apache.poi.ss.formula.ptg.ParenthesisPtg)2 Ptg (org.apache.poi.ss.formula.ptg.Ptg)2 Stack (java.util.Stack)1 AbstractFunctionPtg (org.apache.poi.ss.formula.ptg.AbstractFunctionPtg)1 AddPtg (org.apache.poi.ss.formula.ptg.AddPtg)1 AreaPtg (org.apache.poi.ss.formula.ptg.AreaPtg)1 ArrayPtg (org.apache.poi.ss.formula.ptg.ArrayPtg)1 BoolPtg (org.apache.poi.ss.formula.ptg.BoolPtg)1 ConcatPtg (org.apache.poi.ss.formula.ptg.ConcatPtg)1 DividePtg (org.apache.poi.ss.formula.ptg.DividePtg)1 EqualPtg (org.apache.poi.ss.formula.ptg.EqualPtg)1 ErrPtg (org.apache.poi.ss.formula.ptg.ErrPtg)1 FuncPtg (org.apache.poi.ss.formula.ptg.FuncPtg)1 FuncVarPtg (org.apache.poi.ss.formula.ptg.FuncVarPtg)1 GreaterEqualPtg (org.apache.poi.ss.formula.ptg.GreaterEqualPtg)1 GreaterThanPtg (org.apache.poi.ss.formula.ptg.GreaterThanPtg)1