/******************************************************************************* * You may amend and distribute as you like, but don't remove this header! * * EPPlus provides server-side generation of Excel 2007/2010 spreadsheets. * See http://www.codeplex.com/EPPlus for details. * * Copyright (C) 2011 Jan Källman * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) any later version. * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. * See the GNU Lesser General Public License for more details. * * The GNU Lesser General Public License can be viewed at http://www.opensource.org/licenses/lgpl-license.php * If you unfamiliar with this license or have questions about it, here is an http://www.gnu.org/licenses/gpl-faq.html * * All code and executables are provided "as is" with no warranty either express or implied. * The author accepts no liability for any damage or loss of business that this product may cause. * * Code change notes: * * Author Change Date * ****************************************************************************** * Jan Källman Added this class 2010-01-28 * Jan Källman License changed GPL-->LGPL 2011-12-27 *******************************************************************************/ using System; using System.Collections.Generic; using System.Text; using System.Collections; namespace OfficeOpenXml { /// /// Collection for named ranges /// public class ExcelNamedRangeCollection : IEnumerable { internal ExcelWorksheet _ws; internal ExcelWorkbook _wb; internal ExcelNamedRangeCollection(ExcelWorkbook wb) { _wb = wb; _ws = null; } internal ExcelNamedRangeCollection(ExcelWorkbook wb, ExcelWorksheet ws) { _wb = wb; _ws = ws; } Dictionary _dic = new Dictionary(); /// /// Add a new named range /// /// The name /// The range /// public ExcelNamedRange Add(string Name, ExcelRangeBase Range) { ExcelNamedRange item; if (Range.IsName) { item = new ExcelNamedRange(Name, _wb,_ws); } else { item = new ExcelNamedRange(Name, _ws, Range.Worksheet, Range.Address); } _dic.Add(Name.ToLower(), item); return item; } /// /// Add a defined name referencing value /// /// /// /// public ExcelNamedRange AddValue(string Name, object value) { var item = new ExcelNamedRange(Name,_wb, _ws); item.NameValue = value; _dic.Add(Name.ToLower(), item); return item; } /// /// Add a defined name referencing a formula -- the method name contains a typo. /// This method is obsolete and will be removed in the future. /// Use /// /// /// /// [Obsolete("Call AddFormula() instead. See Issue Tracker Id #14687")] public ExcelNamedRange AddFormla(string Name, string Formula) { return this.AddFormula(Name, Formula); } /// /// Add a defined name referencing a formula /// /// /// /// public ExcelNamedRange AddFormula(string Name, string Formula) { var item = new ExcelNamedRange(Name, _wb, _ws); item.NameFormula = Formula; _dic.Add(Name.ToLower(), item); return item; } /// /// Remove a defined name from the collection /// /// The name public void Remove(string Name) { _dic.Remove(Name.ToLower()); } /// /// Checks collection for the presence of a key /// /// key to search for /// true if the key is in the collection public bool ContainsKey(string key) { return _dic.ContainsKey(key.ToLower()); } /// /// The current number of items in the collection /// public int Count { get { return _dic.Count; } } /// /// Name indexer /// /// The name (key) for a Named range /// a reference to the range /// /// Throws a KeyNotFoundException if the key is not in the collection. /// public ExcelNamedRange this[string Name] { get { return _dic[Name.ToLower()]; } } #region "IEnumerable" #region IEnumerable Members /// /// Implement interface method IEnumerator<ExcelNamedRange> GetEnumerator() /// /// public IEnumerator GetEnumerator() { return _dic.Values.GetEnumerator(); } #endregion #region IEnumerable Members /// /// Implement interface method IEnumeratable GetEnumerator() /// /// IEnumerator IEnumerable.GetEnumerator() { return _dic.Values.GetEnumerator(); } #endregion #endregion } }