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 | * Mats Alm Added 2011-01-08
|
---|
30 | * Jan Källman License changed GPL-->LGPL 2011-12-27
|
---|
31 | *******************************************************************************/
|
---|
32 | using System;
|
---|
33 | using System.Collections.Generic;
|
---|
34 | using System.Linq;
|
---|
35 | using System.Text;
|
---|
36 | using System.Xml;
|
---|
37 | using OfficeOpenXml.Utils;
|
---|
38 | using OfficeOpenXml.DataValidation.Formulas.Contracts;
|
---|
39 |
|
---|
40 | namespace OfficeOpenXml.DataValidation.Formulas
|
---|
41 | {
|
---|
42 | /// <summary>
|
---|
43 | /// Enumeration representing the state of an <see cref="ExcelDataValidationFormulaValue{T}"/>
|
---|
44 | /// </summary>
|
---|
45 | internal enum FormulaState
|
---|
46 | {
|
---|
47 | /// <summary>
|
---|
48 | /// Value is set
|
---|
49 | /// </summary>
|
---|
50 | Value,
|
---|
51 | /// <summary>
|
---|
52 | /// Formula is set
|
---|
53 | /// </summary>
|
---|
54 | Formula
|
---|
55 | }
|
---|
56 |
|
---|
57 | /// <summary>
|
---|
58 | /// Base class for a formula
|
---|
59 | /// </summary>
|
---|
60 | internal abstract class ExcelDataValidationFormula : XmlHelper
|
---|
61 | {
|
---|
62 | /// <summary>
|
---|
63 | /// Constructor
|
---|
64 | /// </summary>
|
---|
65 | /// <param name="namespaceManager">Namespacemanger of the worksheet</param>
|
---|
66 | /// <param name="topNode">validation top node</param>
|
---|
67 | /// <param name="formulaPath">xml path of the current formula</param>
|
---|
68 | public ExcelDataValidationFormula(XmlNamespaceManager namespaceManager, XmlNode topNode, string formulaPath)
|
---|
69 | : base(namespaceManager, topNode)
|
---|
70 | {
|
---|
71 | Require.Argument(formulaPath).IsNotNullOrEmpty("formulaPath");
|
---|
72 | FormulaPath = formulaPath;
|
---|
73 | }
|
---|
74 |
|
---|
75 | private string _formula;
|
---|
76 |
|
---|
77 | protected string FormulaPath
|
---|
78 | {
|
---|
79 | get;
|
---|
80 | private set;
|
---|
81 | }
|
---|
82 |
|
---|
83 | /// <summary>
|
---|
84 | /// State of the validationformula, i.e. tells if value or formula is set
|
---|
85 | /// </summary>
|
---|
86 | protected FormulaState State
|
---|
87 | {
|
---|
88 | get;
|
---|
89 | set;
|
---|
90 | }
|
---|
91 |
|
---|
92 | /// <summary>
|
---|
93 | /// A formula which output must match the current validation type
|
---|
94 | /// </summary>
|
---|
95 | public string ExcelFormula
|
---|
96 | {
|
---|
97 | get
|
---|
98 | {
|
---|
99 | return _formula;
|
---|
100 | }
|
---|
101 | set
|
---|
102 | {
|
---|
103 | if (!string.IsNullOrEmpty(value))
|
---|
104 | {
|
---|
105 | ResetValue();
|
---|
106 | State = FormulaState.Formula;
|
---|
107 | }
|
---|
108 | if (value != null && value.Length > 255)
|
---|
109 | {
|
---|
110 | throw new InvalidOperationException("The length of a DataValidation formula cannot exceed 255 characters");
|
---|
111 | }
|
---|
112 | //var val = SqRefUtility.ToSqRefAddress(value);
|
---|
113 | _formula = value;
|
---|
114 | SetXmlNodeString(FormulaPath, value);
|
---|
115 | }
|
---|
116 | }
|
---|
117 |
|
---|
118 | internal abstract void ResetValue();
|
---|
119 |
|
---|
120 | /// <summary>
|
---|
121 | /// This value will be stored in the xml. Can be overridden by subclasses
|
---|
122 | /// </summary>
|
---|
123 | internal virtual string GetXmlValue()
|
---|
124 | {
|
---|
125 | if (State == FormulaState.Formula)
|
---|
126 | {
|
---|
127 | return ExcelFormula;
|
---|
128 | }
|
---|
129 | return GetValueAsString();
|
---|
130 | }
|
---|
131 |
|
---|
132 | /// <summary>
|
---|
133 | /// Returns the value as a string. Must be implemented by subclasses
|
---|
134 | /// </summary>
|
---|
135 | /// <returns></returns>
|
---|
136 | protected abstract string GetValueAsString();
|
---|
137 | }
|
---|
138 | }
|
---|