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 | *******************************************************************************/
|
---|
31 | using System;
|
---|
32 | using System.Collections.Generic;
|
---|
33 | using System.Linq;
|
---|
34 | using System.Text;
|
---|
35 | using System.Drawing;
|
---|
36 | using System.Xml;
|
---|
37 | using OfficeOpenXml.Utils;
|
---|
38 | using System.Text.RegularExpressions;
|
---|
39 | using System.Globalization;
|
---|
40 | using System.Security;
|
---|
41 |
|
---|
42 | namespace OfficeOpenXml.ConditionalFormatting
|
---|
43 | {
|
---|
44 | /// <summary>
|
---|
45 | /// 18.3.1.11 cfvo (Conditional Format Value Object)
|
---|
46 | /// Describes the values of the interpolation points in a gradient scale.
|
---|
47 | /// </summary>
|
---|
48 | public class ExcelConditionalFormattingIconDataBarValue
|
---|
49 | : XmlHelper
|
---|
50 | {
|
---|
51 | /****************************************************************************************/
|
---|
52 |
|
---|
53 | #region Private Properties
|
---|
54 | private eExcelConditionalFormattingRuleType _ruleType;
|
---|
55 | private ExcelWorksheet _worksheet;
|
---|
56 | #endregion Private Properties
|
---|
57 |
|
---|
58 | /****************************************************************************************/
|
---|
59 |
|
---|
60 | #region Constructors
|
---|
61 | /// <summary>
|
---|
62 | /// Initialize the cfvo (§18.3.1.11) node
|
---|
63 | /// </summary>
|
---|
64 | /// <param name="type"></param>
|
---|
65 | /// <param name="value"></param>
|
---|
66 | /// <param name="formula"></param>
|
---|
67 | /// <param name="ruleType"></param>
|
---|
68 | /// <param name="address"></param>
|
---|
69 | /// <param name="priority"></param>
|
---|
70 | /// <param name="worksheet"></param>
|
---|
71 | /// <param name="itemElementNode">The cfvo (§18.3.1.11) node parent. Can be any of the following:
|
---|
72 | /// colorScale (§18.3.1.16); dataBar (§18.3.1.28); iconSet (§18.3.1.49)</param>
|
---|
73 | /// <param name="namespaceManager"></param>
|
---|
74 | internal ExcelConditionalFormattingIconDataBarValue(
|
---|
75 | eExcelConditionalFormattingValueObjectType type,
|
---|
76 | double value,
|
---|
77 | string formula,
|
---|
78 | eExcelConditionalFormattingRuleType ruleType,
|
---|
79 | ExcelAddress address,
|
---|
80 | int priority,
|
---|
81 | ExcelWorksheet worksheet,
|
---|
82 | XmlNode itemElementNode,
|
---|
83 | XmlNamespaceManager namespaceManager)
|
---|
84 | : this(
|
---|
85 | ruleType,
|
---|
86 | address,
|
---|
87 | worksheet,
|
---|
88 | itemElementNode,
|
---|
89 | namespaceManager)
|
---|
90 | {
|
---|
91 | Require.Argument(priority).IsInRange(1, int.MaxValue, "priority");
|
---|
92 |
|
---|
93 | // Check if the parent does not exists
|
---|
94 | if (itemElementNode == null)
|
---|
95 | {
|
---|
96 | // Get the parent node path by the rule type
|
---|
97 | string parentNodePath = ExcelConditionalFormattingValueObjectType.GetParentPathByRuleType(
|
---|
98 | ruleType);
|
---|
99 |
|
---|
100 | // Check for en error (rule type does not have <cfvo>)
|
---|
101 | if (parentNodePath == string.Empty)
|
---|
102 | {
|
---|
103 | throw new Exception(
|
---|
104 | ExcelConditionalFormattingConstants.Errors.MissingCfvoParentNode);
|
---|
105 | }
|
---|
106 |
|
---|
107 | // Point to the <cfvo> parent node
|
---|
108 | itemElementNode = _worksheet.WorksheetXml.SelectSingleNode(
|
---|
109 | string.Format(
|
---|
110 | "//{0}[{1}='{2}']/{3}[{4}='{5}']/{6}",
|
---|
111 | // {0}
|
---|
112 | ExcelConditionalFormattingConstants.Paths.ConditionalFormatting,
|
---|
113 | // {1}
|
---|
114 | ExcelConditionalFormattingConstants.Paths.SqrefAttribute,
|
---|
115 | // {2}
|
---|
116 | address.Address,
|
---|
117 | // {3}
|
---|
118 | ExcelConditionalFormattingConstants.Paths.CfRule,
|
---|
119 | // {4}
|
---|
120 | ExcelConditionalFormattingConstants.Paths.PriorityAttribute,
|
---|
121 | // {5}
|
---|
122 | priority,
|
---|
123 | // {6}
|
---|
124 | parentNodePath),
|
---|
125 | _worksheet.NameSpaceManager);
|
---|
126 |
|
---|
127 | // Check for en error (rule type does not have <cfvo>)
|
---|
128 | if (itemElementNode == null)
|
---|
129 | {
|
---|
130 | throw new Exception(
|
---|
131 | ExcelConditionalFormattingConstants.Errors.MissingCfvoParentNode);
|
---|
132 | }
|
---|
133 | }
|
---|
134 |
|
---|
135 | TopNode = itemElementNode;
|
---|
136 |
|
---|
137 | // Save the attributes
|
---|
138 | RuleType = ruleType;
|
---|
139 | Type = type;
|
---|
140 | Value = value;
|
---|
141 | Formula = formula;
|
---|
142 | }
|
---|
143 | /// <summary>
|
---|
144 | /// Initialize the cfvo (§18.3.1.11) node
|
---|
145 | /// </summary>
|
---|
146 | /// <param name="ruleType"></param>
|
---|
147 | /// <param name="address"></param>
|
---|
148 | /// <param name="worksheet"></param>
|
---|
149 | /// <param name="itemElementNode">The cfvo (§18.3.1.11) node parent. Can be any of the following:
|
---|
150 | /// colorScale (§18.3.1.16); dataBar (§18.3.1.28); iconSet (§18.3.1.49)</param>
|
---|
151 | /// <param name="namespaceManager"></param>
|
---|
152 | internal ExcelConditionalFormattingIconDataBarValue(
|
---|
153 | eExcelConditionalFormattingRuleType ruleType,
|
---|
154 | ExcelAddress address,
|
---|
155 | ExcelWorksheet worksheet,
|
---|
156 | XmlNode itemElementNode,
|
---|
157 | XmlNamespaceManager namespaceManager)
|
---|
158 | : base(
|
---|
159 | namespaceManager,
|
---|
160 | itemElementNode)
|
---|
161 | {
|
---|
162 | Require.Argument(address).IsNotNull("address");
|
---|
163 | Require.Argument(worksheet).IsNotNull("worksheet");
|
---|
164 |
|
---|
165 | // Save the worksheet for private methods to use
|
---|
166 | _worksheet = worksheet;
|
---|
167 |
|
---|
168 | // Schema order list
|
---|
169 | SchemaNodeOrder = new string[]
|
---|
170 | {
|
---|
171 | ExcelConditionalFormattingConstants.Nodes.Cfvo,
|
---|
172 | };
|
---|
173 |
|
---|
174 | //Check if the parent does not exists
|
---|
175 | if (itemElementNode == null)
|
---|
176 | {
|
---|
177 | // Get the parent node path by the rule type
|
---|
178 | string parentNodePath = ExcelConditionalFormattingValueObjectType.GetParentPathByRuleType(
|
---|
179 | ruleType);
|
---|
180 |
|
---|
181 | // Check for en error (rule type does not have <cfvo>)
|
---|
182 | if (parentNodePath == string.Empty)
|
---|
183 | {
|
---|
184 | throw new Exception(
|
---|
185 | ExcelConditionalFormattingConstants.Errors.MissingCfvoParentNode);
|
---|
186 | }
|
---|
187 | }
|
---|
188 | RuleType = ruleType;
|
---|
189 | }
|
---|
190 | /// <summary>
|
---|
191 | /// Initialize the <see cref="ExcelConditionalFormattingColorScaleValue"/>
|
---|
192 | /// </summary>
|
---|
193 | /// <param name="type"></param>
|
---|
194 | /// <param name="value"></param>
|
---|
195 | /// <param name="formula"></param>
|
---|
196 | /// <param name="ruleType"></param>
|
---|
197 | /// <param name="priority"></param>
|
---|
198 | /// <param name="address"></param>
|
---|
199 | /// <param name="worksheet"></param>
|
---|
200 | /// <param name="namespaceManager"></param>
|
---|
201 | internal ExcelConditionalFormattingIconDataBarValue(
|
---|
202 | eExcelConditionalFormattingValueObjectType type,
|
---|
203 | double value,
|
---|
204 | string formula,
|
---|
205 | eExcelConditionalFormattingRuleType ruleType,
|
---|
206 | ExcelAddress address,
|
---|
207 | int priority,
|
---|
208 | ExcelWorksheet worksheet,
|
---|
209 | XmlNamespaceManager namespaceManager)
|
---|
210 | : this(
|
---|
211 | type,
|
---|
212 | value,
|
---|
213 | formula,
|
---|
214 | ruleType,
|
---|
215 | address,
|
---|
216 | priority,
|
---|
217 | worksheet,
|
---|
218 | null,
|
---|
219 | namespaceManager)
|
---|
220 | {
|
---|
221 |
|
---|
222 | }
|
---|
223 | /// <summary>
|
---|
224 | /// Initialize the <see cref="ExcelConditionalFormattingColorScaleValue"/>
|
---|
225 | /// </summary>
|
---|
226 | /// <param name="type"></param>
|
---|
227 | /// <param name="color"></param>
|
---|
228 | /// <param name="ruleType"></param>
|
---|
229 | /// <param name="priority"></param>
|
---|
230 | /// <param name="address"></param>
|
---|
231 | /// <param name="worksheet"></param>
|
---|
232 | /// <param name="namespaceManager"></param>
|
---|
233 | internal ExcelConditionalFormattingIconDataBarValue(
|
---|
234 | eExcelConditionalFormattingValueObjectType type,
|
---|
235 | Color color,
|
---|
236 | eExcelConditionalFormattingRuleType ruleType,
|
---|
237 | ExcelAddress address,
|
---|
238 | int priority,
|
---|
239 | ExcelWorksheet worksheet,
|
---|
240 | XmlNamespaceManager namespaceManager)
|
---|
241 | : this(
|
---|
242 | type,
|
---|
243 | 0,
|
---|
244 | null,
|
---|
245 | ruleType,
|
---|
246 | address,
|
---|
247 | priority,
|
---|
248 | worksheet,
|
---|
249 | null,
|
---|
250 | namespaceManager)
|
---|
251 | {
|
---|
252 | }
|
---|
253 | #endregion Constructors
|
---|
254 |
|
---|
255 | /****************************************************************************************/
|
---|
256 |
|
---|
257 | #region Methods
|
---|
258 | #endregion
|
---|
259 |
|
---|
260 | /****************************************************************************************/
|
---|
261 |
|
---|
262 | #region Exposed Properties
|
---|
263 |
|
---|
264 | /// <summary>
|
---|
265 | ///
|
---|
266 | /// </summary>
|
---|
267 | internal eExcelConditionalFormattingRuleType RuleType
|
---|
268 | {
|
---|
269 | get { return _ruleType; }
|
---|
270 | set { _ruleType = value; }
|
---|
271 | }
|
---|
272 |
|
---|
273 | /// <summary>
|
---|
274 | ///
|
---|
275 | /// </summary>
|
---|
276 | public eExcelConditionalFormattingValueObjectType Type
|
---|
277 | {
|
---|
278 | get
|
---|
279 | {
|
---|
280 | var typeAttribute = GetXmlNodeString(ExcelConditionalFormattingConstants.Paths.TypeAttribute);
|
---|
281 |
|
---|
282 | return ExcelConditionalFormattingValueObjectType.GetTypeByAttrbiute(typeAttribute);
|
---|
283 | }
|
---|
284 | set
|
---|
285 | {
|
---|
286 | if ((_ruleType==eExcelConditionalFormattingRuleType.ThreeIconSet || _ruleType==eExcelConditionalFormattingRuleType.FourIconSet || _ruleType==eExcelConditionalFormattingRuleType.FiveIconSet) &&
|
---|
287 | (value == eExcelConditionalFormattingValueObjectType.Min || value == eExcelConditionalFormattingValueObjectType.Max))
|
---|
288 | {
|
---|
289 | throw(new ArgumentException("Value type can't be Min or Max for icon sets"));
|
---|
290 | }
|
---|
291 | SetXmlNodeString(ExcelConditionalFormattingConstants.Paths.TypeAttribute, value.ToString().ToLower());
|
---|
292 | }
|
---|
293 | }
|
---|
294 |
|
---|
295 | /// <summary>
|
---|
296 | /// Get/Set the 'cfvo' node @val attribute
|
---|
297 | /// </summary>
|
---|
298 | public Double Value
|
---|
299 | {
|
---|
300 | get
|
---|
301 | {
|
---|
302 | if ((Type == eExcelConditionalFormattingValueObjectType.Num)
|
---|
303 | || (Type == eExcelConditionalFormattingValueObjectType.Percent)
|
---|
304 | || (Type == eExcelConditionalFormattingValueObjectType.Percentile))
|
---|
305 | {
|
---|
306 | return GetXmlNodeDouble(ExcelConditionalFormattingConstants.Paths.ValAttribute);
|
---|
307 | }
|
---|
308 | else
|
---|
309 | {
|
---|
310 | return 0;
|
---|
311 | }
|
---|
312 | }
|
---|
313 | set
|
---|
314 | {
|
---|
315 | string valueToStore = string.Empty;
|
---|
316 |
|
---|
317 | // Only some types use the @val attribute
|
---|
318 | if ((Type == eExcelConditionalFormattingValueObjectType.Num)
|
---|
319 | || (Type == eExcelConditionalFormattingValueObjectType.Percent)
|
---|
320 | || (Type == eExcelConditionalFormattingValueObjectType.Percentile))
|
---|
321 | {
|
---|
322 | valueToStore = value.ToString(CultureInfo.InvariantCulture);
|
---|
323 | }
|
---|
324 |
|
---|
325 | SetXmlNodeString(ExcelConditionalFormattingConstants.Paths.ValAttribute, valueToStore);
|
---|
326 | }
|
---|
327 | }
|
---|
328 |
|
---|
329 | /// <summary>
|
---|
330 | /// Get/Set the Formula of the Object Value (uses the same attribute as the Value)
|
---|
331 | /// </summary>
|
---|
332 | public string Formula
|
---|
333 | {
|
---|
334 | get
|
---|
335 | {
|
---|
336 | // Return empty if the Object Value type is not Formula
|
---|
337 | if (Type != eExcelConditionalFormattingValueObjectType.Formula)
|
---|
338 | {
|
---|
339 | return string.Empty;
|
---|
340 | }
|
---|
341 |
|
---|
342 | // Excel stores the formula in the @val attribute
|
---|
343 | return GetXmlNodeString(ExcelConditionalFormattingConstants.Paths.ValAttribute);
|
---|
344 | }
|
---|
345 | set
|
---|
346 | {
|
---|
347 | // Only store the formula if the Object Value type is Formula
|
---|
348 | if (Type == eExcelConditionalFormattingValueObjectType.Formula)
|
---|
349 | {
|
---|
350 | SetXmlNodeString(ExcelConditionalFormattingConstants.Paths.ValAttribute, value);
|
---|
351 | }
|
---|
352 | }
|
---|
353 | }
|
---|
354 | #endregion Exposed Properties
|
---|
355 |
|
---|
356 | /****************************************************************************************/
|
---|
357 | }
|
---|
358 | } |
---|