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.7KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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.hssf.record.formula.functions;
  16. import org.apache.poi.hssf.record.formula.eval.AreaEval;
  17. import org.apache.poi.hssf.record.formula.eval.BlankEval;
  18. import org.apache.poi.hssf.record.formula.eval.ErrorEval;
  19. import org.apache.poi.hssf.record.formula.eval.NumberEval;
  20. import org.apache.poi.hssf.record.formula.eval.RefEval;
  21. import org.apache.poi.hssf.record.formula.eval.ValueEval;
  22. import org.apache.poi.hssf.record.formula.functions.CountUtils.I_MatchPredicate;
  23. /**
  24. * Implementation for the function COUNTBLANK
  25. * <p>
  26. * Syntax: COUNTBLANK ( range )
  27. * <table border="0" cellpadding="1" cellspacing="0" summary="Parameter descriptions">
  28. * <tr><th>range&nbsp;&nbsp;&nbsp;</th><td>is the range of cells to count blanks</td></tr>
  29. * </table>
  30. * </p>
  31. *
  32. * @author Mads Mohr Christensen
  33. */
  34. public final class Countblank implements Function {
  35. public ValueEval evaluate(ValueEval[] args, int srcRowIndex, short srcColumnIndex) {
  36. if (args.length != 1) {
  37. // TODO - it doesn't seem to be possible to enter COUNTBLANK() into Excel with the wrong arg count
  38. // perhaps this should be an exception
  39. return ErrorEval.VALUE_INVALID;
  40. }
  41. double result;
  42. ValueEval arg0 = args[0];
  43. if (arg0 instanceof RefEval) {
  44. result = CountUtils.countMatchingCell((RefEval) arg0, predicate);
  45. } else if (arg0 instanceof AreaEval) {
  46. result = CountUtils.countMatchingCellsInArea((AreaEval) arg0, predicate);
  47. } else {
  48. throw new IllegalArgumentException("Bad range arg type (" + arg0.getClass().getName() + ")");
  49. }
  50. return new NumberEval(result);
  51. }
  52. private static final I_MatchPredicate predicate = new I_MatchPredicate() {
  53. public boolean matches(ValueEval valueEval) {
  54. // Note - only BlankEval counts
  55. return valueEval == BlankEval.INSTANCE;
  56. }
  57. };
  58. }