|
1 | | -using DocumentFormat.OpenXml; |
| 1 | +using System; |
| 2 | +using DocumentFormat.OpenXml; |
| 3 | +using System.Globalization; |
| 4 | +using DocumentFormat.OpenXml.Spreadsheet; |
2 | 5 |
|
3 | 6 | namespace FileFormat.Cells |
4 | 7 | { |
5 | | - /// <summary> |
6 | | - /// Represents a cell in a row. |
7 | | - /// </summary> |
8 | | - public class Cell |
| 8 | + public sealed class Cell |
9 | 9 | { |
10 | | - /// <value> |
11 | | - /// An object of the Parent Cell class. |
12 | | - /// </value> |
13 | | - protected internal DocumentFormat.OpenXml.Spreadsheet.Cell cell; |
| 10 | + private readonly DocumentFormat.OpenXml.Spreadsheet.Cell _cell; |
| 11 | + private readonly SheetData _sheetData; |
14 | 12 |
|
15 | 13 | /// <summary> |
16 | | - /// Instantiate a new instance of the Cell class. |
| 14 | + /// Gets the cell reference in A1 notation. |
17 | 15 | /// </summary> |
18 | | - public Cell() |
| 16 | + public string CellReference => _cell.CellReference; |
| 17 | + |
| 18 | + /// <summary> |
| 19 | + /// Initializes a new instance of the <see cref="Cell"/> class. |
| 20 | + /// </summary> |
| 21 | + /// <param name="cell">The underlying OpenXML cell object.</param> |
| 22 | + /// <param name="sheetData">The sheet data containing the cell.</param> |
| 23 | + /// <exception cref="ArgumentNullException"> |
| 24 | + /// Thrown when <paramref name="cell"/> or <paramref name="sheetData"/> is null. |
| 25 | + /// </exception> |
| 26 | + public Cell(DocumentFormat.OpenXml.Spreadsheet.Cell cell, SheetData sheetData) |
19 | 27 | { |
20 | | - this.cell = new DocumentFormat.OpenXml.Spreadsheet.Cell(); |
21 | | - //this.styles = new Styles(); |
| 28 | + _cell = cell ?? throw new ArgumentNullException(nameof(cell)); |
| 29 | + _sheetData = sheetData ?? throw new ArgumentNullException(nameof(sheetData)); |
22 | 30 | } |
23 | 31 |
|
24 | 32 | /// <summary> |
25 | | - /// This method is used to set the Cell Reference in a worksheet. |
| 33 | + /// Sets the value of the cell as a string. |
26 | 34 | /// </summary> |
27 | | - /// <param name="value">A string value.</param> |
28 | | - public void setCellReference(string value) |
| 35 | + /// <param name="value">The value to set.</param> |
| 36 | + public void PutValue(string value) |
29 | 37 | { |
30 | | - this.cell.CellReference = value; |
| 38 | + PutValue(value, CellValues.String); |
31 | 39 | } |
32 | 40 |
|
33 | 41 | /// <summary> |
34 | | - /// This method is used to set the Cell data type to String. |
| 42 | + /// Sets the value of the cell as a number. |
35 | 43 | /// </summary> |
36 | | - public void setStringDataType() |
| 44 | + /// <param name="value">The numeric value to set.</param> |
| 45 | + public void PutValue(double value) |
37 | 46 | { |
38 | | - this.cell.DataType = DocumentFormat.OpenXml.Spreadsheet.CellValues.String; |
| 47 | + PutValue(value.ToString(CultureInfo.InvariantCulture), CellValues.Number); |
39 | 48 | } |
| 49 | + |
40 | 50 | /// <summary> |
41 | | - /// This method is used to set the Cell data type to Number. |
| 51 | + /// Sets the value of the cell as a date. |
42 | 52 | /// </summary> |
43 | | - public void setNumberDataType() |
| 53 | + /// <param name="value">The date value to set.</param> |
| 54 | + public void PutValue(DateTime value) |
44 | 55 | { |
45 | | - this.cell.DataType = DocumentFormat.OpenXml.Spreadsheet.CellValues.Number; |
| 56 | + PutValue(value.ToOADate().ToString(CultureInfo.InvariantCulture), CellValues.Date); |
46 | 57 | } |
47 | 58 |
|
48 | 59 | /// <summary> |
49 | | - /// This method is used to set the value of a Cell. |
| 60 | + /// Sets the cell's value with a specific data type. |
50 | 61 | /// </summary> |
51 | | - /// <param name="value">A dynamic value.</param> |
52 | | - public void CellValue(dynamic value) |
| 62 | + /// <param name="value">The value to set.</param> |
| 63 | + /// <param name="dataType">The data type of the value.</param> |
| 64 | + private void PutValue(string value, CellValues dataType) |
53 | 65 | { |
54 | | - this.cell.CellValue = new DocumentFormat.OpenXml.Spreadsheet.CellValue(value); |
| 66 | + _cell.DataType = new EnumValue<CellValues>(dataType); |
| 67 | + _cell.CellValue = new CellValue(value); |
| 68 | + |
55 | 69 | } |
56 | 70 |
|
57 | 71 | /// <summary> |
58 | | - /// Sets the style index of the cell to 1. |
| 72 | + /// Sets a formula for the cell. |
59 | 73 | /// </summary> |
60 | | - public void CellIndex() |
| 74 | + /// <param name="formula">The formula to set.</param> |
| 75 | + public void PutFormula(string formula) |
61 | 76 | { |
62 | | - this.cell.StyleIndex = 1; |
| 77 | + _cell.CellFormula = new CellFormula(formula); |
| 78 | + _cell.CellValue = new CellValue(); // You might want to set some default value or calculated value here |
63 | 79 | } |
64 | 80 |
|
65 | 81 | /// <summary> |
66 | | - /// Sets the style index of the cell to the specified value. |
| 82 | + /// Gets the value of the cell. |
67 | 83 | /// </summary> |
68 | | - /// <param name="num">The style index is to be set for the cell.</param> |
| 84 | + /// <returns>The cell value as a string.</returns> |
| 85 | + public string GetValue() |
| 86 | + { |
| 87 | + return _cell.CellValue?.Text; |
| 88 | + } |
69 | 89 |
|
70 | | - public void CellIndex(UInt32Value num) |
| 90 | + /// <summary> |
| 91 | + /// Gets the data type of the cell's value. |
| 92 | + /// </summary> |
| 93 | + /// <returns>The cell's value data type, or null if not set.</returns> |
| 94 | + public CellValues? GetDataType() |
71 | 95 | { |
72 | | - this.cell.StyleIndex = num; |
| 96 | + return _cell.DataType?.Value; |
73 | 97 | } |
74 | 98 |
|
75 | 99 |
|
76 | | - // Other properties and methods... |
| 100 | + /// <summary> |
| 101 | + /// Gets the formula set for the cell. |
| 102 | + /// </summary> |
| 103 | + /// <returns>The cell's formula as a string, or null if not set.</returns> |
| 104 | + public string GetFormula() |
| 105 | + { |
| 106 | + return _cell.CellFormula?.Text; |
| 107 | + } |
| 108 | + |
| 109 | + /// <summary> |
| 110 | + /// Applies a style to the cell. |
| 111 | + /// </summary> |
| 112 | + /// <param name="styleIndex">The index of the style to apply.</param> |
| 113 | + public void ApplyStyle(uint styleIndex) |
| 114 | + { |
| 115 | + _cell.StyleIndex = styleIndex; |
| 116 | + } |
77 | 117 | } |
78 | 118 |
|
79 | 119 | } |
|
0 commit comments