NanoXLSX.Core 3.0.0-rc.3
Loading...
Searching...
No Matches
NumberFormat.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 © 2025
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.Collections.Generic;
9using System.Text;
11
12namespace NanoXLSX.Styles
13{
18 {
19 #region constants
23 public static readonly int CustomFormatStartNumber = 164;
27 public static readonly FormatNumber DefaultNumber = FormatNumber.None;
28
29 #endregion
30 private int customFormatID;
31
32 #region enums
38 public enum FormatNumber
39 {
41 None = 0,
105 Custom = 164,
106 }
107
130 #endregion
131
132 #region privateFields
133 private string customFormatCode;
134
135 #endregion
136
137 #region properties
144 [Append]
145 public string CustomFormatCode
146 {
147 get => customFormatCode;
148 set
149 {
150 if (string.IsNullOrEmpty(value))
151 {
152 throw new FormatException("A custom format code cannot be null or empty");
153 }
154 customFormatCode = value;
155 }
156 }
157
161 [Append]
162 public int CustomFormatID
163 {
164 get { return customFormatID; }
165 set
166 {
167 if (value < CustomFormatStartNumber && !StyleRepository.Instance.ImportInProgress)
168 {
169 throw new StyleException("The number '" + value + "' is not a valid custom format ID. Must be at least " + CustomFormatStartNumber);
170 }
171 customFormatID = value;
172 }
173 }
174
177 [Append(Ignore = true)]
178 public bool IsCustomFormat
179 {
180 get
181 {
182 if (Number == FormatNumber.Custom)
183 { return true; }
184 else { return false; }
185 }
186 }
187
190 [Append]
191 public FormatNumber Number { get; set; }
192 #endregion
193
194 #region constructors
199 {
201 customFormatCode = string.Empty;
203 }
204 #endregion
205
206 #region methods
207
212 public override string ToString()
213 {
214 StringBuilder sb = new StringBuilder();
215 sb.Append("\"NumberFormat\": {\n");
216 AddPropertyAsJson(sb, "CustomFormatCode", CustomFormatCode);
217 AddPropertyAsJson(sb, "CustomFormatID", CustomFormatID);
218 AddPropertyAsJson(sb, "Number", Number);
219 AddPropertyAsJson(sb, "HashCode", this.GetHashCode(), true);
220 sb.Append("\n}");
221 return sb.ToString();
222 }
223
228 public override AbstractStyle Copy()
229 {
230 NumberFormat copy = new NumberFormat
231 {
232 customFormatCode = customFormatCode,
234 Number = Number
235 };
236 return copy;
237 }
238
244 {
245 return (NumberFormat)Copy();
246 }
247
254 public override int GetHashCode()
255 {
256 unchecked
257 {
258 int hashCode = 495605284;
259 hashCode = hashCode * -1521134295 + EqualityComparer<string>.Default.GetHashCode(CustomFormatCode);
260 hashCode = hashCode * -1521134295 + CustomFormatID.GetHashCode();
261 hashCode = hashCode * -1521134295 + Number.GetHashCode();
262 return hashCode;
263 }
264 }
265
271 public override bool Equals(object obj)
272 {
273 return obj is NumberFormat format &&
274 CustomFormatCode == format.CustomFormatCode &&
275 CustomFormatID == format.CustomFormatID &&
276 Number == format.Number;
277 }
278
279 #endregion
280
281 #region staticMethods
282
289 public static bool IsDateFormat(FormatNumber number)
290 {
291 switch (number)
292 {
293 case FormatNumber.Format14:
294 case FormatNumber.Format15:
295 case FormatNumber.Format16:
296 case FormatNumber.Format17:
297 case FormatNumber.Format22:
298 return true;
299 default:
300 return false;
301 }
302 }
303
310 public static bool IsTimeFormat(FormatNumber number)
311 {
312 switch (number)
313 {
314 case FormatNumber.Format18:
315 case FormatNumber.Format19:
316 case FormatNumber.Format20:
317 case FormatNumber.Format21:
318 case FormatNumber.Format45:
319 case FormatNumber.Format46:
320 case FormatNumber.Format47:
321 return true;
322 default:
323 return false;
324 }
325 }
326
333 public static FormatRange TryParseFormatNumber(int number, out FormatNumber formatNumber)
334 {
335
336 bool isDefined = System.Enum.IsDefined(typeof(FormatNumber), number);
337 if (isDefined)
338 {
339 formatNumber = (FormatNumber)number;
340 return FormatRange.DefinedFormat;
341 }
342 if (number < 0)
343 {
344 formatNumber = FormatNumber.None;
345 return FormatRange.Invalid;
346 }
347 else if (number > 0 && number < CustomFormatStartNumber)
348 {
349 formatNumber = FormatNumber.None;
350 return FormatRange.Undefined;
351 }
352 else
353 {
354 formatNumber = FormatNumber.Custom;
355 return FormatRange.CustomFormat;
356 }
357 }
358
359 #endregion
360
361 }
362}
Class for exceptions regarding format error incidents.
Class for exceptions regarding Style incidents.
Class represents an abstract style component.
static readonly FormatNumber DefaultNumber
Default format number as constant.
string CustomFormatCode
Gets or sets the raw custom format code in the notation of Excel. The code is not escaped or unescape...
override string ToString()
Override toString method.
FormatNumber
Enum for predefined number formats, used by implementations of the NumberFormat class.
@ Custom
Custom Format (ID 164 and higher).
@ Format39
Format: #,##0.00_);(#,##0.00).
@ Format7
Format: $#,##0.00_);($#,##0.00).
@ Format5
Format: $#,##0_);($#,##0).
static bool IsDateFormat(FormatNumber number)
Determines whether a defined style format number represents a date (or date and time).
NumberFormat CopyNumberFormat()
Method to copy the current object to a new one with casting.
bool IsCustomFormat
Gets whether the number format is a custom format (higher or equals 164). If true,...
FormatNumber Number
Gets or sets the format number. Set this to custom (164) in case of custom number formats.
override bool Equals(object obj)
Returns whether two instances are the same.
override AbstractStyle Copy()
Method to copy the current object to a new one without casting.
NumberFormat()
Default constructor.
FormatRange
Range or validity of the format number, used by the NumberFormat class.
@ Invalid
Probably invalid format numbers (e.g. negative value).
@ CustomFormat
Custom defined formats from 165 and higher. Although 164 is already custom, it is still defined as en...
@ DefinedFormat
Format from 0 to 164 (with gaps).
@ Undefined
Values between 0 and 164 that are not defined as enum value. This may be caused by changes of the OOX...
static FormatRange TryParseFormatNumber(int number, out FormatNumber formatNumber)
Tries to parse registered format numbers. If the parsing fails, it is assumed that the number is a cu...
static bool IsTimeFormat(FormatNumber number)
Determines whether a defined style format number represents a time).
int CustomFormatID
Gets or sets the format number of the custom format. Must be higher or equal then predefined custom n...
override int GetHashCode()
Returns a hash code for this instance.
static readonly int CustomFormatStartNumber
Start ID for custom number formats as constant (value 164).
Class to manage all styles at runtime, before writing XLSX files. The main purpose is deduplication a...
static StyleRepository Instance
Gets the singleton instance of the repository.