List of usage examples for org.apache.poi.ss.formula.ptg AttrPtg isSum
public boolean isSum()
From source file:org.jreserve.gui.poi.read.xls.XlsReferenceUtilReader.java
License:Open Source License
private String toFormulaString(Ptg[] ptgs) { if (ptgs == null || ptgs.length == 0) return null; Stack<String> stack = new Stack<String>(); for (int i = 0; i < ptgs.length; i++) { Ptg ptg = ptgs[i];/*from ww w. jav a 2s.c o m*/ if (ptg instanceof MemAreaPtg || ptg instanceof MemFuncPtg || ptg instanceof MemErrPtg) 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() || attrPtg.isSemiVolatile()) continue; if (attrPtg.isSum()) { String[] operands = getOperands(stack, attrPtg.getNumberOfOperands()); stack.push(attrPtg.toFormulaString(operands)); continue; } throw new RuntimeException("Unexpected tAttr: " + attrPtg.toString()); } if (ptg instanceof WorkbookDependentFormula) { WorkbookDependentFormula optg = (WorkbookDependentFormula) ptg; stack.push(optg.toFormulaString(renderer)); 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()) throw new IllegalStateException("Stack underflow"); String result = stack.pop(); if (!stack.isEmpty()) throw new IllegalStateException("too much stuff left on the stack"); return result; }
From source file:org.tiefaces.components.websheet.utility.ShiftFormulaUtility.java
License:MIT License
/** * Convert ptg./*w ww . j av a 2 s.c o m*/ * * @param ptgs * the ptgs * @param position * the position * @param shiftFormulaRef * the shift formula ref * @param ptg * the ptg * @return the ptg[] */ private static Ptg[] convertPtg(final Ptg[] ptgs, final int position, final ShiftFormulaRef shiftFormulaRef, final Object ptg) { byte originalOperandClass = -1; if (!((Ptg) ptg).isBaseToken()) { originalOperandClass = ((Ptg) ptg).getPtgClass(); } int currentRow; currentRow = getFirstSupportedRowNumFromPtg(ptg); if ((currentRow >= 0) && shiftFormulaRef.getWatchList().contains(currentRow)) { return convertPtgForWatchList(ptgs, position, shiftFormulaRef, ptg, originalOperandClass, currentRow); } // no need change ptg if ((ptg instanceof AttrPtg) && (shiftFormulaRef.getFormulaChanged() > 1)) { AttrPtg newPtg = (AttrPtg) ptg; if (newPtg.isSum()) { FuncVarPtg fptg = FuncVarPtg.create("sum", shiftFormulaRef.getFormulaChanged()); return singlePtg(fptg, fptg.getPtgClass(), shiftFormulaRef.getFormulaChanged()); } } return singlePtg(ptg, originalOperandClass, shiftFormulaRef.getFormulaChanged()); }
From source file:uk.ac.liverpool.spreadsheet.FormulaRenderer.java
License:Apache License
/** * 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/*from www . jav a2 s . c o m*/ */ 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 (int i = 0; i < ptgs.length; i++) { Ptg ptg = ptgs[i]; // TODO - what about MemNoMemPtg? if (ptg instanceof MemAreaPtg || ptg instanceof MemFuncPtg || ptg instanceof MemErrPtg) { // marks the start of a list of area expressions which will be naturally combined // by their trailing operators (e.g. UnionPtg) // 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.toString()); } if (ptg instanceof WorkbookDependentFormula) { WorkbookDependentFormula optg = (WorkbookDependentFormula) ptg; stack.push(optg.toFormulaString(book)); continue; } if (!(ptg instanceof OperationPtg)) { String s = ""; if (ptg instanceof AreaPtg) { AreaPtg a = (AreaPtg) ptg; s = formatReferenceAsString(a); } else if (ptg instanceof RefPtg) { RefPtg a = (RefPtg) ptg; CellReference cr = new CellReference(a.getRow(), a.getColumn(), !a.isRowRelative(), !a.isColRelative()); s = "[." + cr.formatAsString() + "]"; } else s = ptg.toFormulaString(); stack.push(s); continue; } OperationPtg o = (OperationPtg) ptg; String[] operands = getOperands(stack, o.getNumberOfOperands()); if (o instanceof AbstractFunctionPtg) { AbstractFunctionPtg a = (AbstractFunctionPtg) o; stack.push(toFormulaString(a, operands)); } else stack.push(o.toFormulaString(operands)); } if (stack.isEmpty()) { // inspection of the code above reveals that every stack.pop() is followed by a // stack.push(). So this is either an internal error or impossible. throw new IllegalStateException("Stack underflow"); } String result = stack.pop(); if (!stack.isEmpty()) { // Might be caused by some tokens like AttrPtg and Mem*Ptg, which really shouldn't // put anything on the stack throw new IllegalStateException("too much stuff left on the stack"); } return result; }