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.

ErrorEval.java 3.9KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. package org.apache.poi.ss.formula.eval;
  18. import java.util.HashMap;
  19. import java.util.Map;
  20. import org.apache.poi.ss.usermodel.FormulaError;
  21. /**
  22. * Evaluations for formula errors
  23. */
  24. public final class ErrorEval implements ValueEval {
  25. private static final Map<FormulaError,ErrorEval> evals = new HashMap<>();
  26. /** <b>#NULL!</b> - Intersection of two cell ranges is empty */
  27. public static final ErrorEval NULL_INTERSECTION = new ErrorEval(FormulaError.NULL);
  28. /** <b>#DIV/0!</b> - Division by zero */
  29. public static final ErrorEval DIV_ZERO = new ErrorEval(FormulaError.DIV0);
  30. /** <b>#VALUE!</b> - Wrong type of operand */
  31. public static final ErrorEval VALUE_INVALID = new ErrorEval(FormulaError.VALUE);
  32. /** <b>#REF!</b> - Illegal or deleted cell reference */
  33. public static final ErrorEval REF_INVALID = new ErrorEval(FormulaError.REF);
  34. /** <b>#NAME?</b> - Wrong function or range name */
  35. public static final ErrorEval NAME_INVALID = new ErrorEval(FormulaError.NAME);
  36. /** <b>#NUM!</b> - Value range overflow */
  37. public static final ErrorEval NUM_ERROR = new ErrorEval(FormulaError.NUM);
  38. /** <b>#N/A</b> - Argument or function not available */
  39. public static final ErrorEval NA = new ErrorEval(FormulaError.NA);
  40. // POI internal error codes
  41. public static final ErrorEval FUNCTION_NOT_IMPLEMENTED = new ErrorEval(FormulaError.FUNCTION_NOT_IMPLEMENTED);
  42. // Note - Excel does not seem to represent this condition with an error code
  43. public static final ErrorEval CIRCULAR_REF_ERROR = new ErrorEval(FormulaError.CIRCULAR_REF);
  44. /**
  45. * Translates an Excel internal error code into the corresponding POI ErrorEval instance
  46. * @param errorCode An error code listed in {@link FormulaError}
  47. * @throws RuntimeException If an unknown errorCode is specified
  48. */
  49. public static ErrorEval valueOf(int errorCode) {
  50. FormulaError error = FormulaError.forInt(errorCode);
  51. ErrorEval eval = evals.get(error);
  52. if (eval != null) {
  53. return eval;
  54. } else {
  55. throw new RuntimeException("Unhandled error type for code " + errorCode);
  56. }
  57. }
  58. /**
  59. * Converts error codes to text. Handles non-standard error codes OK.
  60. * For debug/test purposes (and for formatting error messages).
  61. * @return the String representation of the specified Excel error code.
  62. */
  63. public static String getText(int errorCode) {
  64. if(FormulaError.isValidCode(errorCode)) {
  65. return FormulaError.forInt(errorCode).getString();
  66. }
  67. // Give a special string, based on ~, to make clear this isn't a standard Excel error
  68. return "~non~std~err(" + errorCode + ")~";
  69. }
  70. private final FormulaError _error;
  71. private ErrorEval(FormulaError error) {
  72. _error = error;
  73. evals.put(error, this);
  74. }
  75. public int getErrorCode() {
  76. return _error.getLongCode();
  77. }
  78. public String getErrorString() {
  79. return _error.getString();
  80. }
  81. public String toString() {
  82. return getClass().getName() + " [" +
  83. _error.getString() +
  84. "]";
  85. }
  86. }