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.

FormulaError.java 5.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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.usermodel;
  16. import java.util.Map;
  17. import java.util.HashMap;
  18. /**
  19. * Enumerates error values in SpreadsheetML formula calculations.
  20. *
  21. * @author Yegor Kozlov
  22. */
  23. public enum FormulaError {
  24. /**
  25. * Intended to indicate when two areas are required to intersect, but do not.
  26. * <p>Example:
  27. * In the case of SUM(B1 C1), the space between B1 and C1 is treated as the binary
  28. * intersection operator, when a comma was intended. end example]
  29. * </p>
  30. */
  31. NULL(0x00, "#NULL!"),
  32. /**
  33. * Intended to indicate when any number, including zero, is divided by zero.
  34. * Note: However, any error code divided by zero results in that error code.
  35. */
  36. DIV0(0x07, "#DIV/0!"),
  37. /**
  38. * Intended to indicate when an incompatible type argument is passed to a function, or
  39. * an incompatible type operand is used with an operator.
  40. * <p>Example:
  41. * In the case of a function argument, text was expected, but a number was provided
  42. * </p>
  43. */
  44. VALUE(0x0F, "#VALUE!"),
  45. /**
  46. * Intended to indicate when a cell reference is invalid.
  47. * <p>Example:
  48. * If a formula contains a reference to a cell, and then the row or column containing that cell is deleted,
  49. * a #REF! error results. If a worksheet does not support 20,001 columns,
  50. * OFFSET(A1,0,20000) will result in a #REF! error.
  51. * </p>
  52. */
  53. REF(0x1D, "#REF!"),
  54. /**
  55. * Intended to indicate when what looks like a name is used, but no such name has been defined.
  56. * <p>Example:
  57. * XYZ/3, where XYZ is not a defined name. Total is & A10,
  58. * where neither Total nor is is a defined name. Presumably, "Total is " & A10
  59. * was intended. SUM(A1C10), where the range A1:C10 was intended.
  60. * </p>
  61. */
  62. NAME(0x1D, "#NAME?"),
  63. /**
  64. * Intended to indicate when an argument to a function has a compatible type, but has a
  65. * value that is outside the domain over which that function is defined. (This is known as
  66. * a domain error.)
  67. * <p>Example:
  68. * Certain calls to ASIN, ATANH, FACT, and SQRT might result in domain errors.
  69. * </p>
  70. * Intended to indicate that the result of a function cannot be represented in a value of
  71. * the specified type, typically due to extreme magnitude. (This is known as a range
  72. * error.)
  73. * <p>Example: FACT(1000) might result in a range error. </p>
  74. */
  75. NUM(0x24, "#NUM!"),
  76. /**
  77. * Intended to indicate when a designated value is not available.
  78. * <p>Example:
  79. * Some functions, such as SUMX2MY2, perform a series of operations on corresponding
  80. * elements in two arrays. If those arrays do not have the same number of elements, then
  81. * for some elements in the longer array, there are no corresponding elements in the
  82. * shorter one; that is, one or more values in the shorter array are not available.
  83. * </p>
  84. * This error value can be produced by calling the function NA
  85. */
  86. NA(0x2A, "#N/A");
  87. private byte type;
  88. private String repr;
  89. private FormulaError(int type, String repr) {
  90. this.type = (byte) type;
  91. this.repr = repr;
  92. }
  93. /**
  94. * @return numeric code of the error
  95. */
  96. public byte getCode() {
  97. return type;
  98. }
  99. /**
  100. * @return string representation of the error
  101. */
  102. public String getString() {
  103. return repr;
  104. }
  105. private static Map<String, FormulaError> smap = new HashMap<String, FormulaError>();
  106. private static Map<Byte, FormulaError> imap = new HashMap<Byte, FormulaError>();
  107. static{
  108. for (FormulaError error : values()) {
  109. imap.put(error.getCode(), error);
  110. smap.put(error.getString(), error);
  111. }
  112. }
  113. public static FormulaError forInt(byte type){
  114. FormulaError err = imap.get(type);
  115. if(err == null) throw new IllegalArgumentException("Unknown error type: " + type);
  116. return err;
  117. }
  118. public static FormulaError forString(String code){
  119. FormulaError err = smap.get(code);
  120. if(err == null) throw new IllegalArgumentException("Unknown error code: " + code);
  121. return err;
  122. }
  123. }