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 21-MAR-2011
|
---|
30 | * Jan Källman License changed GPL-->LGPL 2011-12-16
|
---|
31 | *******************************************************************************/
|
---|
32 | using System;
|
---|
33 | using System.Collections.Generic;
|
---|
34 | using System.Text;
|
---|
35 | using System.Xml;
|
---|
36 | using OfficeOpenXml.Style.XmlAccess;
|
---|
37 |
|
---|
38 | namespace OfficeOpenXml.Table.PivotTable
|
---|
39 | {
|
---|
40 | /// <summary>
|
---|
41 | /// A pivo table data field
|
---|
42 | /// </summary>
|
---|
43 | public class ExcelPivotTableDataField : XmlHelper
|
---|
44 | {
|
---|
45 | internal ExcelPivotTableDataField(XmlNamespaceManager ns, XmlNode topNode,ExcelPivotTableField field) :
|
---|
46 | base(ns, topNode)
|
---|
47 | {
|
---|
48 | if (topNode.Attributes.Count == 0)
|
---|
49 | {
|
---|
50 | Index = field.Index;
|
---|
51 | BaseField = 0;
|
---|
52 | BaseItem = 0;
|
---|
53 | }
|
---|
54 |
|
---|
55 | Field = field;
|
---|
56 | }
|
---|
57 | /// <summary>
|
---|
58 | /// The field
|
---|
59 | /// </summary>
|
---|
60 | public ExcelPivotTableField Field
|
---|
61 | {
|
---|
62 | get;
|
---|
63 | private set;
|
---|
64 | }
|
---|
65 | /// <summary>
|
---|
66 | /// The index of the datafield
|
---|
67 | /// </summary>
|
---|
68 | public int Index
|
---|
69 | {
|
---|
70 | get
|
---|
71 | {
|
---|
72 | return GetXmlNodeInt("@fld");
|
---|
73 | }
|
---|
74 | internal set
|
---|
75 | {
|
---|
76 | SetXmlNodeString("@fld",value.ToString());
|
---|
77 | }
|
---|
78 | }
|
---|
79 | /// <summary>
|
---|
80 | /// The name of the datafield
|
---|
81 | /// </summary>
|
---|
82 | public string Name
|
---|
83 | {
|
---|
84 | get
|
---|
85 | {
|
---|
86 | return GetXmlNodeString("@name");
|
---|
87 | }
|
---|
88 | set
|
---|
89 | {
|
---|
90 | if (Field._table.DataFields.ExistsDfName(value, this))
|
---|
91 | {
|
---|
92 | throw (new InvalidOperationException("Duplicate datafield name"));
|
---|
93 | }
|
---|
94 | SetXmlNodeString("@name", value);
|
---|
95 | }
|
---|
96 | }
|
---|
97 | /// <summary>
|
---|
98 | /// Field index. Reference to the field collection
|
---|
99 | /// </summary>
|
---|
100 | public int BaseField
|
---|
101 | {
|
---|
102 | get
|
---|
103 | {
|
---|
104 | return GetXmlNodeInt("@baseField");
|
---|
105 | }
|
---|
106 | set
|
---|
107 | {
|
---|
108 | SetXmlNodeString("@baseField", value.ToString());
|
---|
109 | }
|
---|
110 | }
|
---|
111 | /// <summary>
|
---|
112 | /// Specifies the index to the base item when the ShowDataAs calculation is in use
|
---|
113 | /// </summary>
|
---|
114 | public int BaseItem
|
---|
115 | {
|
---|
116 | get
|
---|
117 | {
|
---|
118 | return GetXmlNodeInt("@baseItem");
|
---|
119 | }
|
---|
120 | set
|
---|
121 | {
|
---|
122 | SetXmlNodeString("@baseItem", value.ToString());
|
---|
123 | }
|
---|
124 | }
|
---|
125 | /// <summary>
|
---|
126 | /// Number format id.
|
---|
127 | /// </summary>
|
---|
128 | internal int NumFmtId
|
---|
129 | {
|
---|
130 | get
|
---|
131 | {
|
---|
132 | return GetXmlNodeInt("@numFmtId");
|
---|
133 | }
|
---|
134 | set
|
---|
135 | {
|
---|
136 | SetXmlNodeString("@numFmtId", value.ToString());
|
---|
137 | }
|
---|
138 | }
|
---|
139 | /// <summary>
|
---|
140 | /// Number format for the data column
|
---|
141 | /// </summary>
|
---|
142 | public string Format
|
---|
143 | {
|
---|
144 | get
|
---|
145 | {
|
---|
146 | foreach (var nf in Field._table.WorkSheet.Workbook.Styles.NumberFormats)
|
---|
147 | {
|
---|
148 | if (nf.NumFmtId == NumFmtId)
|
---|
149 | {
|
---|
150 | return nf.Format;
|
---|
151 | }
|
---|
152 | }
|
---|
153 | return Field._table.WorkSheet.Workbook.Styles.NumberFormats[0].Format;
|
---|
154 | }
|
---|
155 | set
|
---|
156 | {
|
---|
157 | var styles = Field._table.WorkSheet.Workbook.Styles;
|
---|
158 |
|
---|
159 | ExcelNumberFormatXml nf = null;
|
---|
160 | if (!styles.NumberFormats.FindByID(value, ref nf))
|
---|
161 | {
|
---|
162 | nf = new ExcelNumberFormatXml(NameSpaceManager) { Format = value, NumFmtId = styles.NumberFormats.NextId++ };
|
---|
163 | styles.NumberFormats.Add(value, nf);
|
---|
164 | }
|
---|
165 | NumFmtId = nf.NumFmtId;
|
---|
166 | }
|
---|
167 | }
|
---|
168 | /// <summary>
|
---|
169 | /// Type of aggregate function
|
---|
170 | /// </summary>
|
---|
171 | public DataFieldFunctions Function
|
---|
172 | {
|
---|
173 | get
|
---|
174 | {
|
---|
175 | string s=GetXmlNodeString("@subtotal");
|
---|
176 | if(s=="")
|
---|
177 | {
|
---|
178 | return DataFieldFunctions.None;
|
---|
179 | }
|
---|
180 | else
|
---|
181 | {
|
---|
182 | return (DataFieldFunctions)Enum.Parse(typeof(DataFieldFunctions), s, true);
|
---|
183 | }
|
---|
184 | }
|
---|
185 | set
|
---|
186 | {
|
---|
187 | string v;
|
---|
188 | switch(value)
|
---|
189 | {
|
---|
190 | case DataFieldFunctions.None:
|
---|
191 | DeleteNode("@subtotal");
|
---|
192 | return;
|
---|
193 | case DataFieldFunctions.CountNums:
|
---|
194 | v="CountNums";
|
---|
195 | break;
|
---|
196 | case DataFieldFunctions.StdDev:
|
---|
197 | v="stdDev";
|
---|
198 | break;
|
---|
199 | case DataFieldFunctions.StdDevP:
|
---|
200 | v="stdDevP";
|
---|
201 | break;
|
---|
202 | default:
|
---|
203 | v=value.ToString().ToLower();
|
---|
204 | break;
|
---|
205 | }
|
---|
206 | SetXmlNodeString("@subtotal", v);
|
---|
207 | }
|
---|
208 | }
|
---|
209 | ///Since we have no items, Excel will crash when we use showDataAs options that require baseItem's
|
---|
210 | //public eShowDataAs ShowDataAs
|
---|
211 | //{
|
---|
212 | // get
|
---|
213 | // {
|
---|
214 | // string s = GetXmlNodeString("@showDataAs");
|
---|
215 | // if (s == "")
|
---|
216 | // {
|
---|
217 | // return eShowDataAs.Normal;
|
---|
218 | // }
|
---|
219 | // else
|
---|
220 | // {
|
---|
221 | // return (eShowDataAs)Enum.Parse(typeof(eShowDataAs), s, true);
|
---|
222 | // }
|
---|
223 | // }
|
---|
224 | // set
|
---|
225 | // {
|
---|
226 | // string v = value.ToString();
|
---|
227 | // v = v.Substring(0, 1).ToLower() + v.Substring(1);
|
---|
228 | // SetXmlNodeString("@showDataAs", v);
|
---|
229 | // }
|
---|
230 | //}
|
---|
231 | }
|
---|
232 | }
|
---|