Example usage for org.apache.poi.ss.formula FormulaRenderer toFormulaString

List of usage examples for org.apache.poi.ss.formula FormulaRenderer toFormulaString

Introduction

In this page you can find the example usage for org.apache.poi.ss.formula FormulaRenderer toFormulaString.

Prototype

public static String toFormulaString(FormulaRenderingWorkbook book, Ptg[] ptgs) 

Source Link

Document

Static method to convert an array of Ptg s in RPN order to a human readable string format in infix mode.

Usage

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
 * //from  w w w  .  j  a va 2  s  . com
 * @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:org.tiefaces.components.websheet.utility.ConfigurationUtility.java

License:MIT License

/**
 * Builds the cell formula for shifted rows.
 *
 * @param sheet//  w  ww. j  ava  2  s . c om
 *            the sheet
 * @param wbWrapper
 *            the wb wrapper
 * @param shiftFormulaRef
 *            the shift formula ref
 * @param cell
 *            the cell
 * @param originFormula
 *            the origin formula
 */
public static void buildCellFormulaForShiftedRows(final Sheet sheet, final XSSFEvaluationWorkbook wbWrapper,
        final ShiftFormulaRef shiftFormulaRef, final Cell cell, final String originFormula) {
    // only shift when there's watchlist exist.
    if ((shiftFormulaRef.getWatchList() != null) && (!shiftFormulaRef.getWatchList().isEmpty())) {
        Ptg[] ptgs = FormulaParser.parse(originFormula, wbWrapper, FormulaType.CELL,
                sheet.getWorkbook().getSheetIndex(sheet));
        Ptg[] convertedFormulaPtg = ShiftFormulaUtility.convertSharedFormulas(ptgs, shiftFormulaRef);
        if (shiftFormulaRef.getFormulaChanged() > 0) {
            // only change formula when indicator is true
            cell.setCellFormula(FormulaRenderer.toFormulaString(wbWrapper, convertedFormulaPtg));

        }
    }
}