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 | * Jan Källman Added 21-MAR-2011
|
---|
30 | * Jan Källman License changed GPL-->LGPL 2011-12-16
|
---|
31 | *******************************************************************************/
|
---|
32 | using System;
|
---|
33 | using System.Collections.Generic;
|
---|
34 | using System.Text;
|
---|
35 | using System.Xml;
|
---|
36 |
|
---|
37 | namespace OfficeOpenXml.Table.PivotTable
|
---|
38 | {
|
---|
39 | /// <summary>
|
---|
40 | /// Base collection class for pivottable fields
|
---|
41 | /// </summary>
|
---|
42 | /// <typeparam name="T"></typeparam>
|
---|
43 | public class ExcelPivotTableFieldCollectionBase<T> : IEnumerable<T>
|
---|
44 | {
|
---|
45 | protected ExcelPivotTable _table;
|
---|
46 | internal List<T> _list = new List<T>();
|
---|
47 | internal ExcelPivotTableFieldCollectionBase(ExcelPivotTable table)
|
---|
48 | {
|
---|
49 | _table = table;
|
---|
50 | }
|
---|
51 | public IEnumerator<T> GetEnumerator()
|
---|
52 | {
|
---|
53 | return _list.GetEnumerator();
|
---|
54 | }
|
---|
55 |
|
---|
56 | System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
|
---|
57 | {
|
---|
58 | return _list.GetEnumerator();
|
---|
59 | }
|
---|
60 | public int Count
|
---|
61 | {
|
---|
62 | get
|
---|
63 | {
|
---|
64 | return _list.Count;
|
---|
65 | }
|
---|
66 | }
|
---|
67 | internal void AddInternal(T field)
|
---|
68 | {
|
---|
69 | _list.Add(field);
|
---|
70 | }
|
---|
71 | internal void Clear()
|
---|
72 | {
|
---|
73 | _list.Clear();
|
---|
74 | }
|
---|
75 | public T this[int Index]
|
---|
76 | {
|
---|
77 | get
|
---|
78 | {
|
---|
79 | if (Index < 0 || Index >= _list.Count)
|
---|
80 | {
|
---|
81 | throw (new ArgumentOutOfRangeException("Index out of range"));
|
---|
82 | }
|
---|
83 | return _list[Index];
|
---|
84 | }
|
---|
85 | }
|
---|
86 | }
|
---|
87 | public class ExcelPivotTableFieldCollection : ExcelPivotTableFieldCollectionBase<ExcelPivotTableField>
|
---|
88 | {
|
---|
89 | internal ExcelPivotTableFieldCollection(ExcelPivotTable table, string topNode) :
|
---|
90 | base(table)
|
---|
91 | {
|
---|
92 |
|
---|
93 | }
|
---|
94 | /// <summary>
|
---|
95 | /// Indexer by name
|
---|
96 | /// </summary>
|
---|
97 | /// <param name="name"></param>
|
---|
98 | /// <returns></returns>
|
---|
99 | public ExcelPivotTableField this[string name]
|
---|
100 | {
|
---|
101 | get
|
---|
102 | {
|
---|
103 | foreach (var field in _list)
|
---|
104 | {
|
---|
105 | if (field.Name == name)
|
---|
106 | {
|
---|
107 | return field;
|
---|
108 | }
|
---|
109 | }
|
---|
110 | return null;
|
---|
111 | }
|
---|
112 | }
|
---|
113 | /// <summary>
|
---|
114 | /// Returns the date group field.
|
---|
115 | /// </summary>
|
---|
116 | /// <param name="GroupBy">The type of grouping</param>
|
---|
117 | /// <returns>The matching field. If none is found null is returned</returns>
|
---|
118 | public ExcelPivotTableField GetDateGroupField(eDateGroupBy GroupBy)
|
---|
119 | {
|
---|
120 | foreach (var fld in _list)
|
---|
121 | {
|
---|
122 | if (fld.Grouping is ExcelPivotTableFieldDateGroup && (((ExcelPivotTableFieldDateGroup)fld.Grouping).GroupBy) == GroupBy)
|
---|
123 | {
|
---|
124 | return fld;
|
---|
125 | }
|
---|
126 | }
|
---|
127 | return null;
|
---|
128 | }
|
---|
129 | /// <summary>
|
---|
130 | /// Returns the numeric group field.
|
---|
131 | /// </summary>
|
---|
132 | /// <returns>The matching field. If none is found null is returned</returns>
|
---|
133 | public ExcelPivotTableField GetNumericGroupField()
|
---|
134 | {
|
---|
135 | foreach (var fld in _list)
|
---|
136 | {
|
---|
137 | if (fld.Grouping is ExcelPivotTableFieldNumericGroup)
|
---|
138 | {
|
---|
139 | return fld;
|
---|
140 | }
|
---|
141 | }
|
---|
142 | return null;
|
---|
143 | }
|
---|
144 | }
|
---|
145 | /// <summary>
|
---|
146 | /// Collection class for Row and column fields in a Pivottable
|
---|
147 | /// </summary>
|
---|
148 | public class ExcelPivotTableRowColumnFieldCollection : ExcelPivotTableFieldCollectionBase<ExcelPivotTableField>
|
---|
149 | {
|
---|
150 | internal string _topNode;
|
---|
151 | internal ExcelPivotTableRowColumnFieldCollection(ExcelPivotTable table, string topNode) :
|
---|
152 | base(table)
|
---|
153 | {
|
---|
154 | _topNode=topNode;
|
---|
155 | }
|
---|
156 |
|
---|
157 | /// <summary>
|
---|
158 | /// Add a new row/column field
|
---|
159 | /// </summary>
|
---|
160 | /// <param name="Field">The field</param>
|
---|
161 | /// <returns>The new field</returns>
|
---|
162 | public ExcelPivotTableField Add(ExcelPivotTableField Field)
|
---|
163 | {
|
---|
164 | SetFlag(Field, true);
|
---|
165 | _list.Add(Field);
|
---|
166 | return Field;
|
---|
167 | }
|
---|
168 | /// <summary>
|
---|
169 | /// Insert a new row/column field
|
---|
170 | /// </summary>
|
---|
171 | /// <param name="Field">The field</param>
|
---|
172 | /// <param name="Index">The position to insert the field</param>
|
---|
173 | /// <returns>The new field</returns>
|
---|
174 | internal ExcelPivotTableField Insert(ExcelPivotTableField Field, int Index)
|
---|
175 | {
|
---|
176 | SetFlag(Field, true);
|
---|
177 | _list.Insert(Index, Field);
|
---|
178 | return Field;
|
---|
179 | }
|
---|
180 | private void SetFlag(ExcelPivotTableField field, bool value)
|
---|
181 | {
|
---|
182 | switch (_topNode)
|
---|
183 | {
|
---|
184 | case "rowFields":
|
---|
185 | if (field.IsColumnField || field.IsPageField)
|
---|
186 | {
|
---|
187 | throw(new Exception("This field is a column or page field. Can't add it to the RowFields collection"));
|
---|
188 | }
|
---|
189 | field.IsRowField = value;
|
---|
190 | field.Axis = ePivotFieldAxis.Row;
|
---|
191 | break;
|
---|
192 | case "colFields":
|
---|
193 | if (field.IsRowField || field.IsPageField)
|
---|
194 | {
|
---|
195 | throw (new Exception("This field is a row or page field. Can't add it to the ColumnFields collection"));
|
---|
196 | }
|
---|
197 | field.IsColumnField = value;
|
---|
198 | field.Axis = ePivotFieldAxis.Column;
|
---|
199 | break;
|
---|
200 | case "pageFields":
|
---|
201 | if (field.IsColumnField || field.IsRowField)
|
---|
202 | {
|
---|
203 | throw (new Exception("Field is a column or row field. Can't add it to the PageFields collection"));
|
---|
204 | }
|
---|
205 | if (_table.Address._fromRow < 3)
|
---|
206 | {
|
---|
207 | throw(new Exception(string.Format("A pivot table with page fields must be located above row 3. Currenct location is {0}", _table.Address.Address)));
|
---|
208 | }
|
---|
209 | field.IsPageField = value;
|
---|
210 | field.Axis = ePivotFieldAxis.Page;
|
---|
211 | break;
|
---|
212 | case "dataFields":
|
---|
213 |
|
---|
214 | break;
|
---|
215 | }
|
---|
216 | }
|
---|
217 | /// <summary>
|
---|
218 | /// Remove a field
|
---|
219 | /// </summary>
|
---|
220 | /// <param name="Field"></param>
|
---|
221 | public void Remove(ExcelPivotTableField Field)
|
---|
222 | {
|
---|
223 | if(!_list.Contains(Field))
|
---|
224 | {
|
---|
225 | throw new ArgumentException("Field not in collection");
|
---|
226 | }
|
---|
227 | SetFlag(Field, false);
|
---|
228 | _list.Remove(Field);
|
---|
229 | }
|
---|
230 | /// <summary>
|
---|
231 | /// Remove a field at a specific position
|
---|
232 | /// </summary>
|
---|
233 | /// <param name="Index"></param>
|
---|
234 | public void RemoveAt(int Index)
|
---|
235 | {
|
---|
236 | if (Index > -1 && Index < _list.Count)
|
---|
237 | {
|
---|
238 | throw(new IndexOutOfRangeException());
|
---|
239 | }
|
---|
240 | SetFlag(_list[Index], false);
|
---|
241 | _list.RemoveAt(Index);
|
---|
242 | }
|
---|
243 | }
|
---|
244 | /// <summary>
|
---|
245 | /// Collection class for data fields in a Pivottable
|
---|
246 | /// </summary>
|
---|
247 | public class ExcelPivotTableDataFieldCollection : ExcelPivotTableFieldCollectionBase<ExcelPivotTableDataField>
|
---|
248 | {
|
---|
249 | internal ExcelPivotTableDataFieldCollection(ExcelPivotTable table) :
|
---|
250 | base(table)
|
---|
251 | {
|
---|
252 |
|
---|
253 | }
|
---|
254 | /// <summary>
|
---|
255 | /// Add a new datafield
|
---|
256 | /// </summary>
|
---|
257 | /// <param name="field">The field</param>
|
---|
258 | /// <returns>The new datafield</returns>
|
---|
259 | public ExcelPivotTableDataField Add(ExcelPivotTableField field)
|
---|
260 | {
|
---|
261 | var dataFieldsNode = field.TopNode.SelectSingleNode("../../d:dataFields", field.NameSpaceManager);
|
---|
262 | if (dataFieldsNode == null)
|
---|
263 | {
|
---|
264 | _table.CreateNode("d:dataFields");
|
---|
265 | dataFieldsNode = field.TopNode.SelectSingleNode("../../d:dataFields", field.NameSpaceManager);
|
---|
266 | }
|
---|
267 |
|
---|
268 | XmlElement node = _table.PivotTableXml.CreateElement("dataField", ExcelPackage.schemaMain);
|
---|
269 | node.SetAttribute("fld", field.Index.ToString());
|
---|
270 | dataFieldsNode.AppendChild(node);
|
---|
271 |
|
---|
272 | //XmlElement node = field.AppendField(dataFieldsNode, field.Index, "dataField", "fld");
|
---|
273 | field.SetXmlNodeBool("@dataField", true,false);
|
---|
274 |
|
---|
275 | var dataField = new ExcelPivotTableDataField(field.NameSpaceManager, node, field);
|
---|
276 | ValidateDupName(dataField);
|
---|
277 |
|
---|
278 | _list.Add(dataField);
|
---|
279 | return dataField;
|
---|
280 | }
|
---|
281 | private void ValidateDupName(ExcelPivotTableDataField dataField)
|
---|
282 | {
|
---|
283 | if(ExistsDfName(dataField.Field.Name, null))
|
---|
284 | {
|
---|
285 | var index = 2;
|
---|
286 | string name;
|
---|
287 | do
|
---|
288 | {
|
---|
289 | name = dataField.Field.Name + "_" + index++.ToString();
|
---|
290 | }
|
---|
291 | while (ExistsDfName(name,null));
|
---|
292 | dataField.Name = name;
|
---|
293 | }
|
---|
294 | }
|
---|
295 |
|
---|
296 | internal bool ExistsDfName(string name, ExcelPivotTableDataField datafield)
|
---|
297 | {
|
---|
298 | name = name.ToLower();
|
---|
299 | foreach (var df in _list)
|
---|
300 | {
|
---|
301 | if (((!string.IsNullOrEmpty(df.Name) && df.Name.ToLower() == name) ||
|
---|
302 | (string.IsNullOrEmpty(df.Name) && df.Field.Name.ToLower() == name)) && datafield != df)
|
---|
303 | {
|
---|
304 | return true;
|
---|
305 | }
|
---|
306 | }
|
---|
307 | return false;
|
---|
308 | }
|
---|
309 | /// <summary>
|
---|
310 | /// Remove a datafield
|
---|
311 | /// </summary>
|
---|
312 | /// <param name="dataField"></param>
|
---|
313 | public void Remove(ExcelPivotTableDataField dataField)
|
---|
314 | {
|
---|
315 | XmlElement node = dataField.Field.TopNode.SelectSingleNode(string.Format("../../d:dataFields/d:dataField[@fld={0}]", dataField.Index), dataField.NameSpaceManager) as XmlElement;
|
---|
316 | if (node != null)
|
---|
317 | {
|
---|
318 | node.ParentNode.RemoveChild(node);
|
---|
319 | }
|
---|
320 | _list.Remove(dataField);
|
---|
321 | }
|
---|
322 | }
|
---|
323 | } |
---|