NanoXLSX.Core 3.0.0-rc.3
Loading...
Searching...
No Matches
CellXf.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.Text;
10using NanoXLSX.Utils;
12
13namespace NanoXLSX.Styles
14{
18 public class CellXf : AbstractStyle
19 {
20 #region constants
28 public static readonly TextBreakValue DefaultAlignment = TextBreakValue.None;
32 public static readonly TextDirectionValue DefaultTextDirection = TextDirectionValue.Horizontal;
37 #endregion
38
39 #region privateFields
40 private int textRotation;
41 private TextDirectionValue textDirection;
42 private int indent;
43 #endregion
44
45 #region enums
70
83
94
113 #endregion
114
115 #region properties
119 [Append]
120 public bool ForceApplyAlignment { get; set; }
124 [Append]
125 public bool Hidden { get; set; }
129 [Append]
134 [Append]
135 public bool Locked { get; set; }
139 [Append]
140 public TextBreakValue Alignment { get; set; }
144 [Append]
146 {
147 get { return textDirection; }
148 set
149 {
150 textDirection = value;
151 CalculateInternalRotation();
152 }
153 }
154
157 [Append]
158 public int TextRotation
159 {
160 get { return textRotation; }
161 set
162 {
163 textRotation = value;
165 CalculateInternalRotation();
166 }
167 }
168
171 [Append]
173
177 [Append]
178 public int Indent
179 {
180 get => indent;
181 set
182 {
183 if (value >= 0)
184 {
185 indent = value;
186 }
187 else
188 {
189 throw new StyleException("The indent value '" + value + "' is not valid. It must be >= 0");
190 }
191 }
192 }
193
194 #endregion
195
196 #region constructors
200 public CellXf()
201 {
204 textDirection = DefaultTextDirection;
206 Locked = true; // Default in Excel
207 textRotation = 0;
208 Indent = 0;
209 }
210 #endregion
211
212 #region methods
218 internal int CalculateInternalRotation()
219 {
220 if (textRotation < -90 || textRotation > 90)
221 {
222 throw new FormatException("The rotation value (" + ParserUtils.ToString(textRotation) + "°) is out of range. Range is form -90° to +90°");
223 }
224 if (textDirection == TextDirectionValue.Vertical)
225 {
226 textRotation = 255;
227 return textRotation;
228 }
229 else
230 {
231 if (textRotation >= 0)
232 {
233 return textRotation;
234 }
235 else
236 {
237 return (90 - textRotation);
238 }
239 }
240 }
241
246 public override string ToString()
247 {
248 StringBuilder sb = new StringBuilder();
249 sb.Append("\"StyleXF\": {\n");
250 AddPropertyAsJson(sb, "HorizontalAlign", HorizontalAlign);
251 AddPropertyAsJson(sb, "Alignment", Alignment);
252 AddPropertyAsJson(sb, "TextDirection", TextDirection);
253 AddPropertyAsJson(sb, "TextRotation", TextRotation);
254 AddPropertyAsJson(sb, "VerticalAlign", VerticalAlign);
255 AddPropertyAsJson(sb, "ForceApplyAlignment", ForceApplyAlignment);
256 AddPropertyAsJson(sb, "Locked", Locked);
257 AddPropertyAsJson(sb, "Hidden", Hidden);
258 AddPropertyAsJson(sb, "Indent", Indent);
259 AddPropertyAsJson(sb, "HashCode", this.GetHashCode(), true);
260 sb.Append("\n}");
261 return sb.ToString();
262 }
263
270 public override int GetHashCode()
271 {
272 unchecked
273 {
274 int hashCode = 626307906;
275 hashCode = hashCode * -1521134295 + ForceApplyAlignment.GetHashCode();
276 hashCode = hashCode * -1521134295 + Hidden.GetHashCode();
277 hashCode = hashCode * -1521134295 + HorizontalAlign.GetHashCode();
278 hashCode = hashCode * -1521134295 + Locked.GetHashCode();
279 hashCode = hashCode * -1521134295 + Alignment.GetHashCode();
280 hashCode = hashCode * -1521134295 + TextDirection.GetHashCode();
281 hashCode = hashCode * -1521134295 + TextRotation.GetHashCode();
282 hashCode = hashCode * -1521134295 + VerticalAlign.GetHashCode();
283 hashCode = hashCode * -1521134295 + Indent.GetHashCode();
284 return hashCode;
285 }
286 }
287
293 public override bool Equals(object obj)
294 {
295 return obj is CellXf xf &&
296 ForceApplyAlignment == xf.ForceApplyAlignment &&
297 Hidden == xf.Hidden &&
298 HorizontalAlign == xf.HorizontalAlign &&
299 Locked == xf.Locked &&
300 Alignment == xf.Alignment &&
301 TextDirection == xf.TextDirection &&
302 TextRotation == xf.TextRotation &&
303 VerticalAlign == xf.VerticalAlign &&
304 Indent == xf.Indent;
305 }
306
307
308
313 public override AbstractStyle Copy()
314 {
315 CellXf copy = new CellXf
316 {
323 Locked = Locked,
324 Hidden = Hidden,
325 Indent = Indent
326 };
327 return copy;
328 }
329
335 {
336 return (CellXf)Copy();
337 }
338 #endregion
339
340 #region staticMethods
341
345 internal static string GetHorizontalAlignName(HorizontalAlignValue align)
346 {
347 string output = "";
348 switch (align)
349 {
350 case HorizontalAlignValue.Left: output = "left"; break;
351 case HorizontalAlignValue.Center: output = "center"; break;
352 case HorizontalAlignValue.Right: output = "right"; break;
353 case HorizontalAlignValue.Fill: output = "fill"; break;
354 case HorizontalAlignValue.Justify: output = "justify"; break;
355 case HorizontalAlignValue.General: output = "general"; break;
356 case HorizontalAlignValue.CenterContinuous: output = "centerContinuous"; break;
357 case HorizontalAlignValue.Distributed: output = "distributed"; break;
358 }
359 return output;
360 }
361
365 internal static HorizontalAlignValue GetHorizontalAlignEnum(string name)
366 {
367 switch (name)
368 {
369 case "left": return HorizontalAlignValue.Left;
370 case "center": return HorizontalAlignValue.Center;
371 case "right": return HorizontalAlignValue.Right;
372 case "fill": return HorizontalAlignValue.Fill;
373 case "justify": return HorizontalAlignValue.Justify;
374 case "general": return HorizontalAlignValue.General;
375 case "centerContinuous": return HorizontalAlignValue.CenterContinuous;
376 case "distributed": return HorizontalAlignValue.Distributed;
377 default:
378 return HorizontalAlignValue.None;
379 }
380 }
381
385 internal static string GetVerticalAlignName(VerticalAlignValue align)
386 {
387 string output = "";
388 switch (align)
389 {
390 case VerticalAlignValue.Bottom: output = "bottom"; break;
391 case VerticalAlignValue.Top: output = "top"; break;
392 case VerticalAlignValue.Center: output = "center"; break;
393 case VerticalAlignValue.Justify: output = "justify"; break;
394 case VerticalAlignValue.Distributed: output = "distributed"; break;
395 }
396 return output;
397 }
398
402 internal static VerticalAlignValue GetVerticalAlignEnum(string name)
403 {
404 switch (name)
405 {
406 case "bottom": return VerticalAlignValue.Bottom;
407 case "top": return VerticalAlignValue.Top;
408 case "center": return VerticalAlignValue.Center;
409 case "justify": return VerticalAlignValue.Justify;
410 case "distributed": return VerticalAlignValue.Distributed;
411 default:
412 return VerticalAlignValue.None;
413 }
414 }
415
416 #endregion
417
418 }
419
420}
Class for exceptions regarding format error incidents.
Class for exceptions regarding Style incidents.
Class represents an abstract style component.
static readonly TextDirectionValue DefaultTextDirection
Default text direction value as constant.
Definition CellXf.cs:32
override int GetHashCode()
Returns a hash code for this instance.
Definition CellXf.cs:270
static readonly HorizontalAlignValue DefaultHorizontalAlignment
Default horizontal align value as constant.
Definition CellXf.cs:24
HorizontalAlignValue
Enum for the horizontal alignment of a cell, used by the CellXf class.
Definition CellXf.cs:50
@ Center
Content will be aligned in the center.
Definition CellXf.cs:54
@ None
No alignment. The alignment will not be used in a style.
Definition CellXf.cs:68
@ Distributed
Distributed alignment.
Definition CellXf.cs:66
@ Right
Content will be aligned right.
Definition CellXf.cs:56
@ Left
Content will be aligned left.
Definition CellXf.cs:52
@ CenterContinuous
Center continuous alignment.
Definition CellXf.cs:64
TextDirectionValue
Enum for the general text alignment direction, used by the CellXf class.
Definition CellXf.cs:88
@ Vertical
Text direction is vertical.
Definition CellXf.cs:92
@ Horizontal
Text direction is horizontal (default).
Definition CellXf.cs:90
static readonly VerticalAlignValue DefaultVerticalAlignment
Default vertical align value as constant.
Definition CellXf.cs:36
bool Hidden
Gets or sets whether the hidden property (used for protection or hiding of cells) will be defined in ...
Definition CellXf.cs:125
TextBreakValue Alignment
Gets or sets the text break options of the style.
Definition CellXf.cs:140
int TextRotation
Gets or sets the text rotation in degrees (from +90 to -90).
Definition CellXf.cs:159
bool ForceApplyAlignment
Gets or sets whether the applyAlignment property (used to merge cells) will be defined in the XF entr...
Definition CellXf.cs:120
bool Locked
Gets or sets whether the locked property (used for locking / protection of cells or worksheets) will ...
Definition CellXf.cs:135
static readonly TextBreakValue DefaultAlignment
Default text break value as constant.
Definition CellXf.cs:28
VerticalAlignValue
Enum for the vertical alignment of a cell, used by the CellXf class.
Definition CellXf.cs:99
@ Bottom
Content will be aligned on the bottom (default).
Definition CellXf.cs:101
@ Top
Content will be aligned on the top.
Definition CellXf.cs:103
CellXf()
Default constructor.
Definition CellXf.cs:200
int Indent
Gets or sets the indentation in case of left, right or distributed alignment. If 0,...
Definition CellXf.cs:179
TextBreakValue
Enum for text break options, used by the CellXf class.
Definition CellXf.cs:75
@ WrapText
Word wrap is active.
Definition CellXf.cs:77
@ ShrinkToFit
Text will be resized to fit the cell.
Definition CellXf.cs:79
VerticalAlignValue VerticalAlign
Gets or sets the vertical alignment of the style.
Definition CellXf.cs:172
TextDirectionValue TextDirection
Gets or sets the direction of the text within the cell.
Definition CellXf.cs:146
override string ToString()
Override toString method.
Definition CellXf.cs:246
override bool Equals(object obj)
Returns whether two instances are the same.
Definition CellXf.cs:293
CellXf CopyCellXf()
Method to copy the current object to a new one with casting.
Definition CellXf.cs:334
override AbstractStyle Copy()
Method to copy the current object to a new one without casting.
Definition CellXf.cs:313
HorizontalAlignValue HorizontalAlign
Gets or sets the horizontal alignment of the style.
Definition CellXf.cs:130
Class representing a Fill (background) entry. The Fill entry is used to define background colors and ...
Definition Fill.cs:18
Class providing static methods to parse string values to specific types or to print object as languag...
static string ToString(int input)
Transforms an integer to an invariant sting.