|
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; |
14 |
| - |
15 |
| - /// <summary> |
16 |
| - /// Instantiate a new instance of the Cell class. |
17 |
| - /// </summary> |
18 |
| - public Cell() |
| 10 | + private readonly DocumentFormat.OpenXml.Spreadsheet.Cell _cell; |
| 11 | + private readonly SheetData _sheetData; |
| 12 | + |
| 13 | + public string CellReference => _cell.CellReference; |
| 14 | + |
| 15 | + public Cell(DocumentFormat.OpenXml.Spreadsheet.Cell cell, SheetData sheetData) |
19 | 16 | {
|
20 |
| - this.cell = new DocumentFormat.OpenXml.Spreadsheet.Cell(); |
21 |
| - //this.styles = new Styles(); |
| 17 | + _cell = cell ?? throw new ArgumentNullException(nameof(cell)); |
| 18 | + _sheetData = sheetData ?? throw new ArgumentNullException(nameof(sheetData)); |
22 | 19 | }
|
23 | 20 |
|
24 |
| - /// <summary> |
25 |
| - /// This method is used to set the Cell Reference in a worksheet. |
26 |
| - /// </summary> |
27 |
| - /// <param name="value">A string value.</param> |
28 |
| - public void setCellReference(string value) |
| 21 | + public void PutValue(string value) |
29 | 22 | {
|
30 |
| - this.cell.CellReference = value; |
| 23 | + PutValue(value, CellValues.String); |
31 | 24 | }
|
32 | 25 |
|
33 |
| - /// <summary> |
34 |
| - /// This method is used to set the Cell data type to String. |
35 |
| - /// </summary> |
36 |
| - public void setStringDataType() |
| 26 | + public void PutValue(double value) |
37 | 27 | {
|
38 |
| - this.cell.DataType = DocumentFormat.OpenXml.Spreadsheet.CellValues.String; |
| 28 | + PutValue(value.ToString(CultureInfo.InvariantCulture), CellValues.Number); |
39 | 29 | }
|
40 |
| - /// <summary> |
41 |
| - /// This method is used to set the Cell data type to Number. |
42 |
| - /// </summary> |
43 |
| - public void setNumberDataType() |
| 30 | + |
| 31 | + public void PutValue(DateTime value) |
44 | 32 | {
|
45 |
| - this.cell.DataType = DocumentFormat.OpenXml.Spreadsheet.CellValues.Number; |
| 33 | + PutValue(value.ToOADate().ToString(CultureInfo.InvariantCulture), CellValues.Date); |
46 | 34 | }
|
47 | 35 |
|
48 |
| - /// <summary> |
49 |
| - /// This method is used to set the value of a Cell. |
50 |
| - /// </summary> |
51 |
| - /// <param name="value">A dynamic value.</param> |
52 |
| - public void CellValue(dynamic value) |
| 36 | + private void PutValue(string value, CellValues dataType) |
53 | 37 | {
|
54 |
| - this.cell.CellValue = new DocumentFormat.OpenXml.Spreadsheet.CellValue(value); |
| 38 | + _cell.DataType = new EnumValue<CellValues>(dataType); |
| 39 | + _cell.CellValue = new CellValue(value); |
| 40 | + |
55 | 41 | }
|
56 | 42 |
|
57 |
| - /// <summary> |
58 |
| - /// Sets the style index of the cell to 1. |
59 |
| - /// </summary> |
60 |
| - public void CellIndex() |
| 43 | + public void PutFormula(string formula) |
61 | 44 | {
|
62 |
| - this.cell.StyleIndex = 1; |
| 45 | + _cell.CellFormula = new CellFormula(formula); |
| 46 | + _cell.CellValue = new CellValue(); // You might want to set some default value or calculated value here |
63 | 47 | }
|
64 | 48 |
|
65 |
| - /// <summary> |
66 |
| - /// Sets the style index of the cell to the specified value. |
67 |
| - /// </summary> |
68 |
| - /// <param name="num">The style index is to be set for the cell.</param> |
| 49 | + public string GetValue() |
| 50 | + { |
| 51 | + return _cell.CellValue?.Text; |
| 52 | + } |
69 | 53 |
|
70 |
| - public void CellIndex(UInt32Value num) |
| 54 | + public CellValues? GetDataType() |
71 | 55 | {
|
72 |
| - this.cell.StyleIndex = num; |
| 56 | + return _cell.DataType?.Value; |
73 | 57 | }
|
74 | 58 |
|
| 59 | + |
75 | 60 |
|
76 |
| - // Other properties and methods... |
| 61 | + public string GetFormula() |
| 62 | + { |
| 63 | + return _cell.CellFormula?.Text; |
| 64 | + } |
| 65 | + |
| 66 | + public void ApplyStyle(uint styleIndex) |
| 67 | + { |
| 68 | + _cell.StyleIndex = styleIndex; |
| 69 | + } |
77 | 70 | }
|
78 | 71 |
|
79 | 72 | }
|
|
0 commit comments