Example usage for org.apache.poi.ss.formula TwoDEval getWidth

List of usage examples for org.apache.poi.ss.formula TwoDEval getWidth

Introduction

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

Prototype

int getWidth();

Source Link

Usage

From source file:nl.eur.ese.spreadsheettest.DStarRunner.java

License:Apache License

/**
 * For a given database returns the column number for a column heading.
 *
 * @param db Database./* www.j  av  a 2s . c  o  m*/
 * @param name Column heading.
 * @return Corresponding column number.
 * @throws EvaluationException If it's not possible to turn all headings into strings.
 */
private static int getColumnForString(TwoDEval db, String name) throws EvaluationException {
    int resultColumn = -1;
    for (int column = 0; column < db.getWidth(); ++column) {
        ValueEval columnNameValueEval = db.getValue(0, column);
        String columnName = getStringFromValueEval(columnNameValueEval);
        if (name.equals(columnName)) {
            resultColumn = column;
            break;
        }
    }
    return resultColumn;
}

From source file:nl.eur.ese.spreadsheettest.DStarRunner.java

License:Apache License

/**
 * Checks a row in a database against a condition database.
 *
 * @param db Database./*from   w  w  w . j  a v  a2 s .co m*/
 * @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(TwoDEval db, int row, TwoDEval 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.
    for (int conditionRow = 1; conditionRow < cdb.getHeight(); ++conditionRow) {
        boolean matches = true;
        for (int column = 0; column < cdb.getWidth(); ++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;
            try {
                // The condition to apply.
                condition = solveReference(cdb.getValue(conditionRow, column));
            } catch (java.lang.RuntimeException e) {
                // It might be a special formula, then it is ok if it fails.
                columnCondition = false;
            }
            // If the condition is empty it matches.
            if (condition instanceof BlankEval)
                continue;
            // The column in the DB to apply the condition to.
            ValueEval targetHeader = solveReference(cdb.getValue(0, column));
            targetHeader = solveReference(targetHeader);

            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 = db.getValue(row, getColumnForName(targetHeader, db));
                if (!testNormalCondition(value, condition)) {
                    matches = false;
                    break;
                }
            } else { // It's a special formula condition.
                if (getStringFromValueEval(condition).isEmpty()) {
                    throw new EvaluationException(ErrorEval.VALUE_INVALID);
                }
                throw new NotImplementedException("D* function with formula conditions");
            }
        }
        if (matches == true) {
            return true;
        }
    }
    return false;
}