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.

Countblank.java 2.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  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.functions;
  16. import org.apache.poi.ss.formula.ThreeDEval;
  17. import org.apache.poi.ss.formula.eval.*;
  18. import org.apache.poi.ss.formula.functions.CountUtils.I_MatchPredicate;
  19. /**
  20. * Implementation for the function COUNTBLANK
  21. * <p>
  22. * Syntax: COUNTBLANK ( range )
  23. * <table>
  24. * <caption>Parameter descriptions</caption>
  25. * <tr><th>range&nbsp;&nbsp;&nbsp;</th><td>is the range of cells to count blanks</td></tr>
  26. * </table>
  27. * </p>
  28. */
  29. public final class Countblank extends Fixed1ArgFunction {
  30. @Override
  31. public ValueEval evaluate(int srcRowIndex, int srcColumnIndex, ValueEval arg0) {
  32. double result;
  33. if (arg0 instanceof RefEval) {
  34. result = CountUtils.countMatchingCellsInRef((RefEval) arg0, predicate);
  35. } else if (arg0 instanceof ThreeDEval) {
  36. result = CountUtils.countMatchingCellsInArea((ThreeDEval) arg0, predicate);
  37. } else {
  38. throw new IllegalArgumentException("Bad range arg type (" + arg0.getClass().getName() + ")");
  39. }
  40. return new NumberEval(result);
  41. }
  42. private static final I_MatchPredicate predicate = valueEval -> {
  43. // Note - only BlankEval counts
  44. return valueEval == BlankEval.instance ||
  45. // see https://support.office.com/en-us/article/COUNTBLANK-function-6a92d772-675c-4bee-b346-24af6bd3ac22
  46. // "Cells with formulas that return "" (empty text) are also counted."
  47. (valueEval instanceof StringEval && ((StringEval)valueEval).getStringValue().isEmpty());
  48. };
  49. }