largerEqualThan,
smallerThan,
smallerEqualThan,
- equal
+ equal,
+ notEqual
}
/**
}
/**
- * Test a value against a simple (< > <= >= = starts-with) condition string.
+ * Test a value against a simple (< > <= >= = <> starts-with) condition string.
*
* @param value The value to check.
* @param condition The condition to check for.
if(condition instanceof StringEval) {
String conditionString = ((StringEval)condition).getStringValue();
- if(conditionString.startsWith("<")) { // It's a </<= condition.
+ if(conditionString.startsWith("<")) { // It's a </<=/<> condition.
String number = conditionString.substring(1);
if(number.startsWith("=")) {
number = number.substring(1);
return testNumericCondition(value, operator.smallerEqualThan, number);
+ } else if (number.startsWith(">")) {
+ number = number.substring(1);
+ boolean itsANumber = isNumber(number);
+ if (itsANumber) {
+ return testNumericCondition(value, operator.notEqual, number);
+ } else {
+ return testStringCondition(value, operator.notEqual, number);
+ }
} else {
return testNumericCondition(value, operator.smallerThan, number);
}
return value instanceof BlankEval;
}
// Distinguish between string and number.
- boolean itsANumber;
- try {
- Integer.parseInt(stringOrNumber);
- itsANumber = true;
- } catch (NumberFormatException e) { // It's not an int.
- try {
- Double.parseDouble(stringOrNumber);
- itsANumber = true;
- } catch (NumberFormatException e2) { // It's a string.
- itsANumber = false;
- }
- }
+ boolean itsANumber = isNumber(stringOrNumber);
if(itsANumber) {
return testNumericCondition(value, operator.equal, stringOrNumber);
} else { // It's a string.
- String valueString = value instanceof BlankEval ? "" : OperandResolver.coerceValueToString(value);
- return stringOrNumber.equalsIgnoreCase(valueString);
+ return testStringCondition(value, operator.equal, stringOrNumber);
}
} else { // It's a text starts-with condition.
if(conditionString.isEmpty()) {
return result <= 0;
case equal:
return result == 0;
+ case notEqual:
+ return result != 0;
+ }
+ return false; // Can not be reached.
+ }
+
+ /**
+ * Test whether a value matches a text condition.
+ * @param valueEval Value to check.
+ * @param op Comparator to use.
+ * @param condition Value to check against.
+ * @return whether the condition holds.
+ */
+ private static boolean testStringCondition(
+ ValueEval valueEval, operator op, String condition) {
+
+ String valueString = valueEval instanceof BlankEval ? "" : OperandResolver.coerceValueToString(valueEval);
+ switch(op) {
+ case equal:
+ return valueString.equalsIgnoreCase(condition);
+ case notEqual:
+ return !valueString.equalsIgnoreCase(condition);
}
return false; // Can not be reached.
}
return e.getErrorEval();
}
}
+
+ /**
+ * Determines whether a given string represents a valid number.
+ *
+ * @param value The string to be checked if it represents a number.
+ * @return {@code true} if the string can be parsed as either an integer or
+ * a double; {@code false} otherwise.
+ */
+ private static boolean isNumber(String value) {
+ boolean itsANumber;
+ try {
+ Integer.parseInt(value);
+ itsANumber = true;
+ } catch (NumberFormatException e) { // It's not an int.
+ try {
+ Double.parseDouble(value);
+ itsANumber = true;
+ } catch (NumberFormatException e2) { // It's a string.
+ itsANumber = false;
+ }
+ }
+ return itsANumber;
+ }
}
HSSFCell cell = wb.getSheetAt(0).getRow(0).createCell(12);
assertDouble(fe, cell, "DAVERAGE(A4:E10, \"Yield\", A1:B2)", 12);
assertDouble(fe, cell, "DAVERAGE(A4:E10, 3, A4:E10)", 13);
+ assertDouble(fe, cell, "DAVERAGE(A4:E10, \"Profit\", A12:A13)", 92.6);
+ assertDouble(fe, cell, "DAVERAGE(A4:E10, \"Profit\", B12:C13)", 82.5);
}
}
addRow(sheet, 7, "Apple", 14, 15, 10, 75);
addRow(sheet, 8, "Pear", 9, 8, 8, 76.8);
addRow(sheet, 9, "Apple", 8, 9, 6, 45);
+ addRow(sheet, 10);
+ addRow(sheet, 11, "Tree", "Height", "Height");
+ addRow(sheet, 12, "<>Apple", "<>12", "<>9");
return wb;
}
}
assertDouble(fe, cell, "DCOUNT(A5:E11, 2, A1:F3)", 4);
assertDouble(fe, cell, "DCOUNT(A5:E11, 3, A1:F3)", 3);
assertDouble(fe, cell, "DCOUNT(A5:E11, 5, A1:F3)", 3);
+ assertDouble(fe, cell, "DCOUNT(A5:E11, 5, A13:A14)", 2);
+ assertDouble(fe, cell, "DCOUNT(A5:E11, 5, B13:B14)", 3);
}
}
addRow(sheet, 8, "Apple", 14, null, 10, 75);
addRow(sheet, 9, "Pear", 9, 8, 8, "$77");
addRow(sheet, 10, "Apple", 12, 11, 6, 45);
+ addRow(sheet, 11);
+ addRow(sheet, 12, "Tree", "Height");
+ addRow(sheet, 13, "<>Apple", "<>12");
return wb;
}
}
assertDouble(fe, cell, "DCOUNTA(A4:E10, \"Profit\", A1:F2)", 1);
assertDouble(fe, cell, "DCOUNTA(A4:E10, \"Profit\", A1:F3)", 3);
assertDouble(fe, cell, "DCOUNTA(A4:E10, \"Age\", A1:F3)", 2);
+ assertDouble(fe, cell, "DCOUNTA(A4:E10, \"Profit\", A12:A13)", 3);
+ assertDouble(fe, cell, "DCOUNTA(A4:E10, \"Age\", B12:B13)", 4);
}
}
addRow(sheet, 7, "Apple", 14, null, 10, 75);
addRow(sheet, 8, "Pear", 9, 8, 8, "$77");
addRow(sheet, 9, "Apple", 8, 9, 6, 45);
+ addRow(sheet, 10);
+ addRow(sheet, 11, "Tree", "Height");
+ addRow(sheet, 12, "<>Apple", "<>12");
return wb;
}
}
HSSFFormulaEvaluator fe = new HSSFFormulaEvaluator(wb);
HSSFCell cell = wb.getSheetAt(0).getRow(0).createCell(12);
assertDouble(fe, cell, "DPRODUCT(A5:E11, \"Yield\", A1:F3)", 800, 0.0000000001);
+ assertDouble(fe, cell, "DPRODUCT(A5:E11, \"Yield\", A13:A14)", 720, 0.0000000001);
+ assertDouble(fe, cell, "DPRODUCT(A5:E11, \"Yield\", B13:C14)", 7560, 0.0000000001);
}
}
addRow(sheet, 8, "Apple", 14, 15, 10, 75);
addRow(sheet, 9, "Pear", 9, 8, 8, 77);
addRow(sheet, 10, "Apple", 8, 9, 6, 45);
+ addRow(sheet, 11);
+ addRow(sheet, 12, "Tree", "Height", "Height");
+ addRow(sheet, 13, "<>Apple", "<>12", "<>9");
return wb;
}
}
HSSFFormulaEvaluator fe = new HSSFFormulaEvaluator(wb);
HSSFCell cell = wb.getSheetAt(0).getRow(0).createCell(12);
assertDouble(fe, cell, "DSTDEV(A5:E11, \"Yield\", A1:A3)", 2.96647939483827, 0.0000000001);
+ assertDouble(fe, cell, "DSTDEV(A5:E11, \"Yield\", B12:C14)", 2.66458251889485, 0.0000000001);
}
}
HSSFFormulaEvaluator fe = new HSSFFormulaEvaluator(wb);
HSSFCell cell = wb.getSheetAt(0).getRow(0).createCell(12);
assertDouble(fe, cell, "DSTDEVP(A5:E11, \"Yield\", A1:A3)", 2.65329983228432, 0.0000000001);
+ assertDouble(fe, cell, "DSTDEVP(A5:E11, \"Yield\", A12:A13)", 0.816496580927726, 0.0000000001);
+ assertDouble(fe, cell, "DSTDEVP(A5:E11, \"Yield\", B12:C14)", 2.43241991988774, 0.0000000001);
}
}
addRow(sheet, 8, "Apple", 14, 15, 10, 75);
addRow(sheet, 9, "Pear", 9, 8, 8, 77);
addRow(sheet, 10, "Apple", 8, 9, 6, 45);
+ addRow(sheet, 11);
+ addRow(sheet, 11, "Tree", "Height", "Height");
+ addRow(sheet, 12, "<>Apple", "<>12", "<>9");
return wb;
}
}
HSSFFormulaEvaluator fe = new HSSFFormulaEvaluator(wb);
HSSFCell cell = wb.getSheetAt(0).getRow(0).createCell(12);
assertDouble(fe, cell, "DVAR(A4:E10, \"Yield\", A1:A3)", 8.8, 0.0000000001);
+ assertDouble(fe, cell, "DVAR(A4:E10, \"Yield\", A12:A13)", 1.0, 0.0000000001);
+ assertDouble(fe, cell, "DVAR(A4:E10, \"Yield\", B12:C13)", 10.9166666667, 0.0000000001);
}
}
HSSFFormulaEvaluator fe = new HSSFFormulaEvaluator(wb);
HSSFCell cell = wb.getSheetAt(0).getRow(0).createCell(12);
assertDouble(fe, cell, "DVARP(A4:E10, \"Yield\", A1:A3)", 7.04, 0.0000000001);
+ assertDouble(fe, cell, "DVARP(A4:E10, \"Yield\", A12:A13)", 0.666666666666667, 0.0000000001);
+ assertDouble(fe, cell, "DVARP(A4:E10, \"Yield\", B12:C13)", 8.1875, 0.0000000001);
}
}
addRow(sheet, 7, "Apple", 14, 15, 10, 75);
addRow(sheet, 8, "Pear", 9, 8, 8, 77);
addRow(sheet, 9, "Apple", 8, 9, 6, 45);
+ addRow(sheet, 10);
+ addRow(sheet, 11, "Tree", "Height", "Height");
+ addRow(sheet, 12, "<>Apple", "<>12", "<>9");
return wb;
}
}