List of usage examples for org.apache.poi.ss.formula.ptg RefPtgBase getRow
public final int getRow()
From source file:com.blackducksoftware.tools.commonframework.standard.protex.report.template.TemplateWriter.java
License:Apache License
/** * Handles copying the formula for the provided sheet and active row * // ww w .j av a 2 s . c o m * @param sheet * the provided sheet * @param targetCell * the target cell to copy the formula * @param activeRow * the active row * @param column * the TemplateColumn to be used for */ private void copyFormula(Sheet sheet, Cell targetCell, Row activeRow, TemplateColumn column) { if (targetCell == null || sheet == null || targetCell.getCellType() != Cell.CELL_TYPE_FORMULA) { return; } String formula = column.getCellFormula(); int shiftRows = activeRow.getRowNum() - 1; int shiftCols = 0; XSSFEvaluationWorkbook workbookWrapper = XSSFEvaluationWorkbook.create((XSSFWorkbook) sheet.getWorkbook()); Ptg[] ptgs = FormulaParser.parse(formula, workbookWrapper, FormulaType.CELL, sheet.getWorkbook().getSheetIndex(sheet)); for (Ptg ptg : ptgs) { if (ptg instanceof RefPtgBase) { RefPtgBase ref = (RefPtgBase) ptg; if (ref.isColRelative()) { ref.setColumn(ref.getColumn() + shiftCols); } if (ref.isRowRelative()) { ref.setRow(ref.getRow() + shiftRows); } } else if (ptg instanceof AreaPtg) { AreaPtg ref = (AreaPtg) ptg; if (ref.isFirstColRelative()) { ref.setFirstColumn(ref.getFirstColumn() + shiftCols); } if (ref.isLastColRelative()) { ref.setLastColumn(ref.getLastColumn() + shiftCols); } if (ref.isFirstRowRelative()) { ref.setFirstRow(ref.getFirstRow() + shiftRows); } if (ref.isLastRowRelative()) { ref.setLastRow(ref.getLastRow() + shiftRows); } } } formula = FormulaRenderer.toFormulaString(workbookWrapper, ptgs); targetCell.setCellFormula(formula); log.debug("Set Formula for row " + activeRow.getRowNum() + " : " + formula); targetCell.setAsActiveCell(); }
From source file:com.vaadin.addon.spreadsheet.ConditionalFormatter.java
/** * Checks if the formula in the given rule evaluates to <code>true</code>. * <p>/*from ww w. java 2 s .c o m*/ * * NOTE: Does not support HSSF files currently. * * @param rule * Conditional formatting rule to get the formula from * @return True if the formula in the given rule is of boolean formula type * and evaluates to <code>true</code>, false otherwise */ protected boolean matchesFormula(ConditionalFormattingRule rule, int deltaColumn, int deltaRow) { if (!(rule instanceof XSSFConditionalFormattingRule)) { // TODO Does not support HSSF files for now, since HSSF does not // read cell references in the file correctly.Since HSSF formulas // are read completely wrong, that boolean formula above is useless. return false; } String booleanFormula = rule.getFormula1(); if (booleanFormula == null || booleanFormula.isEmpty()) { return false; } // Parse formula and use deltas to get relative cell references to work // (#18702) Ptg[] ptgs = FormulaParser.parse(booleanFormula, WorkbookEvaluatorUtil.getEvaluationWorkbook(spreadsheet), FormulaType.CELL, spreadsheet.getActiveSheetIndex()); for (Ptg ptg : ptgs) { // base class for cell reference "things" if (ptg instanceof RefPtgBase) { RefPtgBase ref = (RefPtgBase) ptg; // re-calculate cell references if (ref.isColRelative()) { ref.setColumn(ref.getColumn() + deltaColumn); } if (ref.isRowRelative()) { ref.setRow(ref.getRow() + deltaRow); } } } ValueEval eval; try { eval = WorkbookEvaluatorUtil.evaluate(spreadsheet, ptgs); } catch (NotImplementedException e) { LOGGER.log(Level.FINEST, e.getMessage(), e); return false; } if (eval instanceof BoolEval) { return eval == null ? false : ((BoolEval) eval).getBooleanValue(); } else { return false; } }