Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.ExtLibs/HeuristicLab.EPPlus/4.0.3/EPPlus-4.0.3/FormulaParsing/ExcelValues.cs @ 12074

Last change on this file since 12074 was 12074, checked in by sraggl, 9 years ago

#2341: Added EPPlus-4.0.3 to ExtLibs

File size: 5.9 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Reflection;
5using System.Text;
6
7namespace OfficeOpenXml
8{
9    /// <summary>
10    /// Represents the errortypes in excel
11    /// </summary>
12    public enum eErrorType
13    {
14        /// <summary>
15        /// Division by zero
16        /// </summary>
17        Div0,
18        /// <summary>
19        /// Not applicable
20        /// </summary>
21        NA,
22        /// <summary>
23        /// Name error
24        /// </summary>
25        Name,
26        /// <summary>
27        /// Null error
28        /// </summary>
29        Null,
30        /// <summary>
31        /// Num error
32        /// </summary>
33        Num,
34        /// <summary>
35        /// Reference error
36        /// </summary>
37        Ref,
38        /// <summary>
39        /// Value error
40        /// </summary>
41        Value
42    }
43
44    /// <summary>
45    /// Represents an Excel error.
46    /// </summary>
47    /// <seealso cref="eErrorType"/>
48    public class ExcelErrorValue
49    {
50        /// <summary>
51        /// Handles the convertion between <see cref="eErrorType"/> and the string values
52        /// used by Excel.
53        /// </summary>
54        public static class Values
55        {
56            public const string Div0 = "#DIV/0!";
57            public const string NA = "#N/A";
58            public const string Name = "#NAME?";
59            public const string Null = "#NULL!";
60            public const string Num = "#NUM!";
61            public const string Ref = "#REF!";
62            public const string Value = "#VALUE!";
63
64            private static Dictionary<string, eErrorType> _values = new Dictionary<string, eErrorType>()
65                {
66                    {Div0, eErrorType.Div0},
67                    {NA, eErrorType.NA},
68                    {Name, eErrorType.Name},
69                    {Null, eErrorType.Null},
70                    {Num, eErrorType.Num},
71                    {Ref, eErrorType.Ref},
72                    {Value, eErrorType.Value}
73                };
74
75            /// <summary>
76            /// Returns true if the supplied <paramref name="candidate"/> is an excel error.
77            /// </summary>
78            /// <param name="candidate"></param>
79            /// <returns></returns>
80            public static bool IsErrorValue(object candidate)
81            {
82                if(candidate == null || !(candidate is ExcelErrorValue)) return false;
83                var candidateString = candidate.ToString();
84                return (!string.IsNullOrEmpty(candidateString) && _values.ContainsKey(candidateString));
85            }
86
87            /// <summary>
88            /// Returns true if the supplied <paramref name="candidate"/> is an excel error.
89            /// </summary>
90            /// <param name="candidate"></param>
91            /// <returns></returns>
92            public static bool StringIsErrorValue(string candidate)
93            {
94                return (!string.IsNullOrEmpty(candidate) && _values.ContainsKey(candidate));
95            }
96
97            /// <summary>
98            /// Converts a string to an <see cref="eErrorType"/>
99            /// </summary>
100            /// <param name="val"></param>
101            /// <returns></returns>
102            /// <exception cref="ArgumentException">Thrown if the supplied value is not an Excel error</exception>
103            public static eErrorType ToErrorType(string val)
104            {
105                if (string.IsNullOrEmpty(val) || !_values.ContainsKey(val))
106                {
107                    throw new ArgumentException("Invalid error code " + (val ?? "<empty>"));
108                }
109                return _values[val];
110            }
111        }
112
113        internal static ExcelErrorValue Create(eErrorType errorType)
114        {
115            return new ExcelErrorValue(errorType);
116        }
117
118        internal static ExcelErrorValue Parse(string val)
119        {
120            if (Values.StringIsErrorValue(val))
121            {
122                return new ExcelErrorValue(Values.ToErrorType(val));
123            }
124            if(string.IsNullOrEmpty(val)) throw new ArgumentNullException("val");
125            throw new ArgumentException("Not a valid error value: " + val);
126        }
127
128        private ExcelErrorValue(eErrorType type)
129        {
130            Type=type;
131        }
132
133        /// <summary>
134        /// The error type
135        /// </summary>
136        public eErrorType Type { get; private set; }
137
138        /// <summary>
139        /// Returns the string representation of the error type
140        /// </summary>
141        /// <returns></returns>
142        public override string ToString()
143        {
144            switch(Type)
145            {
146                case eErrorType.Div0:
147                    return Values.Div0;
148                case eErrorType.NA:
149                    return Values.NA;
150                case eErrorType.Name:
151                    return Values.Name;
152                case eErrorType.Null:
153                    return Values.Null;
154                case eErrorType.Num:
155                    return Values.Num;
156                case eErrorType.Ref:
157                    return Values.Ref;
158                case eErrorType.Value:
159                    return Values.Value;
160                default:
161                    throw(new ArgumentException("Invalid errortype"));
162            }
163        }
164        public static ExcelErrorValue operator +(object v1, ExcelErrorValue v2)
165        {
166            return v2;
167        }
168        public static ExcelErrorValue operator +(ExcelErrorValue v1, ExcelErrorValue v2)
169        {
170            return v1;
171        }
172
173        public override int GetHashCode()
174        {
175            return base.GetHashCode();
176        }
177
178        public override bool Equals(object obj)
179        {
180            if (!(obj is ExcelErrorValue)) return false;
181            return ((ExcelErrorValue) obj).ToString() == this.ToString();
182        }
183    }
184}
Note: See TracBrowser for help on using the repository browser.