Skip to content

Commit

Permalink
Added column sorting & moving
Browse files Browse the repository at this point in the history
  • Loading branch information
smourier committed Oct 19, 2024
1 parent 37f615f commit 191d8b9
Show file tree
Hide file tree
Showing 11 changed files with 831 additions and 2,967 deletions.
421 changes: 362 additions & 59 deletions Amalgamation/SheetReader.cs

Large diffs are not rendered by default.

2,855 changes: 0 additions & 2,855 deletions SheetReader.Test/x.json

This file was deleted.

1 change: 1 addition & 0 deletions SheetReader.Wpf.Test/MainWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@
<TabControl.ContentTemplate>
<DataTemplate>
<smx:SheetControl
MouseDoubleClick="SheetControl_MouseDoubleClick"
MouseMove="SheetControl_MouseMove"
SelectionChanged="SheetControl_SelectionChanged"
Sheet="{Binding}" />
Expand Down
41 changes: 29 additions & 12 deletions SheetReader.Wpf.Test/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -478,32 +478,49 @@ private void Grid_Drop(object sender, DragEventArgs e)
}
}

private void SheetControl_MouseDoubleClick(object sender, MouseButtonEventArgs e)
{
var ctl = (SheetControl)sender;
var result = ctl.HitTest(e);
var cell = result.Cell;
if (cell == null)
return;

var formatted = ctl.Sheet.FormatValue(cell.Value);
MessageBox.Show(formatted, result.RowCol!.ExcelReference);
}

private void SheetControl_MouseMove(object sender, MouseEventArgs e)
{
var ctl = (SheetControl)sender;
var result = ctl.HitTest(e.GetPosition(ctl));
status.Text = GetHitTestText(ctl, e, 100);
}

private string GetHitTestText(SheetControl ctl, MouseEventArgs e, int max)
{
var result = ctl.HitTest(e);
if (result.RowCol != null)
{
if (result.IsOverRowHeader)
{
status.Text = $"Row: {result.RowCol.RowIndex + 1}";
return;
}
return $"Row: {result.RowCol.RowIndex + 1}";

if (result.IsOverColumnHeader)
{
status.Text = $"Column: {Row.GetExcelColumnName(result.RowCol.ColumnIndex)} ({result.RowCol.ColumnIndex})";
return;
}
return $"Column: {Row.GetExcelColumnName(result.RowCol.ColumnIndex)} ({result.RowCol.ColumnIndex})";

var cell = result.Cell;
if (cell != null)
{
status.Text = $"Cell: {result.RowCol.ExcelReference}: {ctl.Sheet.FormatValue(cell.Value)}";
return;
var type = cell.Value?.GetType().Name ?? "<null>";
var formatted = ctl.Sheet.FormatValue(cell.Value)?.Replace('\r', '⇣')?.Replace('\n', '⇣');

if (formatted?.Length > max)
{
formatted = formatted[..max] + "…";
}
return $"Cell: {result.RowCol.ExcelReference}: '{formatted}' of type {type}";
}
}
status.Text = string.Empty;
return string.Empty;
}

private sealed class StyledBookDocument : BookDocument
Expand Down
245 changes: 217 additions & 28 deletions SheetReader.Wpf/SheetControl.cs

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions SheetReader.Wpf/SheetControlHitTestResult.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ public sealed class SheetControlHitTestResult
public BookDocumentCell? Cell { get; internal set; }
public bool IsOverRowHeader { get; internal set; }
public bool IsOverColumnHeader { get; internal set; }
public bool IsInViewport { get; internal set; }
public int? SizingColumnIndex { get; internal set; }
public int? MovingColumnIndex { get; internal set; }

public bool IsOverCorner => IsOverColumnHeader && IsOverRowHeader;
Expand Down
48 changes: 48 additions & 0 deletions SheetReader.Wpf/SheetSelection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,54 @@ public RowCol BottomRight
public RowCol TopRight => new(TopLeft.RowIndex, BottomRight.ColumnIndex);
public RowCol BottomLeft => new(BottomRight.RowIndex, TopLeft.ColumnIndex);

public bool CrossesRow(int rowIndex)
{
var topLeft = TopLeft;
if (rowIndex < topLeft.RowIndex)
return false;

var bottomRight = BottomRight;
if (rowIndex > bottomRight.RowIndex)
return false;

return true;
}

public bool CrossesColumn(int columnIndex)
{
var topLeft = TopLeft;
if (columnIndex < topLeft.ColumnIndex)
return false;

var bottomRight = BottomRight;
if (columnIndex > bottomRight.ColumnIndex)
return false;

return true;
}

public bool Contains(RowCol? rowCol)
{
if (rowCol == null)
return false;

var topLeft = TopLeft;
if (rowCol.ColumnIndex < topLeft.ColumnIndex)
return false;

if (rowCol.RowIndex < topLeft.RowIndex)
return false;

var bottomRight = BottomRight;
if (rowCol.ColumnIndex > bottomRight.ColumnIndex)
return false;

if (rowCol.RowIndex > bottomRight.RowIndex)
return false;

return true;
}

public virtual void SelectRow(int rowIndex)
{
if (Control.Sheet == null || !Control.Sheet.LastRowIndex.HasValue)
Expand Down
57 changes: 51 additions & 6 deletions SheetReader/BookDocument.cs
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,18 @@ static bool isDefaultJsonValue(object? value)
if (value is decimal dec && dec == 0m)
return true;

if (value is DateTime dt && dt == DateTime.MinValue)
return true;

if (value is DateTimeOffset dto && dto == DateTimeOffset.MinValue)
return true;

if (value is TimeSpan ts && ts == TimeSpan.Zero)
return true;

if (value is Guid g && g == Guid.Empty)
return true;

if (value is uint ui && ui == 0)
return true;

Expand All @@ -224,15 +236,19 @@ static bool isDefaultJsonValue(object? value)

void writePositionedCell(BookDocumentCell? cell, int rowIndex, int columnIndex)
{
// don't output null values
if (cell == null || cell.Value == null || Convert.IsDBNull(cell.Value))
return;

writer.WriteStartObject();
writer.WriteNumber("r", rowIndex);
writer.WriteNumber("c", columnIndex);
writer.WritePropertyName("value");

// don't output null values
if (cell == null || cell.Value == null || Convert.IsDBNull(cell.Value))
{
writer.WriteNullValue();
writer.WriteEndObject();
return;
}

if (cell.IsError)
{
if (cell.Value != null)
Expand Down Expand Up @@ -308,6 +324,20 @@ void writePositionedCell(BookDocumentCell? cell, int rowIndex, int columnIndex)
return;
}

if (cell.Value is DateTime dt)
{
writer.WriteStringValue(dt.ToString("u"));
writer.WriteEndObject();
return;
}

if (cell.Value is DateTimeOffset dto)
{
writer.WriteStringValue(dto.ToString("u"));
writer.WriteEndObject();
return;
}

if (cell.Value is byte[] bytes)
{
writer.WriteBase64StringValue(bytes);
Expand Down Expand Up @@ -420,6 +450,18 @@ void writeCell(BookDocumentCell? cell)
return;
}

if (cell.Value is DateTime dt)
{
writer.WriteStringValue(dt.ToString("u"));
return;
}

if (cell.Value is DateTimeOffset dto)
{
writer.WriteStringValue(dto.ToString("u"));
return;
}

if (cell.Value is byte[] bytes)
{
writer.WriteBase64StringValue(bytes);
Expand Down Expand Up @@ -481,7 +523,7 @@ void writeRowAsObjects(BookDocumentSheet sheet, BookDocumentRow? row, Dictionary
row?.Cells.TryGetValue(columnIndex, out cell);
if (!options.HasFlag(ExportOptions.JsonNoDefaultCellValues) || (cell != null && !isDefaultJsonValue(cell.Value)))
{
string colName = colNames[columnIndex];
var colName = colNames[columnIndex];
writer.WritePropertyName(colName);
writeCell(cell);
}
Expand Down Expand Up @@ -547,7 +589,10 @@ void writeSheet(BookDocumentSheet sheet)
{
if (row.Cells.TryGetValue(columnIndex, out var cell))
{
writePositionedCell(cell, rowIndex, columnIndex);
if (!options.HasFlag(ExportOptions.JsonNoDefaultCellValues) || (cell != null && !isDefaultJsonValue(cell.Value)))
{
writePositionedCell(cell, rowIndex, columnIndex);
}
}
}
}
Expand Down
10 changes: 8 additions & 2 deletions SheetReader/BookDocumentRow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ namespace SheetReader
{
public class BookDocumentRow
{
private readonly IDictionary<int, BookDocumentCell> _cells;
private IDictionary<int, BookDocumentCell> _cells;

public BookDocumentRow(BookDocument book, BookDocumentSheet sheet, Row row)
{
Expand Down Expand Up @@ -56,7 +56,7 @@ public BookDocumentRow(BookDocument book, BookDocumentSheet sheet, Row row)
public int? LastCellIndex { get; }
public IDictionary<int, BookDocumentCell> Cells => _cells;

protected virtual IDictionary<int, BookDocumentCell> CreateCells() => new Dictionary<int, BookDocumentCell>();
protected virtual internal IDictionary<int, BookDocumentCell> CreateCells() => new Dictionary<int, BookDocumentCell>();
protected virtual BookDocumentCell CreateCell(Cell cell)
{
ArgumentNullException.ThrowIfNull(cell);
Expand All @@ -66,6 +66,12 @@ protected virtual BookDocumentCell CreateCell(Cell cell)
return cell.IsError ? new BookDocumentCellError(cell) : new BookDocumentCell(cell);
}

protected virtual internal void ReplaceCells(IDictionary<int, BookDocumentCell> cells)
{
ArgumentNullException.ThrowIfNull(cells);
_cells = cells;
}

public override string ToString() => RowIndex.ToString();
}
}
Loading

0 comments on commit 191d8b9

Please sign in to comment.