You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

WorkdayFunction.java 3.1KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. /* ====================================================================
  2. Licensed to the Apache Software Foundation (ASF) under one or more
  3. contributor license agreements. See the NOTICE file distributed with
  4. this work for additional information regarding copyright ownership.
  5. The ASF licenses this file to You under the Apache License, Version 2.0
  6. (the "License"); you may not use this file except in compliance with
  7. the License. You may obtain a copy of the License at
  8. http://www.apache.org/licenses/LICENSE-2.0
  9. Unless required by applicable law or agreed to in writing, software
  10. distributed under the License is distributed on an "AS IS" BASIS,
  11. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. See the License for the specific language governing permissions and
  13. limitations under the License.
  14. ==================================================================== */
  15. package org.apache.poi.ss.formula.atp;
  16. import org.apache.poi.ss.formula.OperationEvaluationContext;
  17. import org.apache.poi.ss.formula.eval.ErrorEval;
  18. import org.apache.poi.ss.formula.eval.EvaluationException;
  19. import org.apache.poi.ss.formula.eval.NumberEval;
  20. import org.apache.poi.ss.formula.eval.ValueEval;
  21. import org.apache.poi.ss.formula.functions.FreeRefFunction;
  22. import org.apache.poi.ss.usermodel.DateUtil;
  23. /**
  24. * Implementation of Excel 'Analysis ToolPak' function WORKDAY()<br>
  25. * Returns the date past a number of workdays beginning at a start date, considering an interval of holidays. A workday is any non
  26. * saturday/sunday date.
  27. * <p>
  28. * <b>Syntax</b><br>
  29. * <b>WORKDAY</b>(<b>startDate</b>, <b>days</b>, holidays)
  30. * <p>
  31. */
  32. final class WorkdayFunction implements FreeRefFunction {
  33. public static final FreeRefFunction instance = new WorkdayFunction(ArgumentsEvaluator.instance);
  34. private ArgumentsEvaluator evaluator;
  35. private WorkdayFunction(ArgumentsEvaluator anEvaluator) {
  36. // enforces singleton
  37. this.evaluator = anEvaluator;
  38. }
  39. /**
  40. * Evaluate for WORKDAY. Given a date, a number of days and a optional date or interval of holidays, determines which date it is past
  41. * number of parametrized workdays.
  42. *
  43. * @return {@link ValueEval} with date as its value.
  44. */
  45. public ValueEval evaluate(ValueEval[] args, OperationEvaluationContext ec) {
  46. if (args.length < 2 || args.length > 3) {
  47. return ErrorEval.VALUE_INVALID;
  48. }
  49. int srcCellRow = ec.getRowIndex();
  50. int srcCellCol = ec.getColumnIndex();
  51. double start;
  52. int days;
  53. double[] holidays;
  54. try {
  55. start = this.evaluator.evaluateDateArg(args[0], srcCellRow, srcCellCol);
  56. days = (int) Math.floor(this.evaluator.evaluateNumberArg(args[1], srcCellRow, srcCellCol));
  57. ValueEval holidaysCell = args.length == 3 ? args[2] : null;
  58. holidays = this.evaluator.evaluateDatesArg(holidaysCell, srcCellRow, srcCellCol);
  59. return new NumberEval(DateUtil.getExcelDate(WorkdayCalculator.instance.calculateWorkdays(start, days, holidays)));
  60. } catch (EvaluationException e) {
  61. return ErrorEval.VALUE_INVALID;
  62. }
  63. }
  64. }