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 | using System.Text.RegularExpressions;
|
---|
40 | using System.Collections;
|
---|
41 |
|
---|
42 | namespace OfficeOpenXml.DataValidation.Formulas
|
---|
43 | {
|
---|
44 | internal class ExcelDataValidationFormulaList : ExcelDataValidationFormula, IExcelDataValidationFormulaList
|
---|
45 | {
|
---|
46 | #region class DataValidationList
|
---|
47 | private class DataValidationList : IList<string>, ICollection
|
---|
48 | {
|
---|
49 | private IList<string> _items = new List<string>();
|
---|
50 | private EventHandler<EventArgs> _listChanged;
|
---|
51 |
|
---|
52 | public event EventHandler<EventArgs> ListChanged
|
---|
53 | {
|
---|
54 | add { _listChanged += value; }
|
---|
55 | remove { _listChanged -= value; }
|
---|
56 | }
|
---|
57 |
|
---|
58 | private void OnListChanged()
|
---|
59 | {
|
---|
60 | if (_listChanged != null)
|
---|
61 | {
|
---|
62 | _listChanged(this, EventArgs.Empty);
|
---|
63 | }
|
---|
64 | }
|
---|
65 |
|
---|
66 | #region IList members
|
---|
67 | int IList<string>.IndexOf(string item)
|
---|
68 | {
|
---|
69 | return _items.IndexOf(item);
|
---|
70 | }
|
---|
71 |
|
---|
72 | void IList<string>.Insert(int index, string item)
|
---|
73 | {
|
---|
74 | _items.Insert(index, item);
|
---|
75 | OnListChanged();
|
---|
76 | }
|
---|
77 |
|
---|
78 | void IList<string>.RemoveAt(int index)
|
---|
79 | {
|
---|
80 | _items.RemoveAt(index);
|
---|
81 | OnListChanged();
|
---|
82 | }
|
---|
83 |
|
---|
84 | string IList<string>.this[int index]
|
---|
85 | {
|
---|
86 | get
|
---|
87 | {
|
---|
88 | return _items[index];
|
---|
89 | }
|
---|
90 | set
|
---|
91 | {
|
---|
92 | _items[index] = value;
|
---|
93 | OnListChanged();
|
---|
94 | }
|
---|
95 | }
|
---|
96 |
|
---|
97 | void ICollection<string>.Add(string item)
|
---|
98 | {
|
---|
99 | _items.Add(item);
|
---|
100 | OnListChanged();
|
---|
101 | }
|
---|
102 |
|
---|
103 | void ICollection<string>.Clear()
|
---|
104 | {
|
---|
105 | _items.Clear();
|
---|
106 | OnListChanged();
|
---|
107 | }
|
---|
108 |
|
---|
109 | bool ICollection<string>.Contains(string item)
|
---|
110 | {
|
---|
111 | return _items.Contains(item);
|
---|
112 | }
|
---|
113 |
|
---|
114 | void ICollection<string>.CopyTo(string[] array, int arrayIndex)
|
---|
115 | {
|
---|
116 | _items.CopyTo(array, arrayIndex);
|
---|
117 | }
|
---|
118 |
|
---|
119 | int ICollection<string>.Count
|
---|
120 | {
|
---|
121 | get { return _items.Count; }
|
---|
122 | }
|
---|
123 |
|
---|
124 | bool ICollection<string>.IsReadOnly
|
---|
125 | {
|
---|
126 | get { return false; }
|
---|
127 | }
|
---|
128 |
|
---|
129 | bool ICollection<string>.Remove(string item)
|
---|
130 | {
|
---|
131 | var retVal = _items.Remove(item);
|
---|
132 | OnListChanged();
|
---|
133 | return retVal;
|
---|
134 | }
|
---|
135 |
|
---|
136 | IEnumerator<string> IEnumerable<string>.GetEnumerator()
|
---|
137 | {
|
---|
138 | return _items.GetEnumerator();
|
---|
139 | }
|
---|
140 |
|
---|
141 | System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
|
---|
142 | {
|
---|
143 | return _items.GetEnumerator();
|
---|
144 | }
|
---|
145 | #endregion
|
---|
146 |
|
---|
147 | public void CopyTo(Array array, int index)
|
---|
148 | {
|
---|
149 | _items.CopyTo((string[])array, index);
|
---|
150 | }
|
---|
151 |
|
---|
152 | int ICollection.Count
|
---|
153 | {
|
---|
154 | get { return _items.Count; }
|
---|
155 | }
|
---|
156 |
|
---|
157 | public bool IsSynchronized
|
---|
158 | {
|
---|
159 | get { return ((ICollection)_items).IsSynchronized; }
|
---|
160 | }
|
---|
161 |
|
---|
162 | public object SyncRoot
|
---|
163 | {
|
---|
164 | get { return ((ICollection)_items).SyncRoot; }
|
---|
165 | }
|
---|
166 | }
|
---|
167 | #endregion
|
---|
168 |
|
---|
169 | public ExcelDataValidationFormulaList(XmlNamespaceManager namespaceManager, XmlNode itemNode, string formulaPath)
|
---|
170 | : base(namespaceManager, itemNode, formulaPath)
|
---|
171 | {
|
---|
172 | Require.Argument(formulaPath).IsNotNullOrEmpty("formulaPath");
|
---|
173 | _formulaPath = formulaPath;
|
---|
174 | var values = new DataValidationList();
|
---|
175 | values.ListChanged += new EventHandler<EventArgs>(values_ListChanged);
|
---|
176 | Values = values;
|
---|
177 | SetInitialValues();
|
---|
178 | }
|
---|
179 |
|
---|
180 | private string _formulaPath;
|
---|
181 |
|
---|
182 | private void SetInitialValues()
|
---|
183 | {
|
---|
184 | var @value = GetXmlNodeString(_formulaPath);
|
---|
185 | if (!string.IsNullOrEmpty(@value))
|
---|
186 | {
|
---|
187 | if (@value.StartsWith("\"") && @value.EndsWith("\""))
|
---|
188 | {
|
---|
189 | @value = @value.TrimStart('"').TrimEnd('"');
|
---|
190 | var items = @value.Split(new char[]{','}, StringSplitOptions.RemoveEmptyEntries);
|
---|
191 | foreach (var item in items)
|
---|
192 | {
|
---|
193 | Values.Add(item);
|
---|
194 | }
|
---|
195 | }
|
---|
196 | else
|
---|
197 | {
|
---|
198 | ExcelFormula = @value;
|
---|
199 | }
|
---|
200 | }
|
---|
201 | }
|
---|
202 |
|
---|
203 | void values_ListChanged(object sender, EventArgs e)
|
---|
204 | {
|
---|
205 | if (Values.Count > 0)
|
---|
206 | {
|
---|
207 | State = FormulaState.Value;
|
---|
208 | }
|
---|
209 | var valuesAsString = GetValueAsString();
|
---|
210 | // Excel supports max 255 characters in this field.
|
---|
211 | if (valuesAsString.Length > 255)
|
---|
212 | {
|
---|
213 | throw new InvalidOperationException("The total length of a DataValidation list cannot exceed 255 characters");
|
---|
214 | }
|
---|
215 | SetXmlNodeString(_formulaPath, valuesAsString);
|
---|
216 | }
|
---|
217 | public IList<string> Values
|
---|
218 | {
|
---|
219 | get;
|
---|
220 | private set;
|
---|
221 | }
|
---|
222 |
|
---|
223 | protected override string GetValueAsString()
|
---|
224 | {
|
---|
225 | var sb = new StringBuilder();
|
---|
226 | foreach (var val in Values)
|
---|
227 | {
|
---|
228 | if (sb.Length == 0)
|
---|
229 | {
|
---|
230 | sb.Append("\"");
|
---|
231 | sb.Append(val);
|
---|
232 | }
|
---|
233 | else
|
---|
234 | {
|
---|
235 | sb.AppendFormat(",{0}", val);
|
---|
236 | }
|
---|
237 | }
|
---|
238 | sb.Append("\"");
|
---|
239 | return sb.ToString();
|
---|
240 | }
|
---|
241 |
|
---|
242 | internal override void ResetValue()
|
---|
243 | {
|
---|
244 | Values.Clear();
|
---|
245 | }
|
---|
246 | }
|
---|
247 | }
|
---|