NanoXLSX.Core 3.2.1
Loading...
Searching...
No Matches
FormulaData.cs
1/*
2 * NanoXLSX is a small .NET library to generate and read XLSX (Microsoft Excel 2007 or newer) files in an easy and native way
3 * Copyright Raphael Stoeckli © 2026
4 * This library is licensed under the MIT License.
5 * You find a copy of the license in project folder or on: http://opensource.org/licenses/MIT
6 */
7
8using System;
9using System.Collections.Generic;
10using NanoXLSX.Enums;
12using NanoXLSX.Utils;
13
14namespace NanoXLSX
15{
19 public class FormulaData : IEquatable<FormulaData>, IComparable<FormulaData>
20 {
21 private string expression;
22
23 #region enums
46 #endregion
47
48 private bool hasExternalReferences;
49 private DefinedName definedNameReference;
50
51 #region properties
52
56 public string Expression
57 {
58 get { return expression; }
59 internal set
60 {
61 expression = value;
62 HasExternalReferences = ParserUtils.ContainsExternalReference(value);
63 }
64 }
65
70 {
71 get { return hasExternalReferences; }
72 internal set
73 {
74 hasExternalReferences = value;
75 Features.SetFormulaFeatures(definedNameReference == null ? false : true, hasExternalReferences);
76 }
77 }
78
82 public FormulaType Type { get; internal set; }
83
87 public string FormulaRange { get; internal set; }
88
94 {
95 get { return definedNameReference; }
96 internal set
97 {
98 definedNameReference = value;
99 Features.SetFormulaFeatures(definedNameReference == null ? false : true, hasExternalReferences);
100 }
101 }
102
108 public object CachedValue { get; internal set; }
109
114 public Cell.CellType CachedValueType { get; internal set; }
115
119 public string MasterCellAddress { get; internal set; }
120
124 internal FeatureSet Features { get; private set; } = FeatureSet.CreateFormula();
125
126 #endregion
127
128 #region constructors
129
133 public FormulaData()
134 {
135 this.Type = FormulaType.Normal;
136 this.CachedValueType = Cell.CellType.Default;
137 }
138
145 public FormulaData(string expression, object cachedValue = null) : this()
146 {
147 Expression = expression;
148 CachedValue = cachedValue;
149 CachedValueType = ResolveCachedValueType(cachedValue);
150 }
151
152 #endregion
153 #region methods
154
160 internal static Cell.CellType ResolveCachedValueType(object cachedValue)
161 {
162 if (cachedValue == null)
163 {
164 return Cell.CellType.Default;
165 }
166 if (cachedValue is bool)
167 {
168 return Cell.CellType.Bool;
169 }
170 if (cachedValue is byte || cachedValue is sbyte || cachedValue is decimal || cachedValue is double
171 || cachedValue is float || cachedValue is int || cachedValue is uint || cachedValue is long
172 || cachedValue is ulong || cachedValue is short || cachedValue is ushort)
173 {
174 return Cell.CellType.Number;
175 }
176 if (cachedValue is DateTime)
177 {
178 return Cell.CellType.Date;
179 }
180 if (cachedValue is TimeSpan)
181 {
182 return Cell.CellType.Time;
183 }
184 if (cachedValue is Errors.FormulaError)
185 {
186 return Cell.CellType.Error;
187 }
188 return Cell.CellType.String;
189 }
190
196 internal FormulaData Copy()
197 {
198 FormulaData data = new FormulaData();
199 data.Expression = this.Expression;
200 data.Type = this.Type;
201 data.FormulaRange = this.FormulaRange;
202 data.CachedValue = this.CachedValue;
203 data.CachedValueType = this.CachedValueType;
204 data.MasterCellAddress = this.MasterCellAddress;
205 data.Features = this.Features.Copy(); // New feature set
206 data.DefinedNameReference = this.DefinedNameReference; // object reference
207 return data;
208 }
209
215 public int CompareTo(FormulaData other)
216 {
217 if (other is null)
218 {
219 return 1;
220 }
221 int cmp = string.CompareOrdinal(Expression, other.Expression);
222 if (cmp != 0)
223 {
224 return cmp;
225 }
226 cmp = Type.CompareTo(other.Type);
227 if (cmp != 0)
228 {
229 return cmp;
230 }
231 cmp = string.CompareOrdinal(FormulaRange, other.FormulaRange);
232 if (cmp != 0)
233 {
234 return cmp;
235 }
236 cmp = Comparer<DefinedName>.Default.Compare(DefinedNameReference, other.DefinedNameReference);
237 if (cmp != 0)
238 {
239 return cmp;
240 }
241 cmp = CachedValueType.CompareTo(other.CachedValueType);
242 if (cmp != 0)
243 {
244 return cmp;
245 }
246 cmp = Comparer<object>.Default.Compare(CachedValue, other.CachedValue);
247 if (cmp != 0)
248 {
249 return cmp;
250 }
251 return string.CompareOrdinal(MasterCellAddress, other.MasterCellAddress);
252 }
253
259 public bool Equals(FormulaData other)
260 {
261 if (other is null)
262 {
263 return false;
264 }
265 if (ReferenceEquals(this, other))
266 {
267 return true;
268 }
269 return string.Equals(Expression, other.Expression, StringComparison.Ordinal)
270 && Type == other.Type
271 && string.Equals(FormulaRange, other.FormulaRange, StringComparison.Ordinal)
272 && EqualityComparer<DefinedName>.Default.Equals(DefinedNameReference, other.DefinedNameReference)
273 && EqualityComparer<object>.Default.Equals(CachedValue, other.CachedValue)
274 && CachedValueType == other.CachedValueType
275 && string.Equals(MasterCellAddress, other.MasterCellAddress, StringComparison.Ordinal);
276 }
277
283 public override bool Equals(object obj)
284 {
285 return Equals(obj as FormulaData);
286 }
287
292 public override int GetHashCode()
293 {
294 unchecked
295 {
296 int hash = 17;
297 hash = (hash * 31) + (Expression != null ? Expression.GetHashCode() : 0);
298 hash = (hash * 31) + Type.GetHashCode();
299 hash = (hash * 31) + (FormulaRange != null ? FormulaRange.GetHashCode() : 0);
300 hash = (hash * 31) + (DefinedNameReference != null ? DefinedNameReference.GetHashCode() : 0);
301 hash = (hash * 31) + (CachedValue != null ? CachedValue.GetHashCode() : 0);
302 hash = (hash * 31) + CachedValueType.GetHashCode();
303 hash = (hash * 31) + (MasterCellAddress != null ? MasterCellAddress.GetHashCode() : 0);
304 return hash;
305 }
306 }
307 #endregion
308 }
309}
Class representing a cell of a worksheet.
Definition Cell.cs:25
CellType
Enum defines the basic data types of a cell.
Definition Cell.cs:35
Class representing a defined name within a workbook. A defined name is a descriptive text that repres...
override int GetHashCode()
Returns a hash code consistent with Equals(FormulaData).
string FormulaRange
Gets the range associated with an array, shared, or data-table formula. Can be a range or address (st...
FormulaData()
Default constructor.
bool Equals(FormulaData other)
Determines whether the specified FormulaData instance is equal to this instance.
bool HasExternalReferences
Gets whether the formula expression contains a reference to an external workbook.
DefinedName DefinedNameReference
Resolved defined name, if the complete formula expression is a direct reference to exactly one define...
override bool Equals(object obj)
Determines whether the specified object is equal to this instance.
string Expression
Gets the formula expression as string. This value is currently identical with Cell....
FormulaType
Enum to define the specific type of a formal if the Cell has the type Cell.CellType....
@ Array
Cell contains a formula that is part of an array.
@ Normal
Cell contains a regular formula (e.g "A1+A2").
@ Shared
Cell contains a shared formula, pointing to another formula that is identical.
@ DataTable
Cell contains a formula that is applied across a range of one or more cells.
string MasterCellAddress
Gets the address of the formula's master cell. This is mainly used in case of FormulaType....
int CompareTo(FormulaData other)
Compares this instance with another FormulaData instance.
object CachedValue
Gets the cached value of the formula.
FormulaData(string expression, object cachedValue=null)
Constructor with formula expression and optional cached value to create a formula of the common type ...
Cell.CellType CachedValueType
Gets the data type of CachedValue. The default is Cell.CellType.Default if no cached value or no supp...
FormulaType Type
Type of the formula. Default is FormulaType.Normal.
Class providing static methods to parse string values to specific types or to print object as languag...