Free cookie consent management tool by TermsFeed Policy Generator

source: branches/ScopedAlgorithms/HeuristicLab.ExtLibs/HeuristicLab.EPPlus/4.0.3/EPPlus-4.0.3/ExcelNamedRangeCollection.cs @ 14544

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

#2341: Added EPPlus-4.0.3 to ExtLibs

File size: 7.5 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 this class            2010-01-28
30 * Jan Källman    License changed GPL-->LGPL 2011-12-27
31 *******************************************************************************/
32using System;
33using System.Collections.Generic;
34using System.Text;
35using System.Collections;
36
37namespace OfficeOpenXml
38{
39    /// <summary>
40    /// Collection for named ranges
41    /// </summary>
42    public class ExcelNamedRangeCollection : IEnumerable<ExcelNamedRange>
43    {
44        internal ExcelWorksheet _ws;
45        internal ExcelWorkbook _wb;
46        internal ExcelNamedRangeCollection(ExcelWorkbook wb)
47        {
48            _wb = wb;
49            _ws = null;
50        }
51        internal ExcelNamedRangeCollection(ExcelWorkbook wb, ExcelWorksheet ws)
52        {
53            _wb = wb;
54            _ws = ws;
55        }
56        List<ExcelNamedRange> _list = new List<ExcelNamedRange>();
57        Dictionary<string, int> _dic = new Dictionary<string, int>(StringComparer.InvariantCultureIgnoreCase);
58        /// <summary>
59        /// Add a new named range
60        /// </summary>
61        /// <param name="Name">The name</param>
62        /// <param name="Range">The range</param>
63        /// <returns></returns>
64        public ExcelNamedRange Add(string Name, ExcelRangeBase Range)
65        {
66            ExcelNamedRange item;
67            if (Range.IsName)
68            {
69
70                item = new ExcelNamedRange(Name, _wb,_ws, _dic.Count);
71            }
72            else
73            {
74                item = new ExcelNamedRange(Name, _ws, Range.Worksheet, Range.Address, _dic.Count);
75            }
76
77            AddName(Name, item);
78
79            return item;
80        }
81
82        private void AddName(string Name, ExcelNamedRange item)
83        {
84            _dic.Add(Name, _list.Count);
85            _list.Add(item);
86        }
87        /// <summary>
88        /// Add a defined name referencing value
89        /// </summary>
90        /// <param name="Name"></param>
91        /// <param name="value"></param>
92        /// <returns></returns>
93        public ExcelNamedRange AddValue(string Name, object value)
94        {
95            var item = new ExcelNamedRange(Name,_wb, _ws, _dic.Count);
96            item.NameValue = value;
97            AddName(Name, item);
98            return item;
99        }
100
101        /// <summary>
102        /// Add a defined name referencing a formula -- the method name contains a typo.
103        /// This method is obsolete and will be removed in the future.
104        /// Use <see cref="AddFormula"/>
105        /// </summary>
106        /// <param name="Name"></param>
107        /// <param name="Formula"></param>
108        /// <returns></returns>
109        [Obsolete("Call AddFormula() instead.  See Issue Tracker Id #14687")]
110        public ExcelNamedRange AddFormla(string Name, string Formula)
111        {
112            return  this.AddFormula(Name, Formula);
113        }
114
115        /// <summary>
116        /// Add a defined name referencing a formula
117        /// </summary>
118        /// <param name="Name"></param>
119        /// <param name="Formula"></param>
120        /// <returns></returns>
121        public ExcelNamedRange AddFormula(string Name, string Formula)
122        {
123            var item = new ExcelNamedRange(Name, _wb, _ws, _dic.Count);
124            item.NameFormula = Formula;
125            AddName(Name, item);
126            return item;
127        }
128        /// <summary>
129        /// Remove a defined name from the collection
130        /// </summary>
131        /// <param name="Name">The name</param>
132        public void Remove(string Name)
133        {
134            if(_dic.ContainsKey(Name))
135            {
136                var ix = _dic[Name];
137
138                for (int i = ix+1; i < _list.Count; i++)
139                {
140                    _dic.Remove(_list[i].Name);
141                    _list[i].Index--;
142                    _dic.Add(_list[i].Name, _list[i].Index);
143                }
144                _dic.Remove(Name);
145                _list.RemoveAt(ix);
146            }
147        }
148        /// <summary>
149        /// Checks collection for the presence of a key
150        /// </summary>
151        /// <param name="key">key to search for</param>
152        /// <returns>true if the key is in the collection</returns>
153        public bool ContainsKey(string key)
154        {
155            return _dic.ContainsKey(key);
156        }
157        /// <summary>
158        /// The current number of items in the collection
159        /// </summary>
160        public int Count
161        {
162            get
163            {
164                return _dic.Count;
165            }
166        }
167        /// <summary>
168        /// Name indexer
169        /// </summary>
170        /// <param name="Name">The name (key) for a Named range</param>
171        /// <returns>a reference to the range</returns>
172        /// <remarks>
173        /// Throws a KeyNotFoundException if the key is not in the collection.
174        /// </remarks>
175        public ExcelNamedRange this[string Name]
176        {
177            get
178            {
179                return _list[_dic[Name]];
180            }
181        }
182        public ExcelNamedRange this[int Index]
183        {
184            get
185            {
186                return _list[Index];
187            }
188        }
189
190        #region "IEnumerable"
191        #region IEnumerable<ExcelNamedRange> Members
192        /// <summary>
193        /// Implement interface method IEnumerator&lt;ExcelNamedRange&gt; GetEnumerator()
194        /// </summary>
195        /// <returns></returns>
196        public IEnumerator<ExcelNamedRange> GetEnumerator()
197        {
198            return _list.GetEnumerator();
199        }
200        #endregion
201        #region IEnumerable Members
202        /// <summary>
203        /// Implement interface method IEnumeratable GetEnumerator()
204        /// </summary>
205        /// <returns></returns>
206        IEnumerator IEnumerable.GetEnumerator()
207        {
208            return _list.GetEnumerator();
209        }
210
211        #endregion
212        #endregion
213
214        internal void Clear()
215        {
216            while(Count>0)
217            {
218                Remove(_list[0].Name);
219            }
220        }
221    }
222}
Note: See TracBrowser for help on using the repository browser.