Search in sources :

Example 1 with WorkbookNotFoundException

use of org.apache.poi.ss.formula.CollaboratingWorkbooksEnvironment.WorkbookNotFoundException in project poi by apache.

the class OperationEvaluationContext method getExternalNameXEval.

private ValueEval getExternalNameXEval(ExternalName externName, String workbookName) {
    try {
        // Fetch the workbook this refers to, and the name as defined with that
        WorkbookEvaluator refWorkbookEvaluator = _bookEvaluator.getOtherWorkbookEvaluator(workbookName);
        EvaluationName evaluationName = refWorkbookEvaluator.getName(externName.getName(), externName.getIx() - 1);
        if (evaluationName != null && evaluationName.hasFormula()) {
            if (evaluationName.getNameDefinition().length > 1) {
                throw new RuntimeException("Complex name formulas not supported yet");
            }
            // Need to evaluate the reference in the context of the other book
            OperationEvaluationContext refWorkbookContext = new OperationEvaluationContext(refWorkbookEvaluator, refWorkbookEvaluator.getWorkbook(), -1, -1, -1, _tracker);
            Ptg ptg = evaluationName.getNameDefinition()[0];
            if (ptg instanceof Ref3DPtg) {
                Ref3DPtg ref3D = (Ref3DPtg) ptg;
                return refWorkbookContext.getRef3DEval(ref3D);
            } else if (ptg instanceof Ref3DPxg) {
                Ref3DPxg ref3D = (Ref3DPxg) ptg;
                return refWorkbookContext.getRef3DEval(ref3D);
            } else if (ptg instanceof Area3DPtg) {
                Area3DPtg area3D = (Area3DPtg) ptg;
                return refWorkbookContext.getArea3DEval(area3D);
            } else if (ptg instanceof Area3DPxg) {
                Area3DPxg area3D = (Area3DPxg) ptg;
                return refWorkbookContext.getArea3DEval(area3D);
            }
        }
        return ErrorEval.REF_INVALID;
    } catch (WorkbookNotFoundException wnfe) {
        return ErrorEval.REF_INVALID;
    }
}
Also used : Area3DPxg(org.apache.poi.ss.formula.ptg.Area3DPxg) NameXPtg(org.apache.poi.ss.formula.ptg.NameXPtg) Ptg(org.apache.poi.ss.formula.ptg.Ptg) Area3DPtg(org.apache.poi.ss.formula.ptg.Area3DPtg) Ref3DPtg(org.apache.poi.ss.formula.ptg.Ref3DPtg) Area3DPtg(org.apache.poi.ss.formula.ptg.Area3DPtg) Ref3DPxg(org.apache.poi.ss.formula.ptg.Ref3DPxg) WorkbookNotFoundException(org.apache.poi.ss.formula.CollaboratingWorkbooksEnvironment.WorkbookNotFoundException) Ref3DPtg(org.apache.poi.ss.formula.ptg.Ref3DPtg)

Example 2 with WorkbookNotFoundException

use of org.apache.poi.ss.formula.CollaboratingWorkbooksEnvironment.WorkbookNotFoundException 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)

Aggregations

WorkbookNotFoundException (org.apache.poi.ss.formula.CollaboratingWorkbooksEnvironment.WorkbookNotFoundException)2 Area3DPtg (org.apache.poi.ss.formula.ptg.Area3DPtg)1 Area3DPxg (org.apache.poi.ss.formula.ptg.Area3DPxg)1 NameXPtg (org.apache.poi.ss.formula.ptg.NameXPtg)1 Ptg (org.apache.poi.ss.formula.ptg.Ptg)1 Ref3DPtg (org.apache.poi.ss.formula.ptg.Ref3DPtg)1 Ref3DPxg (org.apache.poi.ss.formula.ptg.Ref3DPxg)1 CellReference (org.apache.poi.ss.util.CellReference)1