Free cookie consent management tool by TermsFeed Policy Generator

source: branches/Async/HeuristicLab.ExtLibs/HeuristicLab.EPPlus/4.0.3/EPPlus-4.0.3/Table/PivotTable/ExcelPivotTableCollection.cs @ 13401

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

#2341: Added EPPlus-4.0.3 to ExtLibs

File size: 6.3 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 * Jan Källman    Added   21-MAR-2011
30 * Jan Källman    License changed GPL-->LGPL 2011-12-16
31 *******************************************************************************/
32using System;
33using System.Collections.Generic;
34using System.Text;
35using System.Xml;
36
37namespace OfficeOpenXml.Table.PivotTable
38{
39    /// <summary>
40    /// A collection of pivottable objects
41    /// </summary>
42    public class ExcelPivotTableCollection : IEnumerable<ExcelPivotTable>
43    {
44        List<ExcelPivotTable> _pivotTables = new List<ExcelPivotTable>();
45        internal Dictionary<string, int> _pivotTableNames = new Dictionary<string, int>();
46        ExcelWorksheet _ws;       
47        internal ExcelPivotTableCollection(ExcelWorksheet ws)
48        {
49            var pck = ws._package.Package;
50            _ws = ws;           
51            foreach(var rel in ws.Part.GetRelationships())
52            {
53                if (rel.RelationshipType == ExcelPackage.schemaRelationships + "/pivotTable")
54                {
55                    var tbl = new ExcelPivotTable(rel, ws);
56                    _pivotTableNames.Add(tbl.Name, _pivotTables.Count);
57                    if (tbl.Id + 1 > _ws.Workbook._nextPivotTableID) _ws.Workbook._nextPivotTableID = tbl.Id + 1;
58                    _pivotTables.Add(tbl);
59                }
60            }
61        }
62        private ExcelPivotTable Add(ExcelPivotTable tbl)
63        {
64            _pivotTables.Add(tbl);
65            _pivotTableNames.Add(tbl.Name, _pivotTables.Count - 1);
66            if (tbl.Id >= _ws.Workbook._nextPivotTableID)
67            {
68                _ws.Workbook._nextPivotTableID = tbl.Id + 1;
69            }
70            return tbl;
71        }
72
73        /// <summary>
74        /// Create a pivottable on the supplied range
75        /// </summary>
76        /// <param name="Range">The range address including header and total row</param>
77        /// <param name="Source">The Source data range address</param>
78        /// <param name="Name">The name of the table. Must be unique </param>
79        /// <returns>The pivottable object</returns>
80        public ExcelPivotTable Add(ExcelAddressBase Range, ExcelRangeBase Source, string Name)
81        {
82            if (string.IsNullOrEmpty(Name))
83            {
84                Name = GetNewTableName();
85            }
86            if (Range.WorkSheet != _ws.Name)
87            {
88                throw(new Exception("The Range must be in the current worksheet"));
89            }
90            else if (_ws.Workbook.ExistsTableName(Name))
91            {
92                throw (new ArgumentException("Tablename is not unique"));
93            }
94            foreach (var t in _pivotTables)
95            {
96                if (t.Address.Collide(Range) != ExcelAddressBase.eAddressCollition.No)
97                {
98                    throw (new ArgumentException(string.Format("Table range collides with table {0}", t.Name)));
99                }
100            }
101            return Add(new ExcelPivotTable(_ws, Range, Source, Name, _ws.Workbook._nextPivotTableID++));
102        }
103
104        internal string GetNewTableName()
105        {
106            string name = "Pivottable1";
107            int i = 2;
108            while (_ws.Workbook.ExistsPivotTableName(name))
109            {
110                name = string.Format("Pivottable{0}", i++);
111            }
112            return name;
113        }
114        public int Count
115        {
116            get
117            {
118                return _pivotTables.Count;
119            }
120        }
121        /// <summary>
122        /// The pivottable Index. Base 0.
123        /// </summary>
124        /// <param name="Index"></param>
125        /// <returns></returns>
126        public ExcelPivotTable this[int Index]
127        {
128            get
129            {
130                if (Index < 0 || Index >= _pivotTables.Count)
131                {
132                    throw (new ArgumentOutOfRangeException("PivotTable index out of range"));
133                }
134                return _pivotTables[Index];
135            }
136        }
137        /// <summary>
138        /// Pivottabes accesed by name
139        /// </summary>
140        /// <param name="Name">The name of the pivottable</param>
141        /// <returns>The Pivotable. Null if the no match is found</returns>
142        public ExcelPivotTable this[string Name]
143        {
144            get
145            {
146                if (_pivotTableNames.ContainsKey(Name))
147                {
148                    return _pivotTables[_pivotTableNames[Name]];
149                }
150                else
151                {
152                    return null;
153                }
154            }
155        }
156        public IEnumerator<ExcelPivotTable> GetEnumerator()
157        {
158            return _pivotTables.GetEnumerator();
159        }
160
161        System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
162        {
163            return _pivotTables.GetEnumerator();
164        }
165    }
166}
Note: See TracBrowser for help on using the repository browser.