Search in sources :

Example 1 with NotImplementedException

use of org.apache.poi.ss.formula.eval.NotImplementedException in project poi by apache.

the class DStarRunner method fullfillsConditions.

/**
     * Checks a row in a database against a condition database.
     *
     * @param db Database.
     * @param row The row in the database to check.
     * @param cdb The condition database to use for checking.
     * @return Whether the row matches the conditions.
     * @throws EvaluationException If references could not be resolved or comparison
     * operators and operands didn't match.
     */
private static boolean fullfillsConditions(AreaEval db, int row, AreaEval cdb) throws EvaluationException {
    // Only one row must match to accept the input, so rows are ORed.
    // Each row is made up of cells where each cell is a condition,
    // all have to match, so they are ANDed.
    final int height = cdb.getHeight();
    for (int conditionRow = 1; conditionRow < height; ++conditionRow) {
        boolean matches = true;
        final int width = cdb.getWidth();
        for (int column = 0; column < width; ++column) {
            // columns are ANDed
            // Whether the condition column matches a database column, if not it's a
            // special column that accepts formulas.
            boolean columnCondition = true;
            ValueEval condition = null;
            // The condition to apply.
            condition = resolveReference(cdb, conditionRow, column);
            // If the condition is empty it matches.
            if (condition instanceof BlankEval)
                continue;
            // The column in the DB to apply the condition to.
            ValueEval targetHeader = resolveReference(cdb, 0, column);
            if (!(targetHeader instanceof StringValueEval)) {
                throw new EvaluationException(ErrorEval.VALUE_INVALID);
            }
            if (getColumnForName(targetHeader, db) == -1)
                // No column found, it's again a special column that accepts formulas.
                columnCondition = false;
            if (columnCondition == true) {
                // normal column condition
                // Should not throw, checked above.
                ValueEval value = resolveReference(db, row, getColumnForName(targetHeader, db));
                if (!testNormalCondition(value, condition)) {
                    matches = false;
                    break;
                }
            } else {
                // TODO: Check whether the condition cell contains a formula and return #VALUE! if it doesn't.
                if (OperandResolver.coerceValueToString(condition).isEmpty()) {
                    throw new EvaluationException(ErrorEval.VALUE_INVALID);
                }
                throw new NotImplementedException("D* function with formula conditions");
            }
        }
        if (matches == true) {
            return true;
        }
    }
    return false;
}
Also used : NotImplementedException(org.apache.poi.ss.formula.eval.NotImplementedException) NumericValueEval(org.apache.poi.ss.formula.eval.NumericValueEval) ValueEval(org.apache.poi.ss.formula.eval.ValueEval) StringValueEval(org.apache.poi.ss.formula.eval.StringValueEval) BlankEval(org.apache.poi.ss.formula.eval.BlankEval) EvaluationException(org.apache.poi.ss.formula.eval.EvaluationException) StringValueEval(org.apache.poi.ss.formula.eval.StringValueEval)

Example 2 with NotImplementedException

use of org.apache.poi.ss.formula.eval.NotImplementedException in project poi by apache.

the class BaseTestExternalFunctions method testExternalFunctions.

@Test
public void testExternalFunctions() throws IOException {
    Workbook wb = _testDataProvider.createWorkbook();
    FormulaEvaluator evaluator = wb.getCreationHelper().createFormulaEvaluator();
    Sheet sh = wb.createSheet();
    Cell cell1 = sh.createRow(0).createCell(0);
    // functions from the Excel Analysis Toolpack
    cell1.setCellFormula("ISODD(1)+ISEVEN(2)");
    assertEquals("ISODD(1)+ISEVEN(2)", cell1.getCellFormula());
    Cell cell2 = sh.createRow(1).createCell(0);
    // unregistered functions are parseable and renderable, but may not be evaluateable
    cell2.setCellFormula("MYFUNC(\"B1\")");
    try {
        evaluator.evaluate(cell2);
        fail("Expected NotImplementedFunctionException/NotImplementedException");
    } catch (final NotImplementedException e) {
        assertTrue(e.getCause() instanceof NotImplementedFunctionException);
    // Alternatively, a future implementation of evaluate could return #NAME? error to align behavior with Excel
    // assertEquals(ErrorEval.NAME_INVALID, ErrorEval.valueOf(evaluator.evaluate(cell2).getErrorValue()));
    }
    wb.addToolPack(customToolpack);
    cell2.setCellFormula("MYFUNC(\"B1\")");
    assertEquals("MYFUNC(\"B1\")", cell2.getCellFormula());
    Cell cell3 = sh.createRow(2).createCell(0);
    //where A2 is defined above
    cell3.setCellFormula("MYFUNC2(\"C1\")&\"-\"&A2");
    assertEquals("MYFUNC2(\"C1\")&\"-\"&A2", cell3.getCellFormula());
    assertEquals(2.0, evaluator.evaluate(cell1).getNumberValue(), 0);
    assertEquals("B1abc", evaluator.evaluate(cell2).getStringValue());
    assertEquals("C1abc2-B1abc", evaluator.evaluate(cell3).getStringValue());
    wb.close();
}
Also used : NotImplementedException(org.apache.poi.ss.formula.eval.NotImplementedException) NotImplementedFunctionException(org.apache.poi.ss.formula.eval.NotImplementedFunctionException) Sheet(org.apache.poi.ss.usermodel.Sheet) Cell(org.apache.poi.ss.usermodel.Cell) Workbook(org.apache.poi.ss.usermodel.Workbook) FormulaEvaluator(org.apache.poi.ss.usermodel.FormulaEvaluator) Test(org.junit.Test)

Example 3 with NotImplementedException

use of org.apache.poi.ss.formula.eval.NotImplementedException in project poi by apache.

the class TestFunctionRegistry method testRegisterInRuntime.

public void testRegisterInRuntime() {
    HSSFWorkbook wb = new HSSFWorkbook();
    HSSFSheet sheet = wb.createSheet("Sheet1");
    HSSFRow row = sheet.createRow(0);
    HSSFFormulaEvaluator fe = new HSSFFormulaEvaluator(wb);
    HSSFCell cellA = row.createCell(0);
    cellA.setCellFormula("FISHER(A5)");
    CellValue cv;
    try {
        cv = fe.evaluate(cellA);
        fail("expectecd exception");
    } catch (NotImplementedException e) {
    }
    FunctionEval.registerFunction("FISHER", new Function() {

        @Override
        public ValueEval evaluate(ValueEval[] args, int srcRowIndex, int srcColumnIndex) {
            return ErrorEval.NA;
        }
    });
    cv = fe.evaluate(cellA);
    assertEquals(ErrorEval.NA.getErrorCode(), cv.getErrorValue());
    HSSFCell cellB = row.createCell(1);
    cellB.setCellFormula("CUBEMEMBERPROPERTY(A5)");
    try {
        cv = fe.evaluate(cellB);
        fail("expectecd exception");
    } catch (NotImplementedException e) {
    }
    AnalysisToolPak.registerFunction("CUBEMEMBERPROPERTY", new FreeRefFunction() {

        @Override
        public ValueEval evaluate(ValueEval[] args, OperationEvaluationContext ec) {
            return ErrorEval.NUM_ERROR;
        }
    });
    cv = fe.evaluate(cellB);
    assertEquals(ErrorEval.NUM_ERROR.getErrorCode(), cv.getErrorValue());
}
Also used : NotImplementedException(org.apache.poi.ss.formula.eval.NotImplementedException) ValueEval(org.apache.poi.ss.formula.eval.ValueEval) FreeRefFunction(org.apache.poi.ss.formula.functions.FreeRefFunction) Function(org.apache.poi.ss.formula.functions.Function) FreeRefFunction(org.apache.poi.ss.formula.functions.FreeRefFunction) CellValue(org.apache.poi.ss.usermodel.CellValue)

Example 4 with NotImplementedException

use of org.apache.poi.ss.formula.eval.NotImplementedException in project poi by apache.

the class NumericFunctionInvoker method invokeInternal.

/**
	 * Formats nicer error messages for the junit output
	 */
private static double invokeInternal(Function target, ValueEval[] args, int srcCellRow, int srcCellCol) throws NumericEvalEx {
    ValueEval evalResult;
    try {
        evalResult = target.evaluate(args, srcCellRow, (short) srcCellCol);
    } catch (NotImplementedException e) {
        throw new NumericEvalEx("Not implemented:" + e.getMessage());
    }
    if (evalResult == null) {
        throw new NumericEvalEx("Result object was null");
    }
    if (evalResult instanceof ErrorEval) {
        ErrorEval ee = (ErrorEval) evalResult;
        throw new NumericEvalEx(formatErrorMessage(ee));
    }
    if (!(evalResult instanceof NumericValueEval)) {
        throw new NumericEvalEx("Result object type (" + evalResult.getClass().getName() + ") is invalid.  Expected implementor of (" + NumericValueEval.class.getName() + ")");
    }
    NumericValueEval result = (NumericValueEval) evalResult;
    return result.getNumberValue();
}
Also used : NumericValueEval(org.apache.poi.ss.formula.eval.NumericValueEval) NotImplementedException(org.apache.poi.ss.formula.eval.NotImplementedException) NumericValueEval(org.apache.poi.ss.formula.eval.NumericValueEval) ValueEval(org.apache.poi.ss.formula.eval.ValueEval) ErrorEval(org.apache.poi.ss.formula.eval.ErrorEval)

Example 5 with NotImplementedException

use of org.apache.poi.ss.formula.eval.NotImplementedException in project poi by apache.

the class CheckFunctionsSupported method getEvaluationProblems.

public FormulaEvaluationProblems getEvaluationProblems(Sheet sheet) {
    Set<String> unsupportedFunctions = new HashSet<String>();
    Map<CellReference, Exception> unevaluatableCells = new HashMap<CellReference, Exception>();
    for (Row r : sheet) {
        for (Cell c : r) {
            try {
                evaluator.evaluate(c);
            } catch (Exception e) {
                if (e instanceof NotImplementedException && e.getCause() != null) {
                    // Has been wrapped with cell details, but we know those
                    e = (Exception) e.getCause();
                }
                if (e instanceof NotImplementedFunctionException) {
                    NotImplementedFunctionException nie = (NotImplementedFunctionException) e;
                    unsupportedFunctions.add(nie.getFunctionName());
                }
                unevaluatableCells.put(new CellReference(c), e);
            }
        }
    }
    return new FormulaEvaluationProblems(unsupportedFunctions, unevaluatableCells);
}
Also used : HashMap(java.util.HashMap) NotImplementedException(org.apache.poi.ss.formula.eval.NotImplementedException) Row(org.apache.poi.ss.usermodel.Row) CellReference(org.apache.poi.ss.util.CellReference) NotImplementedFunctionException(org.apache.poi.ss.formula.eval.NotImplementedFunctionException) Cell(org.apache.poi.ss.usermodel.Cell) NotImplementedException(org.apache.poi.ss.formula.eval.NotImplementedException) NotImplementedFunctionException(org.apache.poi.ss.formula.eval.NotImplementedFunctionException) HashSet(java.util.HashSet)

Aggregations

NotImplementedException (org.apache.poi.ss.formula.eval.NotImplementedException)5 ValueEval (org.apache.poi.ss.formula.eval.ValueEval)3 NotImplementedFunctionException (org.apache.poi.ss.formula.eval.NotImplementedFunctionException)2 NumericValueEval (org.apache.poi.ss.formula.eval.NumericValueEval)2 Cell (org.apache.poi.ss.usermodel.Cell)2 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 BlankEval (org.apache.poi.ss.formula.eval.BlankEval)1 ErrorEval (org.apache.poi.ss.formula.eval.ErrorEval)1 EvaluationException (org.apache.poi.ss.formula.eval.EvaluationException)1 StringValueEval (org.apache.poi.ss.formula.eval.StringValueEval)1 FreeRefFunction (org.apache.poi.ss.formula.functions.FreeRefFunction)1 Function (org.apache.poi.ss.formula.functions.Function)1 CellValue (org.apache.poi.ss.usermodel.CellValue)1 FormulaEvaluator (org.apache.poi.ss.usermodel.FormulaEvaluator)1 Row (org.apache.poi.ss.usermodel.Row)1 Sheet (org.apache.poi.ss.usermodel.Sheet)1 Workbook (org.apache.poi.ss.usermodel.Workbook)1 CellReference (org.apache.poi.ss.util.CellReference)1 Test (org.junit.Test)1