Skip to content

Commit

Permalink
Added sort capabilities to SheetReader & SheetControl
Browse files Browse the repository at this point in the history
  • Loading branch information
smourier committed Oct 17, 2024
1 parent 8ef4184 commit 37f615f
Show file tree
Hide file tree
Showing 11 changed files with 3,098 additions and 58 deletions.
2,855 changes: 2,855 additions & 0 deletions SheetReader.Test/x.json

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion SheetReader.Wpf.Test/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ public string? FileName
_fileName = value;
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(FileName)));
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(HasFile)));
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(HasNotTempFile)));
}
}

Expand Down Expand Up @@ -512,7 +513,7 @@ private sealed class StyledBookDocument : BookDocument

private sealed class StyledBookDocumentSheet(BookDocument book, Sheet sheet) : BookDocumentSheet(book, sheet)
{
protected override IDictionary<int, Column> CreateColumns() => new ConcurrentDictionary<int, Column>();
protected override IDictionary<int, BookDocumentColumn> CreateColumns() => new ConcurrentDictionary<int, BookDocumentColumn>();
protected override IDictionary<int, BookDocumentRow> CreateRows() => new ConcurrentDictionary<int, BookDocumentRow>();
protected override BookDocumentRow CreateRow(BookDocument book, Row row) => new StyledBookDocumentRow(book, this, row);
}
Expand Down
40 changes: 37 additions & 3 deletions SheetReader.Wpf/SheetControl.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Globalization;
using System.Net;
using System.Runtime.CompilerServices;
Expand Down Expand Up @@ -182,7 +183,7 @@ private void SetColumnsAutoSize(int columnIndex, bool includeNextColumns, IEnume
var sizes = new Dictionary<int, double>();
foreach (var row in rows)
{
context.RowCol.RowIndex = row.RowIndex;
context.RowCol.RowIndex = row.SortIndex;
for (var i = columnIndex; i < _columnSettings.Count; i++)
{
var colWidth = _columnSettings[i].Width;
Expand Down Expand Up @@ -247,10 +248,10 @@ protected virtual void OnSheetChanged()
{
if (!sheet.Columns.TryGetValue(i, out var column))
{
column = new Column
column = new BookDocumentColumn(new Column
{
Index = i,
};
});
}

_columnSettings.Add(new SheetControlColumn(column) { Width = GetColumnWidth() });
Expand Down Expand Up @@ -437,9 +438,28 @@ protected override void OnPreviewMouseDoubleClick(MouseButtonEventArgs e)
var result = HitTest(e.GetPosition(_scrollViewer));
if (result.MovingColumnIndex.HasValue)
{
// move column size
var next = Keyboard.Modifiers.HasFlag(ModifierKeys.Shift) && Keyboard.Modifiers.HasFlag(ModifierKeys.Control);
SetColumnsAutoSize(result.MovingColumnIndex.Value, next, sheet.Rows.Values);
}
else if (result.RowCol != null)
{
// sort by column
if (sheet.SortDirection == ListSortDirection.Ascending && sheet.SortColumnIndex == result.RowCol.ColumnIndex)
{
sheet.UnsortRows();
}
else
{
var direction = ListSortDirection.Descending;
if (sheet.SortDirection.HasValue && sheet.SortColumnIndex == result.RowCol.ColumnIndex)
{
direction = ListSortDirection.Ascending;
}

sheet.SortRows(result.RowCol.ColumnIndex, direction, null);
}
}
}
}
}
Expand Down Expand Up @@ -798,6 +818,20 @@ protected override void OnRender(DrawingContext drawingContext)
var textOffsetY = (context.RowHeight.Value - formattedCol.Height) / 2; // center vertically
drawingContext.DrawText(formattedCol, new Point(currentColX + context.LineSize.Value / 2 + textOffsetX, offsetY + textOffsetY));

if (_control.Sheet.SortDirection.HasValue && _control.Sheet.SortColumnIndex == i)
{
var arrowText = _control.Sheet.SortDirection.Value == ListSortDirection.Ascending ? "▲" : "▼";
var arrow = new FormattedText(arrowText, CultureInfo.CurrentCulture, FlowDirection.LeftToRight, context.Typeface, _control.FontSize, _control.Foreground, context.PixelsPerDip)
{
MaxTextWidth = colWidth,
MaxLineCount = 1
};

const int arrowPadding = 10;
var arrowOffsetY = (context.RowHeight.Value - arrow.Height) / 2; // center vertically
drawingContext.DrawText(arrow, new Point(currentColX + colWidth - arrowPadding, offsetY + arrowOffsetY));
}

drawingContext.DrawLine(context.LinePen, new Point(currentColX, offsetY), new Point(currentColX, offsetY + Math.Min(rowsHeight, viewHeight)));
currentColX += colWidth + context.LineSize.Value;
}
Expand Down
4 changes: 2 additions & 2 deletions SheetReader.Wpf/SheetControlColumn.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ namespace SheetReader.Wpf
{
public class SheetControlColumn
{
public SheetControlColumn(Column column)
public SheetControlColumn(BookDocumentColumn column)
{
ArgumentNullException.ThrowIfNull(column);
Column = column;
}

public Column Column { get; }
public BookDocumentColumn Column { get; }
public virtual double Width { get; set; }

public override string ToString() => $"{Column} {Width}";
Expand Down
28 changes: 18 additions & 10 deletions SheetReader/Book.cs
Original file line number Diff line number Diff line change
Expand Up @@ -384,18 +384,21 @@ public override IEnumerable<Row> EnumerateRows()
JsonElement? cells = null;
if (Format.CellsPropertyName == null)
{
if (Element.TryGetProperty("cells", out var element) && element.ValueKind == JsonValueKind.Array)
if (Element.ValueKind == JsonValueKind.Object)
{
cells = element;
}
else if (Element.TryGetProperty("Cells", out element) && element.ValueKind == JsonValueKind.Array)
{
cells = element;
if (Element.TryGetProperty("cells", out var element) && element.ValueKind == JsonValueKind.Array)
{
cells = element;
}
else if (Element.TryGetProperty("Cells", out element) && element.ValueKind == JsonValueKind.Array)
{
cells = element;
}
}
}
else
{
if (Element.TryGetProperty(Format.CellsPropertyName, out var element) && element.ValueKind == JsonValueKind.Array)
if (Element.ValueKind == JsonValueKind.Object && Element.TryGetProperty(Format.CellsPropertyName, out var element) && element.ValueKind == JsonValueKind.Array)
{
cells = element;
}
Expand All @@ -417,11 +420,11 @@ public override IEnumerable<Row> EnumerateRows()
{
rows = Element;
}
else if (Element.TryGetProperty("rows", out var element) && element.ValueKind == JsonValueKind.Array)
else if (Element.ValueKind == JsonValueKind.Object && Element.TryGetProperty("rows", out var element) && element.ValueKind == JsonValueKind.Array)
{
rows = element;
}
else if (Element.TryGetProperty("Rows", out element) && element.ValueKind == JsonValueKind.Array)
else if (Element.ValueKind == JsonValueKind.Object && Element.TryGetProperty("Rows", out element) && element.ValueKind == JsonValueKind.Array)
{
rows = element;
}
Expand Down Expand Up @@ -450,7 +453,7 @@ public override IEnumerable<Row> EnumerateRows()
}
else
{
if (!Element.TryGetProperty(Format.RowsPropertyName, out var element) || element.ValueKind != JsonValueKind.Array)
if (Element.ValueKind != JsonValueKind.Object || !Element.TryGetProperty(Format.RowsPropertyName, out var element) || element.ValueKind != JsonValueKind.Array)
yield break;

rows = element;
Expand Down Expand Up @@ -941,6 +944,11 @@ public virtual IEnumerable<Row> EnumerateDeclaredRows()
var row = CreateRow();
if (row != null)
{
if (Format.LoadOptions.HasFlag(LoadOptions.FirstRowDefinesColumns))
{
rowIndex--;
}

row.Index = rowIndex - 1;
var hidden = Reader.GetAttribute("hidden");
if (hidden != null && Extensions.EqualsIgnoreCase(hidden, "true") || hidden == "1")
Expand Down
10 changes: 3 additions & 7 deletions SheetReader/BookDocument.cs
Original file line number Diff line number Diff line change
Expand Up @@ -531,17 +531,13 @@ void writeSheet(BookDocumentSheet sheet)
writer.WriteStartObject();
writeSheetHeader(sheet);

if (!sheet.ColumnsHaveBeenGenerated)
{
writeColumns(sheet);
}
writeColumns(sheet);

writer.WritePropertyName(format.RowsPropertyName ?? "cells");
writer.WriteStartArray();

if (sheet.FirstRowIndex.HasValue && sheet.LastRowIndex.HasValue)
{
var rowOffset = sheet.ColumnsHaveBeenGenerated ? 0 : -1;
for (var rowIndex = sheet.FirstRowIndex.Value; rowIndex <= sheet.LastRowIndex.Value; rowIndex++)
{
sheet.Rows.TryGetValue(rowIndex, out var row);
Expand All @@ -551,7 +547,7 @@ void writeSheet(BookDocumentSheet sheet)
{
if (row.Cells.TryGetValue(columnIndex, out var cell))
{
writePositionedCell(cell, rowIndex + rowOffset, columnIndex);
writePositionedCell(cell, rowIndex, columnIndex);
}
}
}
Expand All @@ -563,7 +559,7 @@ void writeSheet(BookDocumentSheet sheet)
return;
}

if (sheet.ColumnsHaveBeenGenerated && !options.HasFlag(ExportOptions.JsonRowsAsObject))
if (!options.HasFlag(ExportOptions.JsonRowsAsObject))
{
writer.WriteStartArray();
if (sheet.FirstRowIndex.HasValue && sheet.LastRowIndex.HasValue)
Expand Down
20 changes: 20 additions & 0 deletions SheetReader/BookDocumentColumn.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using System;

namespace SheetReader
{
public class BookDocumentColumn
{
public BookDocumentColumn(Column column)
{
ArgumentNullException.ThrowIfNull(column);
Column = column;
}

public Column Column { get; }
public virtual int Index => Column.Index;
public virtual string? Name => Column.Name;
public virtual bool IsAutoGenerated { get; set; }

public override string ToString() => Column.Name ?? string.Empty;
}
}
9 changes: 7 additions & 2 deletions SheetReader/BookDocumentRow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,18 @@ public BookDocumentRow(BookDocument book, BookDocumentSheet sheet, Row row)

Row = row;
RowIndex = row.Index;
SortIndex = RowIndex;
IsHidden = !row.IsVisible;
foreach (var cell in row.EnumerateCells())
{
var bdCell = CreateCell(cell);
if (bdCell == null)
continue;

_cells[cell.ColumnIndex] = CreateCell(cell);
if (!sheet.EnsureColumn(book, cell.ColumnIndex))
break;

_cells[cell.ColumnIndex] = bdCell;

if (LastCellIndex == null || row.Index > LastCellIndex)
{
Expand All @@ -45,7 +49,8 @@ public BookDocumentRow(BookDocument book, BookDocumentSheet sheet, Row row)
}

public Row Row { get; }
public int RowIndex { get; }
public int RowIndex { get; } // orginal index
public virtual int SortIndex { get; set; } // current sorted index
public virtual bool IsHidden { get; }
public int? FirstCellIndex { get; }
public int? LastCellIndex { get; }
Expand Down
Loading

0 comments on commit 37f615f

Please sign in to comment.