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.

Errortype.java 2.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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.eval.ErrorEval;
  17. import org.apache.poi.ss.formula.eval.EvaluationException;
  18. import org.apache.poi.ss.formula.eval.NumberEval;
  19. import org.apache.poi.ss.formula.eval.OperandResolver;
  20. import org.apache.poi.ss.formula.eval.ValueEval;
  21. import org.apache.poi.ss.usermodel.FormulaError;
  22. /**
  23. * Implementation for the ERROR.TYPE() Excel function.
  24. * <p>
  25. * <b>Syntax:</b><br>
  26. * <b>ERROR.TYPE</b>(<b>errorValue</b>)</p>
  27. * <p>
  28. * Returns a number corresponding to the error type of the supplied argument.
  29. * <table>
  30. * <caption>Return values for ERROR.TYPE()</caption>
  31. * <tr><td>errorValue</td><td>Return Value</td></tr>
  32. * <tr><td>#NULL!</td><td>1</td></tr>
  33. * <tr><td>#DIV/0!</td><td>2</td></tr>
  34. * <tr><td>#VALUE!</td><td>3</td></tr>
  35. * <tr><td>#REF!</td><td>4</td></tr>
  36. * <tr><td>#NAME?</td><td>5</td></tr>
  37. * <tr><td>#NUM!</td><td>6</td></tr>
  38. * <tr><td>#N/A!</td><td>7</td></tr>
  39. * <tr><td>everything else</td><td>#N/A!</td></tr>
  40. * </table>
  41. *
  42. * Note - the results of ERROR.TYPE() are different to the constants defined in
  43. * {@code ErrorConstants}.
  44. */
  45. public final class Errortype extends Fixed1ArgFunction {
  46. @Override
  47. public ValueEval evaluate(int srcRowIndex, int srcColumnIndex, ValueEval arg0) {
  48. try {
  49. OperandResolver.getSingleValue(arg0, srcRowIndex, srcColumnIndex);
  50. return ErrorEval.NA;
  51. } catch (EvaluationException e) {
  52. int result = translateErrorCodeToErrorTypeValue(e.getErrorEval().getErrorCode());
  53. return new NumberEval(result);
  54. }
  55. }
  56. private int translateErrorCodeToErrorTypeValue(int errorCode) {
  57. switch (FormulaError.forInt(errorCode)) {
  58. case NULL: return 1;
  59. case DIV0: return 2;
  60. case VALUE: return 3;
  61. case REF: return 4;
  62. case NAME: return 5;
  63. case NUM: return 6;
  64. case NA: return 7;
  65. default:
  66. throw new IllegalArgumentException("Invalid error code (" + errorCode + ")");
  67. }
  68. }
  69. }