Skip to content

Commit 7c3800e

Browse files
author
fileformat-cells
authored
Merge pull request #9 from fahadadeel/main
Revamp and Feature Enhancements for FileFormat.Cells
2 parents 5ff3b0a + d1fd5a6 commit 7c3800e

13 files changed

+1190
-1412
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
using System;
2+
namespace FileFormat.Cells
3+
{
4+
public class BuiltInDocumentProperties
5+
{
6+
public string Author { get; set; }
7+
public string Title { get; set; }
8+
public DateTime CreatedDate { get; set; }
9+
public string ModifiedBy { get; set; }
10+
public DateTime ModifiedDate { get; set; }
11+
public string Subject { get; set; }
12+
13+
// ... other properties as needed ...
14+
}
15+
16+
}
17+

FileFormat.Cells/Cell.cs

+75-35
Original file line numberDiff line numberDiff line change
@@ -1,79 +1,119 @@
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;
10+
private readonly DocumentFormat.OpenXml.Spreadsheet.Cell _cell;
11+
private readonly SheetData _sheetData;
1412

1513
/// <summary>
16-
/// Instantiate a new instance of the Cell class.
14+
/// Gets the cell reference in A1 notation.
1715
/// </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)
1927
{
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));
2230
}
2331

2432
/// <summary>
25-
/// This method is used to set the Cell Reference in a worksheet.
33+
/// Sets the value of the cell as a string.
2634
/// </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)
2937
{
30-
this.cell.CellReference = value;
38+
PutValue(value, CellValues.String);
3139
}
3240

3341
/// <summary>
34-
/// This method is used to set the Cell data type to String.
42+
/// Sets the value of the cell as a number.
3543
/// </summary>
36-
public void setStringDataType()
44+
/// <param name="value">The numeric value to set.</param>
45+
public void PutValue(double value)
3746
{
38-
this.cell.DataType = DocumentFormat.OpenXml.Spreadsheet.CellValues.String;
47+
PutValue(value.ToString(CultureInfo.InvariantCulture), CellValues.Number);
3948
}
49+
4050
/// <summary>
41-
/// This method is used to set the Cell data type to Number.
51+
/// Sets the value of the cell as a date.
4252
/// </summary>
43-
public void setNumberDataType()
53+
/// <param name="value">The date value to set.</param>
54+
public void PutValue(DateTime value)
4455
{
45-
this.cell.DataType = DocumentFormat.OpenXml.Spreadsheet.CellValues.Number;
56+
PutValue(value.ToOADate().ToString(CultureInfo.InvariantCulture), CellValues.Date);
4657
}
4758

4859
/// <summary>
49-
/// This method is used to set the value of a Cell.
60+
/// Sets the cell's value with a specific data type.
5061
/// </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)
5365
{
54-
this.cell.CellValue = new DocumentFormat.OpenXml.Spreadsheet.CellValue(value);
66+
_cell.DataType = new EnumValue<CellValues>(dataType);
67+
_cell.CellValue = new CellValue(value);
68+
5569
}
5670

5771
/// <summary>
58-
/// Sets the style index of the cell to 1.
72+
/// Sets a formula for the cell.
5973
/// </summary>
60-
public void CellIndex()
74+
/// <param name="formula">The formula to set.</param>
75+
public void PutFormula(string formula)
6176
{
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
6379
}
6480

6581
/// <summary>
66-
/// Sets the style index of the cell to the specified value.
82+
/// Gets the value of the cell.
6783
/// </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+
}
6989

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()
7195
{
72-
this.cell.StyleIndex = num;
96+
return _cell.DataType?.Value;
7397
}
7498

7599

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+
}
77117
}
78118

79119
}

FileFormat.Cells/CellStyle.cs

-27
This file was deleted.

0 commit comments

Comments
 (0)