Skip to content

Commit d29398b

Browse files
author
Fahad Adeel
committed
Revamped the Cell class in the FileFormat.Cells namespace:
- Removed the unnecessary `Row` & `Properties` class reference. - Sealed the `Cell` class to prevent further inheritance. - Enhanced the value assignment with type-specific `PutValue` methods for strings, numbers, and dates. - Added `PutFormula` method for formula assignment. - Enhanced cell value retrieval with `GetValue` and `GetFormula` methods. - Introduced `ApplyStyle` method for cell style application. - General code cleanup and refactoring for improved readability and maintainability.
1 parent 2580a96 commit d29398b

File tree

3 files changed

+43
-164
lines changed

3 files changed

+43
-164
lines changed

FileFormat.Cells/Cell.cs

Lines changed: 43 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,79 +1,72 @@
1-
using DocumentFormat.OpenXml;
1+
using System;
2+
using DocumentFormat.OpenXml;
3+
using System.Globalization;
4+
using DocumentFormat.OpenXml.Spreadsheet;
25

36
namespace FileFormat.Cells
47
{
5-
/// <summary>
6-
/// Represents a cell in a row.
7-
/// </summary>
8-
public class Cell
8+
public sealed class Cell
99
{
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)
1916
{
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));
2219
}
2320

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)
2922
{
30-
this.cell.CellReference = value;
23+
PutValue(value, CellValues.String);
3124
}
3225

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)
3727
{
38-
this.cell.DataType = DocumentFormat.OpenXml.Spreadsheet.CellValues.String;
28+
PutValue(value.ToString(CultureInfo.InvariantCulture), CellValues.Number);
3929
}
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)
4432
{
45-
this.cell.DataType = DocumentFormat.OpenXml.Spreadsheet.CellValues.Number;
33+
PutValue(value.ToOADate().ToString(CultureInfo.InvariantCulture), CellValues.Date);
4634
}
4735

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)
5337
{
54-
this.cell.CellValue = new DocumentFormat.OpenXml.Spreadsheet.CellValue(value);
38+
_cell.DataType = new EnumValue<CellValues>(dataType);
39+
_cell.CellValue = new CellValue(value);
40+
5541
}
5642

57-
/// <summary>
58-
/// Sets the style index of the cell to 1.
59-
/// </summary>
60-
public void CellIndex()
43+
public void PutFormula(string formula)
6144
{
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
6347
}
6448

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+
}
6953

70-
public void CellIndex(UInt32Value num)
54+
public CellValues? GetDataType()
7155
{
72-
this.cell.StyleIndex = num;
56+
return _cell.DataType?.Value;
7357
}
7458

59+
7560

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+
}
7770
}
7871

7972
}

FileFormat.Cells/Properties.cs

Lines changed: 0 additions & 72 deletions
This file was deleted.

FileFormat.Cells/Row.cs

Lines changed: 0 additions & 42 deletions
This file was deleted.

0 commit comments

Comments
 (0)