Skip to content

Commit

Permalink
Merge pull request #81 from anmcgrath/date-formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
anmcgrath authored Jun 22, 2024
2 parents 3e7bbf5 + fd455ad commit 69d97cd
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 12 deletions.
2 changes: 1 addition & 1 deletion src/BlazorDatasheet.Core/Data/Sheet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -420,7 +420,7 @@ public void EndBatchUpdates()
/// <param name="row"></param>
/// <param name="col"></param>
/// <returns></returns>
public CellFormat? GetFormat(int row, int col)
public CellFormat GetFormat(int row, int col)
{
var defaultFormat = new CellFormat();
var cellFormat = Cells.GetFormat(row, col).Clone();
Expand Down
2 changes: 1 addition & 1 deletion src/BlazorDatasheet.Core/Data/SheetCell.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public T GetValue<T>()
/// Gets the merged format at the cell's position. Setting the format will
/// merge the format with existing formats in the sheet.
/// </summary>
public CellFormat? Format
public CellFormat Format
{
get => _sheet.GetFormat(Row, Col);
set => _sheet.Cells.MergeFormatImpl(new Region(Row, Col), value);
Expand Down
6 changes: 5 additions & 1 deletion src/BlazorDatasheet.SharedPages/Pages/Index.razor
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
<button @onclick="@(() => Sheet.Cells.Merge(Sheet.Selection.Regions))">Merge cells</button>
<button @onclick="@(SortSelectionDesc)">Sort Desc</button>
<button @onclick="@(SortSelectionAsc)">Sort ASc</button>
<button @onclick="@(() => Sheet.SetFormat(Sheet.Selection.Regions, new CellFormat(){NumberFormat="C2"}))">Currency</button>
<button @onclick="@(() => Sheet.SetFormat(Sheet.Selection.Regions, new CellFormat() { NumberFormat = "C2" }))">Currency</button>
</div>

<div>
Expand Down Expand Up @@ -161,6 +161,10 @@
Sheet.Cells[3, 3].Value = "Date/Time:";
Sheet.Cells[3, 4].Type = "datetime";
Sheet.Cells[3, 4].Value = DateTime.Now;
Sheet.Cells[3, 4].Format = new CellFormat()
{
NumberFormat = "yyyy-MM-dd"
};

Sheet.Cells[4, 3].Value = "Select:";
Sheet.Cells[4, 4].Type = "select";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
@using BlazorDatasheet.Core.Interfaces
@using BlazorDatasheet.Core.Data
@using BlazorDatasheet.Core.Edit
@using BlazorDatasheet.Formula.Core
@inherits BaseEditor

<input
Expand Down Expand Up @@ -37,19 +38,20 @@
public override void BeforeEdit(IReadOnlyCell cell, Sheet sheet)
{
Cell = cell;
CurrentValue = cell?.Value?.ToString() ?? string.Empty;
}

public override void BeginEdit(EditEntryMode entryMode, string? editValue, string key)
{
/*var canParse = DateTime.TryParse(editValue?.ToString(), out var parsedDateTime);
if (canParse)
{
CurrentDateTime = parsedDateTime;
}
else
{
CurrentDateTime = DateTime.Now;
}*/
/*var canParse = DateTime.TryParse(editValue?.ToString(), out var parsedDateTime);
if (canParse)
{
CurrentDateTime = parsedDateTime;
}
else
{
CurrentDateTime = DateTime.Now;
}*/
}

}
3 changes: 3 additions & 0 deletions src/BlazorDatasheet/Render/VisualCell.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,11 @@ public VisualCell(int row, int col, Sheet sheet)

var cellValue = sheet.Cells.GetCellValue(row, col);
Value = cellValue.Data;

if (cellValue.ValueType == CellValueType.Number && format.NumberFormat != null)
FormattedString = (cellValue.GetValue<double>()).ToString(format.NumberFormat);
else if (cellValue.ValueType == CellValueType.Date && format.NumberFormat != null)
FormattedString = (cellValue.GetValue<DateTime>()).ToString(format.NumberFormat);
else
FormattedString = Value?.ToString() ?? string.Empty;

Expand Down

0 comments on commit 69d97cd

Please sign in to comment.