use of org.apache.poi.ss.formula.eval.ValueEval in project poi by apache.
the class NetworkdaysFunction method evaluate.
/**
* Evaluate for NETWORKDAYS. Given two dates and a optional date or interval of holidays, determines how many working days are there
* between those dates.
*
* @return {@link ValueEval} for the number of days between two dates.
*/
public ValueEval evaluate(ValueEval[] args, OperationEvaluationContext ec) {
// NOSONAR
if (args.length < 2 || args.length > 3) {
return ErrorEval.VALUE_INVALID;
}
int srcCellRow = ec.getRowIndex();
int srcCellCol = ec.getColumnIndex();
double start, end;
double[] holidays;
try {
start = this.evaluator.evaluateDateArg(args[0], srcCellRow, srcCellCol);
end = this.evaluator.evaluateDateArg(args[1], srcCellRow, srcCellCol);
if (start > end) {
return ErrorEval.NAME_INVALID;
}
ValueEval holidaysCell = args.length == 3 ? args[2] : null;
holidays = this.evaluator.evaluateDatesArg(holidaysCell, srcCellRow, srcCellCol);
return new NumberEval(WorkdayCalculator.instance.calculateWorkdays(start, end, holidays));
} catch (EvaluationException e) {
return ErrorEval.VALUE_INVALID;
}
}
use of org.apache.poi.ss.formula.eval.ValueEval in project poi by apache.
the class WorkdayFunction method evaluate.
/**
* Evaluate for WORKDAY. Given a date, a number of days and a optional date or interval of holidays, determines which date it is past
* number of parametrized workdays.
*
* @return {@link ValueEval} with date as its value.
*/
public ValueEval evaluate(ValueEval[] args, OperationEvaluationContext ec) {
if (args.length < 2 || args.length > 3) {
return ErrorEval.VALUE_INVALID;
}
int srcCellRow = ec.getRowIndex();
int srcCellCol = ec.getColumnIndex();
double start;
int days;
double[] holidays;
try {
start = this.evaluator.evaluateDateArg(args[0], srcCellRow, srcCellCol);
days = (int) Math.floor(this.evaluator.evaluateNumberArg(args[1], srcCellRow, srcCellCol));
ValueEval holidaysCell = args.length == 3 ? args[2] : null;
holidays = this.evaluator.evaluateDatesArg(holidaysCell, srcCellRow, srcCellCol);
return new NumberEval(DateUtil.getExcelDate(WorkdayCalculator.instance.calculateWorkdays(start, days, holidays)));
} catch (EvaluationException e) {
return ErrorEval.VALUE_INVALID;
}
}
use of org.apache.poi.ss.formula.eval.ValueEval in project poi by apache.
the class YearFrac method evaluateDateArg.
private static double evaluateDateArg(ValueEval arg, int srcCellRow, int srcCellCol) throws EvaluationException {
ValueEval ve = OperandResolver.getSingleValue(arg, srcCellRow, (short) srcCellCol);
if (ve instanceof StringEval) {
String strVal = ((StringEval) ve).getStringValue();
Double dVal = OperandResolver.parseDouble(strVal);
if (dVal != null) {
return dVal.doubleValue();
}
Calendar date = DateParser.parseDate(strVal);
return DateUtil.getExcelDate(date, false);
}
return OperandResolver.coerceValueToDouble(ve);
}
use of org.apache.poi.ss.formula.eval.ValueEval in project poi by apache.
the class ArgumentsEvaluator method evaluateDateArg.
/**
* Evaluate a generic {@link ValueEval} argument to a double value that represents a date in POI.
*
* @param arg {@link ValueEval} an argument.
* @param srcCellRow number cell row.
* @param srcCellCol number cell column.
* @return a double representing a date in POI.
* @throws EvaluationException exception upon argument evaluation.
*/
public double evaluateDateArg(ValueEval arg, int srcCellRow, int srcCellCol) throws EvaluationException {
ValueEval ve = OperandResolver.getSingleValue(arg, srcCellRow, (short) srcCellCol);
if (ve instanceof StringEval) {
String strVal = ((StringEval) ve).getStringValue();
Double dVal = OperandResolver.parseDouble(strVal);
if (dVal != null) {
return dVal.doubleValue();
}
Calendar date = DateParser.parseDate(strVal);
return DateUtil.getExcelDate(date, false);
}
return OperandResolver.coerceValueToDouble(ve);
}
use of org.apache.poi.ss.formula.eval.ValueEval in project poi by apache.
the class TestProper method checkProper.
private void checkProper(String input, String expected) {
ValueEval strArg = new StringEval(input);
final ValueEval ret = TextFunction.PROPER.evaluate(new ValueEval[] { strArg }, 0, 0);
assertEquals(expected, ((StringEval) ret).getStringValue());
}
Aggregations