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 | *******************************************************************************/
|
---|
32 | using System;
|
---|
33 | using System.Collections.Generic;
|
---|
34 | using System.Text;
|
---|
35 | using System.Collections;
|
---|
36 |
|
---|
37 | namespace 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 | Dictionary<string, ExcelNamedRange> _dic = new Dictionary<string, ExcelNamedRange>();
|
---|
57 | /// <summary>
|
---|
58 | /// Add a new named range
|
---|
59 | /// </summary>
|
---|
60 | /// <param name="Name">The name</param>
|
---|
61 | /// <param name="Range">The range</param>
|
---|
62 | /// <returns></returns>
|
---|
63 | public ExcelNamedRange Add(string Name, ExcelRangeBase Range)
|
---|
64 | {
|
---|
65 | ExcelNamedRange item;
|
---|
66 | if (Range.IsName)
|
---|
67 | {
|
---|
68 |
|
---|
69 | item = new ExcelNamedRange(Name, _wb,_ws);
|
---|
70 | }
|
---|
71 | else
|
---|
72 | {
|
---|
73 | item = new ExcelNamedRange(Name, _ws, Range.Worksheet, Range.Address);
|
---|
74 | }
|
---|
75 |
|
---|
76 | _dic.Add(Name.ToLower(), item);
|
---|
77 | return item;
|
---|
78 | }
|
---|
79 | /// <summary>
|
---|
80 | /// Add a defined name referencing value
|
---|
81 | /// </summary>
|
---|
82 | /// <param name="Name"></param>
|
---|
83 | /// <param name="value"></param>
|
---|
84 | /// <returns></returns>
|
---|
85 | public ExcelNamedRange AddValue(string Name, object value)
|
---|
86 | {
|
---|
87 | var item = new ExcelNamedRange(Name,_wb, _ws);
|
---|
88 | item.NameValue = value;
|
---|
89 | _dic.Add(Name.ToLower(), item);
|
---|
90 | return item;
|
---|
91 | }
|
---|
92 | /// <summary>
|
---|
93 | /// Add a defined name referencing a formula -- the method name contains a typo.
|
---|
94 | /// This method is obsolete and will be removed in the future.
|
---|
95 | /// Use <see cref="AddFormula"/>
|
---|
96 | /// </summary>
|
---|
97 | /// <param name="Name"></param>
|
---|
98 | /// <param name="Formula"></param>
|
---|
99 | /// <returns></returns>
|
---|
100 | [Obsolete("Call AddFormula() instead. See Issue Tracker Id #14687")]
|
---|
101 | public ExcelNamedRange AddFormla(string Name, string Formula)
|
---|
102 | {
|
---|
103 | return this.AddFormula(Name, Formula);
|
---|
104 | }
|
---|
105 |
|
---|
106 | /// <summary>
|
---|
107 | /// Add a defined name referencing a formula
|
---|
108 | /// </summary>
|
---|
109 | /// <param name="Name"></param>
|
---|
110 | /// <param name="Formula"></param>
|
---|
111 | /// <returns></returns>
|
---|
112 | public ExcelNamedRange AddFormula(string Name, string Formula)
|
---|
113 | {
|
---|
114 | var item = new ExcelNamedRange(Name, _wb, _ws);
|
---|
115 | item.NameFormula = Formula;
|
---|
116 | _dic.Add(Name.ToLower(), item);
|
---|
117 | return item;
|
---|
118 | }
|
---|
119 | /// <summary>
|
---|
120 | /// Remove a defined name from the collection
|
---|
121 | /// </summary>
|
---|
122 | /// <param name="Name">The name</param>
|
---|
123 | public void Remove(string Name)
|
---|
124 | {
|
---|
125 | _dic.Remove(Name.ToLower());
|
---|
126 | }
|
---|
127 | /// <summary>
|
---|
128 | /// Checks collection for the presence of a key
|
---|
129 | /// </summary>
|
---|
130 | /// <param name="key">key to search for</param>
|
---|
131 | /// <returns>true if the key is in the collection</returns>
|
---|
132 | public bool ContainsKey(string key)
|
---|
133 | {
|
---|
134 | return _dic.ContainsKey(key.ToLower());
|
---|
135 | }
|
---|
136 | /// <summary>
|
---|
137 | /// The current number of items in the collection
|
---|
138 | /// </summary>
|
---|
139 | public int Count
|
---|
140 | {
|
---|
141 | get
|
---|
142 | {
|
---|
143 | return _dic.Count;
|
---|
144 | }
|
---|
145 | }
|
---|
146 | /// <summary>
|
---|
147 | /// Name indexer
|
---|
148 | /// </summary>
|
---|
149 | /// <param name="Name">The name (key) for a Named range</param>
|
---|
150 | /// <returns>a reference to the range</returns>
|
---|
151 | /// <remarks>
|
---|
152 | /// Throws a KeyNotFoundException if the key is not in the collection.
|
---|
153 | /// </remarks>
|
---|
154 | public ExcelNamedRange this[string Name]
|
---|
155 | {
|
---|
156 | get
|
---|
157 | {
|
---|
158 | return _dic[Name.ToLower()];
|
---|
159 | }
|
---|
160 | }
|
---|
161 |
|
---|
162 | #region "IEnumerable"
|
---|
163 | #region IEnumerable<ExcelNamedRange> Members
|
---|
164 | /// <summary>
|
---|
165 | /// Implement interface method IEnumerator<ExcelNamedRange> GetEnumerator()
|
---|
166 | /// </summary>
|
---|
167 | /// <returns></returns>
|
---|
168 | public IEnumerator<ExcelNamedRange> GetEnumerator()
|
---|
169 | {
|
---|
170 | return _dic.Values.GetEnumerator();
|
---|
171 | }
|
---|
172 | #endregion
|
---|
173 | #region IEnumerable Members
|
---|
174 | /// <summary>
|
---|
175 | /// Implement interface method IEnumeratable GetEnumerator()
|
---|
176 | /// </summary>
|
---|
177 | /// <returns></returns>
|
---|
178 | IEnumerator IEnumerable.GetEnumerator()
|
---|
179 | {
|
---|
180 | return _dic.Values.GetEnumerator();
|
---|
181 | }
|
---|
182 |
|
---|
183 | #endregion
|
---|
184 | #endregion
|
---|
185 | }
|
---|
186 | }
|
---|