Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2839_HiveProjectManagement/HeuristicLab.ExtLibs/HeuristicLab.EPPlus/4.0.3/EPPlus-4.0.3/VBA/ExcelVbaModuleCollection.cs @ 15761

Last change on this file since 15761 was 12463, checked in by ascheibe, 10 years ago

#2399 fixed EPPlus so that it compiles on Linux

File size: 8.1 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   12-APR-2012
30 *******************************************************************************/
31using System;
32using System.Collections.Generic;
33using System.Linq;
34using System.Text;
35
36namespace OfficeOpenXml.VBA
37{
38    /// <summary>
39    /// Base class for VBA collections
40    /// </summary>
41    /// <typeparam name="T"></typeparam>
42    public class ExcelVBACollectionBase<T> : IEnumerable<T>
43    {
44        internal protected List<T> _list=new List<T>();       
45        public IEnumerator<T> GetEnumerator()
46        {
47            return _list.GetEnumerator();
48        }
49
50        System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
51        {
52            return _list.GetEnumerator();
53        }
54        /// <summary>
55        /// Indexer
56        /// </summary>
57        /// <param name="Name">Name</param>
58        /// <returns></returns>
59        public T this [string Name]
60        {
61            get
62            {
63                return _list.Find((f) => f.GetType().GetProperty("Name").GetValue(f, null).ToString().Equals(Name,StringComparison.InvariantCultureIgnoreCase));
64            }
65        }
66        /// <summary>
67        /// Indexer
68        /// </summary>
69        /// <param name="Index">Position</param>
70        /// <returns></returns>
71        public T this[int Index]
72        {
73            get
74            {
75                return _list[Index];
76            }
77        }
78        /// <summary>
79        /// Number of items in the collection
80        /// </summary>
81        public int Count
82        {
83            get { return _list.Count; }
84        }
85        /// <summary>
86        /// If a specific name exists in the collection
87        /// </summary>
88        /// <param name="Name">The name</param>
89        /// <returns>True if the name exists</returns>
90        public bool Exists(string Name)
91        {
92            return _list.Exists((f) => f.GetType().GetProperty("Name").GetValue(f, null).ToString().Equals(Name,StringComparison.InvariantCultureIgnoreCase));
93        }
94        /// <summary>
95        /// Removes the item
96        /// </summary>
97        /// <param name="Item"></param>
98        public void Remove(T Item)
99        {
100            _list.Remove(Item);
101        }
102        /// <summary>
103        /// Removes the item at the specified index
104        /// </summary>
105        /// <param name="index">THe index</param>
106        public void RemoveAt(int index)
107        {
108            _list.RemoveAt(index);
109        }
110       
111        internal void Clear()
112        {
113            _list.Clear();
114        }
115    }
116    /// <summary>
117    /// Collection class for VBA modules
118    /// </summary>
119    public class ExcelVbaModuleCollection : ExcelVBACollectionBase<ExcelVBAModule>
120    {
121        ExcelVbaProject _project;
122        internal ExcelVbaModuleCollection (ExcelVbaProject project)
123      {
124            _project=project;
125      }
126        internal void Add(ExcelVBAModule Item)
127        {
128            _list.Add(Item);
129        }
130        /// <summary>
131        /// Adds a new VBA Module
132        /// </summary>
133        /// <param name="Name">The name of the module</param>
134        /// <returns>The module object</returns>
135        public ExcelVBAModule AddModule(string Name)
136        {
137            if (this[Name] != null)
138            {
139                throw(new ArgumentException("Vba modulename already exist."));
140            }
141            var m = new ExcelVBAModule();
142            m.Name = Name;
143            m.Type = eModuleType.Module;
144            m.Attributes._list.Add(new ExcelVbaModuleAttribute() { Name = "VB_Name", Value = Name, DataType = eAttributeDataType.String });
145            m.Type = eModuleType.Module;
146            _list.Add(m);
147            return m;
148        }
149        /// <summary>
150        /// Adds a new VBA class
151        /// </summary>
152        /// <param name="Name">The name of the class</param>
153        /// <param name="Exposed">Private or Public not createble</param>
154        /// <returns>The class object</returns>
155        public ExcelVBAModule AddClass(string Name, bool Exposed)
156        {
157            var m = new ExcelVBAModule();
158            m.Name = Name;           
159            m.Type = eModuleType.Class;
160            m.Attributes._list.Add(new ExcelVbaModuleAttribute() { Name = "VB_Name", Value = Name, DataType = eAttributeDataType.String });
161            m.Attributes._list.Add(new ExcelVbaModuleAttribute() { Name = "VB_Base", Value = "0{FCFB3D2A-A0FA-1068-A738-08002B3371B5}", DataType = eAttributeDataType.String });
162            m.Attributes._list.Add(new ExcelVbaModuleAttribute() { Name = "VB_GlobalNameSpace", Value = "False", DataType = eAttributeDataType.NonString });
163            m.Attributes._list.Add(new ExcelVbaModuleAttribute() { Name = "VB_Creatable", Value = "False", DataType = eAttributeDataType.NonString });
164            m.Attributes._list.Add(new ExcelVbaModuleAttribute() { Name = "VB_PredeclaredId", Value = "False", DataType = eAttributeDataType.NonString });
165            m.Attributes._list.Add(new ExcelVbaModuleAttribute() { Name = "VB_Exposed", Value = Exposed ? "True" : "False", DataType = eAttributeDataType.NonString });
166            m.Attributes._list.Add(new ExcelVbaModuleAttribute() { Name = "VB_TemplateDerived", Value = "False", DataType = eAttributeDataType.NonString });
167            m.Attributes._list.Add(new ExcelVbaModuleAttribute() { Name = "VB_Customizable", Value = "False", DataType = eAttributeDataType.NonString });
168
169            //m.Code = _project.GetBlankClassModule(Name, Exposed);
170            m.Private = !Exposed;
171            //m.ClassID=
172            _list.Add(m);
173            return m;
174        }
175    }
176    /// <summary>
177    /// A collection of the vba projects references
178    /// </summary>
179    public class ExcelVbaReferenceCollection : ExcelVBACollectionBase<ExcelVbaReference>
180    {       
181        internal ExcelVbaReferenceCollection()
182        {
183
184        }
185        /// <summary>
186        /// Adds a new reference
187        /// </summary>
188        /// <param name="Item">The reference object</param>
189        public void Add(ExcelVbaReference Item)
190        {
191            _list.Add(Item);
192        }
193    }
194    /// <summary>
195    /// A collection of the module level attributes
196    /// </summary>
197    public class ExcelVbaModuleAttributesCollection : ExcelVBACollectionBase<ExcelVbaModuleAttribute>
198    {
199        internal string GetAttributeText()
200        {
201            StringBuilder sb=new StringBuilder();
202
203            foreach (var attr in this)
204            {
205                sb.AppendFormat("Attribute {0} = {1}\r\n", attr.Name, attr.DataType==eAttributeDataType.String ? "\"" + attr.Value + "\"" : attr.Value);
206            }
207            return sb.ToString();
208        }
209    }
210}
Note: See TracBrowser for help on using the repository browser.