|  | /******************************************************************************* | 
|  | * 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; | 
|  | using System.Collections.Generic; | 
|  |  | 
|  | namespace OfficeOpenXml; | 
|  |  | 
|  | /// <summary> | 
|  | /// Collection for named ranges | 
|  | /// </summary> | 
|  | public class ExcelNamedRangeCollection : IEnumerable<ExcelNamedRange> { | 
|  | internal ExcelWorksheet _ws; | 
|  | internal ExcelWorkbook _wb; | 
|  |  | 
|  | internal ExcelNamedRangeCollection(ExcelWorkbook wb) { | 
|  | _wb = wb; | 
|  | _ws = null; | 
|  | } | 
|  |  | 
|  | internal ExcelNamedRangeCollection(ExcelWorkbook wb, ExcelWorksheet ws) { | 
|  | _wb = wb; | 
|  | _ws = ws; | 
|  | } | 
|  |  | 
|  | private List<ExcelNamedRange> _list = new(); | 
|  | private Dictionary<string, int> _dic = new(StringComparer.InvariantCultureIgnoreCase); | 
|  |  | 
|  | /// <summary> | 
|  | /// Add a new named range | 
|  | /// </summary> | 
|  | /// <param name="name">The name</param> | 
|  | /// <param name="range">The range</param> | 
|  | /// <returns></returns> | 
|  | public ExcelNamedRange Add(string name, ExcelRangeBase range) { | 
|  | ExcelNamedRange item; | 
|  | if (range.IsName) { | 
|  | item = new(name, _wb, _ws, _dic.Count); | 
|  | } else { | 
|  | item = new(name, _ws, range.Worksheet, range.Address, _dic.Count); | 
|  | } | 
|  |  | 
|  | AddName(name, item); | 
|  |  | 
|  | return item; | 
|  | } | 
|  |  | 
|  | private void AddName(string name, ExcelNamedRange item) { | 
|  | // Is 'Name' already present | 
|  | if (_dic.ContainsKey(name)) { | 
|  | int index = _dic[name]; | 
|  | if ((0 <= index) && (index < _list.Count)) { | 
|  | ExcelNamedRange listItem = _list[index]; | 
|  | if ((listItem != null) | 
|  | && (listItem.FullAddress != null) | 
|  | && (listItem.FullAddress == item.FullAddress)) { | 
|  | return; | 
|  | } | 
|  |  | 
|  | //throw new Exception(string.Format("Name '{0}' is defined in the worksheet more than once. First as '{1}' and second as '{2}'.", Name, listItem.FullAddress, item.FullAddress)); | 
|  | return; | 
|  | } | 
|  |  | 
|  | //throw new Exception(string.Format("Name '{0}' is defined in the worksheet more than once.", Name)); | 
|  | return; | 
|  | } | 
|  |  | 
|  | _dic.Add(name, _list.Count); | 
|  | _list.Add(item); | 
|  | } | 
|  |  | 
|  | /// <summary> | 
|  | /// Add a defined name referencing value | 
|  | /// </summary> | 
|  | /// <param name="name"></param> | 
|  | /// <param name="value"></param> | 
|  | /// <returns></returns> | 
|  | public ExcelNamedRange AddValue(string name, object value) { | 
|  | var item = new ExcelNamedRange(name, _wb, _ws, _dic.Count); | 
|  | item.NameValue = value; | 
|  | AddName(name, item); | 
|  | return item; | 
|  | } | 
|  |  | 
|  | /// <summary> | 
|  | /// 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 <see cref="AddFormula"/> | 
|  | /// </summary> | 
|  | /// <param name="name"></param> | 
|  | /// <param name="formula"></param> | 
|  | /// <returns></returns> | 
|  | [Obsolete("Call AddFormula() instead.  See Issue Tracker Id #14687")] | 
|  | public ExcelNamedRange AddFormla(string name, string formula) { | 
|  | return AddFormula(name, formula); | 
|  | } | 
|  |  | 
|  | /// <summary> | 
|  | /// Add a defined name referencing a formula | 
|  | /// </summary> | 
|  | /// <param name="name"></param> | 
|  | /// <param name="formula"></param> | 
|  | /// <returns></returns> | 
|  | public ExcelNamedRange AddFormula(string name, string formula) { | 
|  | var item = new ExcelNamedRange(name, _wb, _ws, _dic.Count); | 
|  | item.NameFormula = formula; | 
|  | AddName(name, item); | 
|  | return item; | 
|  | } | 
|  |  | 
|  | /// <summary> | 
|  | /// Remove a defined name from the collection | 
|  | /// </summary> | 
|  | /// <param name="name">The name</param> | 
|  | public void Remove(string name) { | 
|  | if (_dic.ContainsKey(name)) { | 
|  | var ix = _dic[name]; | 
|  |  | 
|  | for (int i = ix + 1; i < _list.Count; i++) { | 
|  | _dic.Remove(_list[i].Name); | 
|  | _list[i].Index--; | 
|  | _dic.Add(_list[i].Name, _list[i].Index); | 
|  | } | 
|  | _dic.Remove(name); | 
|  | _list.RemoveAt(ix); | 
|  | } | 
|  | } | 
|  |  | 
|  | /// <summary> | 
|  | /// Checks collection for the presence of a key | 
|  | /// </summary> | 
|  | /// <param name="key">key to search for</param> | 
|  | /// <returns>true if the key is in the collection</returns> | 
|  | public bool ContainsKey(string key) { | 
|  | return _dic.ContainsKey(key); | 
|  | } | 
|  |  | 
|  | /// <summary> | 
|  | /// The current number of items in the collection | 
|  | /// </summary> | 
|  | public int Count => _dic.Count; | 
|  |  | 
|  | /// <summary> | 
|  | /// Name indexer | 
|  | /// </summary> | 
|  | /// <param name="name">The name (key) for a Named range</param> | 
|  | /// <returns>a reference to the range</returns> | 
|  | /// <remarks> | 
|  | /// Throws a KeyNotFoundException if the key is not in the collection. | 
|  | /// </remarks> | 
|  | public ExcelNamedRange this[string name] { | 
|  | get { return _list[_dic[name]]; } | 
|  | } | 
|  |  | 
|  | public ExcelNamedRange this[int index] { | 
|  | get { return _list[index]; } | 
|  | } | 
|  |  | 
|  | /// <summary> | 
|  | /// Implement interface method IEnumerator<ExcelNamedRange> GetEnumerator() | 
|  | /// </summary> | 
|  | /// <returns></returns> | 
|  | public IEnumerator<ExcelNamedRange> GetEnumerator() { | 
|  | return _list.GetEnumerator(); | 
|  | } | 
|  |  | 
|  | /// <summary> | 
|  | /// Implement interface method IEnumeratable GetEnumerator() | 
|  | /// </summary> | 
|  | /// <returns></returns> | 
|  | IEnumerator IEnumerable.GetEnumerator() { | 
|  | return _list.GetEnumerator(); | 
|  | } | 
|  |  | 
|  | internal void Clear() { | 
|  | while (Count > 0) { | 
|  | Remove(_list[0].Name); | 
|  | } | 
|  | } | 
|  | } |