Free cookie consent management tool by TermsFeed Policy Generator

source: branches/crossvalidation-2434/HeuristicLab.ExtLibs/HeuristicLab.EPPlus/4.0.3/EPPlus-4.0.3/ConditionalFormatting/ExcelConditionalFormattingHelper.cs @ 12779

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

#2341: Added EPPlus-4.0.3 to ExtLibs

File size: 7.6 KB
Line 
1/*******************************************************************************
2 * You may amend and distribute as you like, but don't remove this header!
3 *
4 * EPPlus provides server-side generation of Excel 2007/2010 spreadsheets.
5 * See http://www.codeplex.com/EPPlus for details.
6 *
7 * Copyright (C) 2011  Jan Källman
8 *
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
13
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 
17 * See the GNU Lesser General Public License for more details.
18 *
19 * The GNU Lesser General Public License can be viewed at http://www.opensource.org/licenses/lgpl-license.php
20 * If you unfamiliar with this license or have questions about it, here is an http://www.gnu.org/licenses/gpl-faq.html
21 *
22 * All code and executables are provided "as is" with no warranty either express or implied.
23 * The author accepts no liability for any damage or loss of business that this product may cause.
24 *
25 * Code change notes:
26 *
27 * Author             Change            Date
28 * ******************************************************************************
29 * Eyal Seagull        Added            2012-04-03
30 *******************************************************************************/
31using System;
32using System.Collections.Generic;
33using System.Linq;
34using System.Text;
35using System.Text.RegularExpressions;
36using OfficeOpenXml.Utils;
37using System.Drawing;
38using System.Globalization;
39using System.Xml;
40
41namespace OfficeOpenXml.ConditionalFormatting
42{
43  /// <summary>
44  /// Conditional formatting helper
45  /// </summary>
46  internal static class ExcelConditionalFormattingHelper
47  {
48    /// <summary>
49    /// Check and fix an address (string address)
50    /// </summary>
51    /// <param name="address"></param>
52    /// <returns></returns>
53    public static string CheckAndFixRangeAddress(
54      string address)
55    {
56      if (address.Contains(','))
57      {
58        throw new FormatException(
59          ExcelConditionalFormattingConstants.Errors.CommaSeparatedAddresses);
60      }
61
62      address = address.ToUpper(CultureInfo.InvariantCulture);
63
64      if (Regex.IsMatch(address, @"[A-Z]+:[A-Z]+"))
65      {
66        address = AddressUtility.ParseEntireColumnSelections(address);
67      }
68
69      return address;
70    }
71
72    /// <summary>
73    /// Convert a color code to Color Object
74    /// </summary>
75    /// <param name="colorCode">Color Code (Ex. "#FFB43C53" or "FFB43C53")</param>
76    /// <returns></returns>
77    public static Color ConvertFromColorCode(
78      string colorCode)
79    {
80      try
81      {
82        return Color.FromArgb(Int32.Parse(colorCode.Replace("#", ""), NumberStyles.HexNumber));
83      }
84      catch
85      {
86        // Assume white is the default color (instead of giving an error)
87        return Color.White;
88      }
89    }
90
91    /// <summary>
92    ///
93    /// </summary>
94    /// <param name="node"></param>
95    /// <param name="attribute"></param>
96    /// <returns></returns>
97    public static string GetAttributeString(
98      XmlNode node,
99      string attribute)
100    {
101      try
102      {
103        var value = node.Attributes[attribute].Value;
104        return (value == null) ? string.Empty : value;
105      }
106      catch
107      {
108        return string.Empty;
109      }
110    }
111
112    /// <summary>
113    ///
114    /// </summary>
115    /// <param name="node"></param>
116    /// <param name="attribute"></param>
117    /// <returns></returns>
118    public static int GetAttributeInt(
119      XmlNode node,
120      string attribute)
121    {
122      try
123      {
124        var value = node.Attributes[attribute].Value;
125        return int.Parse(value, NumberStyles.Integer, CultureInfo.InvariantCulture);
126      }
127      catch
128      {
129        return int.MinValue;
130      }
131    }
132
133    /// <summary>
134    ///
135    /// </summary>
136    /// <param name="node"></param>
137    /// <param name="attribute"></param>
138    /// <returns></returns>
139    public static int? GetAttributeIntNullable(
140      XmlNode node,
141      string attribute)
142    {
143      try
144      {
145          if (node.Attributes[attribute] == null)
146          {
147              return null;
148          }
149          else
150          {
151              var value = node.Attributes[attribute].Value;
152              return int.Parse(value, NumberStyles.Integer, CultureInfo.InvariantCulture);
153          }
154      }
155      catch
156      {
157        return null;
158      }
159    }
160
161    /// <summary>
162    ///
163    /// </summary>
164    /// <param name="node"></param>
165    /// <param name="attribute"></param>
166    /// <returns></returns>
167    public static bool GetAttributeBool(
168      XmlNode node,
169      string attribute)
170    {
171      try
172      {
173        var value = node.Attributes[attribute].Value;
174        return (value == "1" || value == "-1" || value.Equals("TRUE", StringComparison.InvariantCultureIgnoreCase));
175      }
176      catch
177      {
178        return false;
179      }
180    }
181
182    /// <summary>
183    ///
184    /// </summary>
185    /// <param name="node"></param>
186    /// <param name="attribute"></param>
187    /// <returns></returns>
188    public static bool? GetAttributeBoolNullable(
189      XmlNode node,
190      string attribute)
191    {
192      try
193      {
194          if (node.Attributes[attribute] == null)
195          {
196              return null;
197          }
198          else
199          {
200              var value = node.Attributes[attribute].Value;
201              return (value == "1" || value == "-1" || value.Equals("TRUE",StringComparison.InvariantCultureIgnoreCase));
202          }
203      }
204      catch
205      {
206        return null;
207      }
208    }
209
210    /// <summary>
211    ///
212    /// </summary>
213    /// <param name="node"></param>
214    /// <param name="attribute"></param>
215    /// <returns></returns>
216    public static double GetAttributeDouble(
217      XmlNode node,
218      string attribute)
219    {
220      try
221      {
222        var value = node.Attributes[attribute].Value;
223        return double.Parse(value, NumberStyles.Number, CultureInfo.InvariantCulture);
224      }
225      catch
226      {
227        return double.NaN;
228      }
229    }
230
231    /// <summary>
232    ///
233    /// </summary>
234    /// <param name="node"></param>
235    /// <param name="attribute"></param>
236    /// <returns></returns>
237    public static decimal GetAttributeDecimal(
238      XmlNode node,
239      string attribute)
240    {
241      try
242      {
243        var value = node.Attributes[attribute].Value;
244        return decimal.Parse(value, NumberStyles.Any, CultureInfo.InvariantCulture);
245      }
246      catch
247      {
248        return decimal.MinValue;
249      }
250    }
251
252    /// <summary>
253    /// Encode to XML (special characteres: &apos; &quot; &gt; &lt; &amp;)
254    /// </summary>
255    /// <param name="s"></param>
256    /// <returns></returns>
257    public static string EncodeXML(
258      this string s)
259    {
260      return s.Replace("&", "&amp;").Replace("<", "&lt;").Replace(">", "&gt;").Replace("\"", "&quot;").Replace("'", "&apos;");
261    }
262
263    /// <summary>
264    /// Decode from XML (special characteres: &apos; &quot; &gt; &lt; &amp;)
265    /// </summary>
266    /// <param name="s"></param>
267    /// <returns></returns>
268    public static string DecodeXML(
269      this string s)
270    {
271      return s.Replace("'", "&apos;").Replace("\"", "&quot;").Replace(">", "&gt;").Replace("<", "&lt;").Replace("&", "&amp;");
272    }
273  }
274}
Note: See TracBrowser for help on using the repository browser.