選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

FormulaError.java 6.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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 org.apache.poi.util.Internal;
  18. import java.util.HashMap;
  19. /**
  20. * Enumerates error values in SpreadsheetML formula calculations.
  21. *
  22. * See also OOO's excelfileformat.pdf (2.5.6)
  23. */
  24. public enum FormulaError {
  25. @Internal
  26. _NO_ERROR(-1, "(no error)"),
  27. /**
  28. * Intended to indicate when two areas are required to intersect, but do not.
  29. * <p>Example:
  30. * In the case of SUM(B1 C1), the space between B1 and C1 is treated as the binary
  31. * intersection operator, when a comma was intended. end example]
  32. * </p>
  33. */
  34. NULL(0x00, "#NULL!"),
  35. /**
  36. * Intended to indicate when any number, including zero, is divided by zero.
  37. * Note: However, any error code divided by zero results in that error code.
  38. */
  39. DIV0(0x07, "#DIV/0!"),
  40. /**
  41. * Intended to indicate when an incompatible type argument is passed to a function, or
  42. * an incompatible type operand is used with an operator.
  43. * <p>Example:
  44. * In the case of a function argument, text was expected, but a number was provided
  45. * </p>
  46. */
  47. VALUE(0x0F, "#VALUE!"),
  48. /**
  49. * Intended to indicate when a cell reference is invalid.
  50. * <p>Example:
  51. * If a formula contains a reference to a cell, and then the row or column containing that cell is deleted,
  52. * a #REF! error results. If a worksheet does not support 20,001 columns,
  53. * OFFSET(A1,0,20000) will result in a #REF! error.
  54. * </p>
  55. */
  56. REF(0x17, "#REF!"),
  57. /**
  58. * Intended to indicate when what looks like a name is used, but no such name has been defined.
  59. * <p>Example:
  60. * XYZ/3, where XYZ is not a defined name. Total is & A10,
  61. * where neither Total nor is is a defined name. Presumably, "Total is " & A10
  62. * was intended. SUM(A1C10), where the range A1:C10 was intended.
  63. * </p>
  64. */
  65. NAME(0x1D, "#NAME?"),
  66. /**
  67. * Intended to indicate when an argument to a function has a compatible type, but has a
  68. * value that is outside the domain over which that function is defined. (This is known as
  69. * a domain error.)
  70. * <p>Example:
  71. * Certain calls to ASIN, ATANH, FACT, and SQRT might result in domain errors.
  72. * </p>
  73. * Intended to indicate that the result of a function cannot be represented in a value of
  74. * the specified type, typically due to extreme magnitude. (This is known as a range
  75. * error.)
  76. * <p>Example: FACT(1000) might result in a range error. </p>
  77. */
  78. NUM(0x24, "#NUM!"),
  79. /**
  80. * Intended to indicate when a designated value is not available.
  81. * <p>Example:
  82. * Some functions, such as SUMX2MY2, perform a series of operations on corresponding
  83. * elements in two arrays. If those arrays do not have the same number of elements, then
  84. * for some elements in the longer array, there are no corresponding elements in the
  85. * shorter one; that is, one or more values in the shorter array are not available.
  86. * </p>
  87. * This error value can be produced by calling the function NA
  88. */
  89. NA(0x2A, "#N/A"),
  90. // These are POI-specific error codes
  91. // It is desirable to make these (arbitrary) strings look clearly different from any other
  92. // value expression that might appear in a formula. In addition these error strings should
  93. // look unlike the standard Excel errors. Hence tilde ('~') was used.
  94. /**
  95. * POI specific code to indicate that there is a circular reference
  96. * in the formula
  97. */
  98. CIRCULAR_REF(0xFFFFFFC4, "~CIRCULAR~REF~"),
  99. /**
  100. * POI specific code to indicate that the funcition required is
  101. * not implemented in POI
  102. */
  103. FUNCTION_NOT_IMPLEMENTED(0xFFFFFFE2, "~FUNCTION~NOT~IMPLEMENTED~");
  104. private final byte type;
  105. private final int longType;
  106. private final String repr;
  107. private FormulaError(int type, String repr) {
  108. this.type = (byte)type;
  109. this.longType = type;
  110. this.repr = repr;
  111. }
  112. /**
  113. * @return numeric code of the error
  114. */
  115. public byte getCode() {
  116. return type;
  117. }
  118. /**
  119. * @return long (internal) numeric code of the error
  120. */
  121. public int getLongCode() {
  122. return longType;
  123. }
  124. /**
  125. * @return string representation of the error
  126. */
  127. public String getString() {
  128. return repr;
  129. }
  130. private static final Map<String, FormulaError> smap = new HashMap<>();
  131. private static final Map<Byte, FormulaError> bmap = new HashMap<>();
  132. private static final Map<Integer, FormulaError> imap = new HashMap<>();
  133. static{
  134. for (FormulaError error : values()) {
  135. bmap.put(error.getCode(), error);
  136. imap.put(error.getLongCode(), error);
  137. smap.put(error.getString(), error);
  138. }
  139. }
  140. public static boolean isValidCode(int errorCode) {
  141. for (FormulaError error : values()) {
  142. if (error.getCode() == errorCode) return true;
  143. if (error.getLongCode() == errorCode) return true;
  144. }
  145. return false;
  146. }
  147. public static FormulaError forInt(byte type) throws IllegalArgumentException {
  148. FormulaError err = bmap.get(type);
  149. if(err == null) throw new IllegalArgumentException("Unknown error type: " + type);
  150. return err;
  151. }
  152. public static FormulaError forInt(int type) throws IllegalArgumentException {
  153. FormulaError err = imap.get(type);
  154. if(err == null) err = bmap.get((byte)type);
  155. if(err == null) throw new IllegalArgumentException("Unknown error type: " + type);
  156. return err;
  157. }
  158. public static FormulaError forString(String code) throws IllegalArgumentException {
  159. FormulaError err = smap.get(code);
  160. if(err == null) throw new IllegalArgumentException("Unknown error code: " + code);
  161. return err;
  162. }
  163. }