use of org.apache.poi.ss.formula.ptg.MemAreaPtg in project poi by apache.
the class TestFormulaParser method testComplexExplicitRangeEncodings.
/**
* Checks that the area-ref and explicit range operators get the right associativity
* and that the {@link MemFuncPtg} / {@link MemAreaPtg} is added correctly
*/
@Test
public void testComplexExplicitRangeEncodings() {
Ptg[] ptgs;
ptgs = parseFormula("SUM(OFFSET(A1,0,0):B2:C3:D4:E5:OFFSET(F6,1,1):G7)");
confirmTokenClasses(ptgs, // len 57
MemFuncPtg.class, // [A1]
RefPtg.class, // [0]
IntPtg.class, // [0]
IntPtg.class, // [OFFSET nArgs=3]
FuncVarPtg.class, // [B2:C3]
AreaPtg.class, RangePtg.class, // [D4:E5]
AreaPtg.class, RangePtg.class, // [F6]
RefPtg.class, // [1]
IntPtg.class, // [1]
IntPtg.class, // [OFFSET nArgs=3]
FuncVarPtg.class, RangePtg.class, // [G7]
RefPtg.class, RangePtg.class, // [sum ]
AttrPtg.class);
MemFuncPtg mf = (MemFuncPtg) ptgs[0];
assertEquals(57, mf.getLenRefSubexpression());
assertEquals("D4:E5", ((AreaPtgBase) ptgs[7]).toFormulaString());
assertTrue(((AttrPtg) ptgs[16]).isSum());
ptgs = parseFormula("SUM(A1:B2:C3:D4)");
confirmTokenClasses(ptgs, // len 19
MemAreaPtg.class, // [A1:B2]
AreaPtg.class, // [C3:D4]
AreaPtg.class, RangePtg.class, // [sum ]
AttrPtg.class);
MemAreaPtg ma = (MemAreaPtg) ptgs[0];
assertEquals(19, ma.getLenRefSubexpression());
}
use of org.apache.poi.ss.formula.ptg.MemAreaPtg 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;
}
use of org.apache.poi.ss.formula.ptg.MemAreaPtg in project poi by apache.
the class OperandClassTransformer method transformNode.
/**
* @param callerForceArrayFlag <code>true</code> if one of the current node's parents is a
* function Ptg which has been changed from default 'V' to 'A' type (due to requirements on
* the function return value).
*/
private void transformNode(ParseNode node, byte desiredOperandClass, boolean callerForceArrayFlag) {
Ptg token = node.getToken();
ParseNode[] children = node.getChildren();
boolean isSimpleValueFunc = isSimpleValueFunction(token);
if (isSimpleValueFunc) {
boolean localForceArray = desiredOperandClass == Ptg.CLASS_ARRAY;
for (int i = 0; i < children.length; i++) {
transformNode(children[i], desiredOperandClass, localForceArray);
}
setSimpleValueFuncClass((AbstractFunctionPtg) token, desiredOperandClass, callerForceArrayFlag);
return;
}
if (isSingleArgSum(token)) {
// Need to process the argument of SUM with transformFunctionNode below
// so make a dummy FuncVarPtg for that call.
token = FuncVarPtg.SUM;
// Note - the tAttrSum token (node.getToken()) is a base
// token so does not need to have its operand class set
}
if (token instanceof ValueOperatorPtg || token instanceof ControlPtg || token instanceof MemFuncPtg || token instanceof MemAreaPtg || token instanceof UnionPtg || token instanceof IntersectionPtg) {
// Value Operator Ptgs and Control are base tokens, so token will be unchanged
// but any child nodes are processed according to desiredOperandClass and callerForceArrayFlag
// As per OOO documentation Sec 3.2.4 "Token Class Transformation", "Step 1"
// All direct operands of value operators that are initially 'R' type will
// be converted to 'V' type.
byte localDesiredOperandClass = desiredOperandClass == Ptg.CLASS_REF ? Ptg.CLASS_VALUE : desiredOperandClass;
for (int i = 0; i < children.length; i++) {
transformNode(children[i], localDesiredOperandClass, callerForceArrayFlag);
}
return;
}
if (token instanceof AbstractFunctionPtg) {
transformFunctionNode((AbstractFunctionPtg) token, children, desiredOperandClass, callerForceArrayFlag);
return;
}
if (children.length > 0) {
if (token == RangePtg.instance) {
// TODO is any token transformation required under the various ref operators?
return;
}
throw new IllegalStateException("Node should not have any children");
}
if (token.isBaseToken()) {
// nothing to do
return;
}
token.setClass(transformClass(token.getPtgClass(), desiredOperandClass, callerForceArrayFlag));
}
Aggregations