Search in sources :

Example 76 with CellReference

use of org.apache.poi.ss.util.CellReference in project poi by apache.

the class WorkbookEvaluator method evaluateAny.

/**
     * @return never <code>null</code>, never {@link BlankEval}
     */
private ValueEval evaluateAny(EvaluationCell srcCell, int sheetIndex, int rowIndex, int columnIndex, EvaluationTracker tracker) {
    // avoid tracking dependencies to cells that have constant definition
    boolean shouldCellDependencyBeRecorded = _stabilityClassifier == null ? true : !_stabilityClassifier.isCellFinal(sheetIndex, rowIndex, columnIndex);
    if (srcCell == null || srcCell.getCellTypeEnum() != CellType.FORMULA) {
        ValueEval result = getValueFromNonFormulaCell(srcCell);
        if (shouldCellDependencyBeRecorded) {
            tracker.acceptPlainValueDependency(_workbookIx, sheetIndex, rowIndex, columnIndex, result);
        }
        return result;
    }
    FormulaCellCacheEntry cce = _cache.getOrCreateFormulaCellEntry(srcCell);
    if (shouldCellDependencyBeRecorded || cce.isInputSensitive()) {
        tracker.acceptFormulaDependency(cce);
    }
    IEvaluationListener evalListener = _evaluationListener;
    ValueEval result;
    if (cce.getValue() == null) {
        if (!tracker.startEvaluate(cce)) {
            return ErrorEval.CIRCULAR_REF_ERROR;
        }
        OperationEvaluationContext ec = new OperationEvaluationContext(this, _workbook, sheetIndex, rowIndex, columnIndex, tracker);
        try {
            Ptg[] ptgs = _workbook.getFormulaTokens(srcCell);
            if (evalListener == null) {
                result = evaluateFormula(ec, ptgs);
            } else {
                evalListener.onStartEvaluate(srcCell, cce);
                result = evaluateFormula(ec, ptgs);
                evalListener.onEndEvaluate(cce, result);
            }
            tracker.updateCacheResult(result);
        } catch (NotImplementedException e) {
            throw addExceptionInfo(e, sheetIndex, rowIndex, columnIndex);
        } catch (RuntimeException re) {
            if (re.getCause() instanceof WorkbookNotFoundException && _ignoreMissingWorkbooks) {
                logInfo(re.getCause().getMessage() + " - Continuing with cached value!");
                switch(srcCell.getCachedFormulaResultTypeEnum()) {
                    case NUMERIC:
                        result = new NumberEval(srcCell.getNumericCellValue());
                        break;
                    case STRING:
                        result = new StringEval(srcCell.getStringCellValue());
                        break;
                    case BLANK:
                        result = BlankEval.instance;
                        break;
                    case BOOLEAN:
                        result = BoolEval.valueOf(srcCell.getBooleanCellValue());
                        break;
                    case ERROR:
                        result = ErrorEval.valueOf(srcCell.getErrorCellValue());
                        break;
                    case FORMULA:
                    default:
                        throw new RuntimeException("Unexpected cell type '" + srcCell.getCellTypeEnum() + "' found!");
                }
            } else {
                throw re;
            }
        } finally {
            tracker.endEvaluate(cce);
        }
    } else {
        if (evalListener != null) {
            evalListener.onCacheHit(sheetIndex, rowIndex, columnIndex, cce.getValue());
        }
        return cce.getValue();
    }
    if (isDebugLogEnabled()) {
        String sheetName = getSheetName(sheetIndex);
        CellReference cr = new CellReference(rowIndex, columnIndex);
        logDebug("Evaluated " + sheetName + "!" + cr.formatAsString() + " to " + result);
    }
    // the top evaluation frame
    return result;
}
Also used : WorkbookNotFoundException(org.apache.poi.ss.formula.CollaboratingWorkbooksEnvironment.WorkbookNotFoundException) CellReference(org.apache.poi.ss.util.CellReference)

Example 77 with CellReference

use of org.apache.poi.ss.util.CellReference in project poi by apache.

the class XSSFTable method getRowCount.

/**
     * @return the total number of rows in the selection. (Note: in this version autofiltering is ignored)
     * Returns <code>0</code> if the start or end cell references are not set.
     * 
     * Does not track updates to underlying changes to CTTable
     * To synchronize with changes to the underlying CTTable,
     * call {@link #updateReferences()}.
     */
public int getRowCount() {
    CellReference from = getStartCellReference();
    CellReference to = getEndCellReference();
    int rowCount = 0;
    if (from != null && to != null) {
        rowCount = to.getRow() - from.getRow() + 1;
    }
    return rowCount;
}
Also used : CellReference(org.apache.poi.ss.util.CellReference)

Example 78 with CellReference

use of org.apache.poi.ss.util.CellReference in project poi by apache.

the class XSSFTable method setCellReferences.

/**
     * @since POI 3.15 beta 3
     */
private void setCellReferences() {
    String ref = ctTable.getRef();
    if (ref != null) {
        String[] boundaries = ref.split(":", 2);
        String from = boundaries[0];
        String to = boundaries[1];
        startCellReference = new CellReference(from);
        endCellReference = new CellReference(to);
    }
}
Also used : CellReference(org.apache.poi.ss.util.CellReference)

Example 79 with CellReference

use of org.apache.poi.ss.util.CellReference in project poi by apache.

the class XSSFTable method updateHeaders.

/**
     * Synchronize table headers with cell values in the parent sheet.
     * Headers <em>must</em> be in sync, otherwise Excel will display a
     * "Found unreadable content" message on startup.
     * 
     * If calling both {@link #updateReferences()} and
     * {@link #updateHeaders()}, {@link #updateReferences()}
     * should be called first.
     */
public void updateHeaders() {
    XSSFSheet sheet = (XSSFSheet) getParent();
    CellReference ref = getStartCellReference();
    if (ref == null)
        return;
    int headerRow = ref.getRow();
    int firstHeaderColumn = ref.getCol();
    XSSFRow row = sheet.getRow(headerRow);
    if (row != null && row.getCTRow().validate()) {
        int cellnum = firstHeaderColumn;
        for (CTTableColumn col : getCTTable().getTableColumns().getTableColumnArray()) {
            XSSFCell cell = row.getCell(cellnum);
            if (cell != null) {
                col.setName(cell.getStringCellValue());
            }
            cellnum++;
        }
        ctColumns = null;
        columnMap = null;
        xmlColumnPr = null;
        commonXPath = null;
    }
}
Also used : CellReference(org.apache.poi.ss.util.CellReference) CTTableColumn(org.openxmlformats.schemas.spreadsheetml.x2006.main.CTTableColumn)

Example 80 with CellReference

use of org.apache.poi.ss.util.CellReference in project poi by apache.

the class TestXSSFComment method getSetRow.

@Test
public void getSetRow() {
    CommentsTable sheetComments = new CommentsTable();
    XSSFVMLDrawing vml = new XSSFVMLDrawing();
    CTComment ctComment = sheetComments.newComment(CellAddress.A1);
    CTShape vmlShape = vml.newCommentShape();
    XSSFComment comment = new XSSFComment(sheetComments, ctComment, vmlShape);
    comment.setRow(1);
    assertEquals(1, comment.getRow());
    assertEquals(1, new CellReference(ctComment.getRef()).getRow());
    assertEquals(1, vmlShape.getClientDataArray(0).getRowArray(0).intValue());
    comment.setRow(5);
    assertEquals(5, comment.getRow());
    assertEquals(5, new CellReference(ctComment.getRef()).getRow());
    assertEquals(5, vmlShape.getClientDataArray(0).getRowArray(0).intValue());
}
Also used : CTComment(org.openxmlformats.schemas.spreadsheetml.x2006.main.CTComment) CTShape(com.microsoft.schemas.vml.CTShape) CellReference(org.apache.poi.ss.util.CellReference) CommentsTable(org.apache.poi.xssf.model.CommentsTable) Test(org.junit.Test)

Aggregations

CellReference (org.apache.poi.ss.util.CellReference)125 Test (org.junit.Test)52 Cell (org.apache.poi.ss.usermodel.Cell)28 Row (org.apache.poi.ss.usermodel.Row)27 AreaReference (org.apache.poi.ss.util.AreaReference)20 Sheet (org.apache.poi.ss.usermodel.Sheet)18 Workbook (org.apache.poi.ss.usermodel.Workbook)14 CellRangeAddress (org.apache.poi.ss.util.CellRangeAddress)14 HSSFWorkbook (org.apache.poi.hssf.usermodel.HSSFWorkbook)13 SXSSFWorkbook (org.apache.poi.xssf.streaming.SXSSFWorkbook)12 XSSFWorkbook (org.apache.poi.xssf.usermodel.XSSFWorkbook)11 FormulaEvaluator (org.apache.poi.ss.usermodel.FormulaEvaluator)8 XSSFSheet (org.apache.poi.xssf.usermodel.XSSFSheet)8 CellStyle (org.apache.poi.ss.usermodel.CellStyle)7 ArrayList (java.util.ArrayList)6 IOException (java.io.IOException)5 HSSFSheet (org.apache.poi.hssf.usermodel.HSSFSheet)5 RichTextString (org.apache.poi.ss.usermodel.RichTextString)5 FileOutputStream (java.io.FileOutputStream)4 OutputStream (java.io.OutputStream)4