Skip to content

Commit

Permalink
Merge pull request #52 from anmcgrath/formula_2
Browse files Browse the repository at this point in the history
Formula + many other significant changes
  • Loading branch information
anmcgrath authored Nov 2, 2023
2 parents 9c80a89 + 4b0382f commit 858d4e6
Show file tree
Hide file tree
Showing 322 changed files with 13,922 additions and 5,560 deletions.
90 changes: 87 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,18 @@

A simple datasheet component for editing tabular data.

![image](https://user-images.githubusercontent.com/34253568/197425287-690a747a-24f5-4e0d-afcf-e2a09efbaba2.png)
<img width="820" alt="Screenshot 2023-11-02 at 5 58 45 pm" src="https://github.com/anmcgrath/BlazorDatasheet/assets/34253568/10f70492-0d95-4ce2-bcbb-009b6a3976bd">

#### Features
- Data editing
- Built in editors including text, date, select, boolean, text area, enum
- Add custom editors for any data type
- Conditional formatting
- Data validation
- Build datasheet from an object definition
- Formula
- Keyboard navigation
- Copy and paste from tabulated data
- Virtualization via Blazor Virtualization - handles many cells at once.
- Virtualization - _handles_ many cells at once in both rows & cols.

Demo: https://anmcgrath.github.io/BlazorDatasheet/

Expand Down Expand Up @@ -43,3 +43,87 @@ The following code displays an empty 3 x 3 data grid.
```

The default editor is the text editor, but can be changed by defining the Type property of each cell.

### Setting & getting cell values

Cell values can be set in a few ways:

```csharp
sheet.Cells[0, 0].Value = "Test"
sheet.Range("A1").Value = "Test";
sheet.Cells.SetValue(0, 0, "Test");
sheet.Commands.ExecuteCommand(new SetCellValueCommand(0, 0, "Test"));
```

In this example, the first two methods set the value but cannot be undone. The last two methods can be undone.

### Formula

Formula can be applied to cells. When the cells or ranges that the formula cells reference change, the cell value is re-calculated.

Currently, the whole sheet is calculated if any referenced cell or range changes.

```csharp
sheet.Cells[0, 0].Formula = "=10+A2"
```

### Formatting

Cell formats can be set in the following ways:

```csharp
sheet.Range("A1:A2").Format = new CellFormat() { BackgroundColor = "red" };
sheet.Commands.ExecuteCommand(
new SetFormatCommand(new RowRegion(10, 12), new CellFormat() { ForegroundColor = "blue" }));
sheet.SetFormat(sheet.Range(new ColumnRegion(5)), new CellFormat() { FontWeight = "bold" });
sheet.Cells[0, 0].Format = new CellFormat() { TextAlign = "center" };
```

When a cell format is set, it will be merged into any existing cell formats in the region that it is applied to. Any non-null format paremeters will be merged:

```csharp
sheet.Range("A1").Format = new CellFormat() { BackgroundColor = "red" };
sheet.Range("A1:A2").Format = new CellFormat() { ForegroundColor = "blue" };
var format = sheet.Cells[0, 0].Format; // backroundColor = "red", foreground = "blue"
var format2 = sheet.Cells[1, 0].Format; // foreground = "blue"
```

### Cell types
The cell type specifies which renderer and editor are used for the cell.

```csharp
sheet.Range("A1:B5").Type = "boolean"; // renders a checkbox
```

Custom editors and renderers can be defined. See the examples for more information.

### Validation
Data validation can be set on cells/ranges. There are two modes of validation: strict and non-strict. When a validator is strict, the cell value will not be set by the editor if it fails validation.

If validation is not strict, the value can be set during editing but will show a validation error when rendered.

Although a strict validation may be set on a cell, the value can be changed programmatically, but it will display as a validation error.

```csharp
sheet.Validators.Add(new ColumnRegion(0), new NumberValidator(isStrict: true));
```

### Regions and ranges

A region is a geometric construct, for example:

```csharp
var region = new Region(0, 5, 0, 5); // r0 to r5, c0 to c5
var cellRegion = new Region(0, 0); // cell A1
var colRegion = new ColumnRegion(0, 4); // col region spanning A to D
var rowRegion = new RowRegion(0, 3); // row region spanning 1 to 4
```

A range is a collection of regions that also knows about the sheet. Ranges can be used to set certain parts of the sheet.

```csharp
var range = sheet.Range("A1:C5, B2:B7");
var range = sheet.Range(new ColumnRegion(0));
var range = sheet.Range(regions);
var range = sheet.Range(0, 0, 4, 5);
```
4 changes: 2 additions & 2 deletions src/.run/dotnet watch run.run.xml
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<component name="ProjectRunConfigurationManager">
<configuration default="false" name="dotnet watch run" type="RunNativeExe" factoryName="Native Executable">
<option name="EXE_PATH" value="/usr/local/share/dotnet/dotnet" />
<option name="EXE_PATH" value="dotnet" />
<option name="PROGRAM_PARAMETERS" value="watch run" />
<option name="WORKING_DIRECTORY" value="$PROJECT_DIR$/BlazorDatasheet.Server/" />
<option name="WORKING_DIRECTORY" value="" />
<option name="PASS_PARENT_ENVS" value="1" />
<option name="USE_EXTERNAL_CONSOLE" value="0" />
<method v="2">
Expand Down
15 changes: 15 additions & 0 deletions src/BlazorDatasheet.Core/BlazorDatasheet.Core.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\BlazorDatasheet.DataStructures\BlazorDatasheet.DataStructures.csproj" />
<ProjectReference Include="..\BlazorDatasheet.Formula.Core\BlazorDatasheet.Formula.Core.csproj" />
<ProjectReference Include="..\BlazorDatasheet.Formula.Functions\BlazorDatasheet.Formula.Functions.csproj" />
</ItemGroup>

</Project>
116 changes: 116 additions & 0 deletions src/BlazorDatasheet.Core/Color/ColorConverter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
using BlazorDatasheet.DataStructures.Util;

namespace BlazorDatasheet.Core.Color;

public class ColorConverter
{
public static System.Drawing.Color HSVToRGB(double H, double S, double V)
{
double r = 0, g = 0, b = 0;

if (S == 0)
{
r = V;
g = V;
b = V;
}
else
{
int i;
double f, p, q, t;

if (H == 360)
H = 0;
else
H = H / 60;

i = (int)Math.Truncate(H);
f = H - i;

p = V * (1.0 - S);
q = V * (1.0 - (S * f));
t = V * (1.0 - (S * (1.0 - f)));

switch (i)
{
case 0:
r = V;
g = t;
b = p;
break;

case 1:
r = q;
g = V;
b = p;
break;

case 2:
r = p;
g = V;
b = t;
break;

case 3:
r = p;
g = q;
b = V;
break;

case 4:
r = t;
g = p;
b = V;
break;

default:
r = V;
g = p;
b = q;
break;
}
}

return System.Drawing.Color.FromArgb((byte)(r * 255), (byte)(g * 255), (byte)(b * 255));
}

public static (double h, double s, double v) RGBToHSV(System.Drawing.Color color)
{
float cmax = Math.Max(color.R, Math.Max(color.G, color.B));
float cmin = Math.Min(color.R, Math.Min(color.G, color.B));
float delta = cmax - cmin;

float hue = 0;
float saturation = 0;

if (cmax == color.R)
{
hue = 60 * (((color.G - color.B) / delta) % 6);
}
else if (cmax == color.G)
{
hue = 60 * ((color.B - color.R) / delta + 2);
}
else if (cmax == color.B)
{
hue = 60 * ((color.R - color.G) / delta + 4);
}

if (cmax > 0)
{
saturation = delta / cmax;
}

return (hue, saturation, cmax);
}


public static (double h, double s, double v) HsvInterp((double h, double s, double v) c0, (double h, double s,
double v) c1, double t)
{
double h = ((1 - t) * c0.h + t * c1.h) % 360;
double s = SheetMath.ClampDouble(0, 1, ((1 - t) * c0.s + t * c1.s));
double v = SheetMath.ClampDouble(0, 1, ((1 - t) * c0.v + t * c1.v));
return (h, s, v);
}
}
31 changes: 31 additions & 0 deletions src/BlazorDatasheet.Core/Commands/ClearCellsCommand.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
using BlazorDatasheet.Core.Data;
using BlazorDatasheet.Core.Data.Cells;
using BlazorDatasheet.Formula.Core;

namespace BlazorDatasheet.Core.Commands;

/// <summary>
/// Clears cell values in the given ranges
/// </summary>
public class ClearCellsCommand : IUndoableCommand
{
private readonly SheetRange _range;
private CellStoreRestoreData _restoreData;

public ClearCellsCommand(SheetRange range)
{
_range = range.Clone();
}

public bool Execute(Sheet sheet)
{
_restoreData = sheet.Cells.ClearCellsImpl(_range.Regions);
return true;
}

public bool Undo(Sheet sheet)
{
sheet.Cells.Restore(_restoreData);
return true;
}
}
Original file line number Diff line number Diff line change
@@ -1,26 +1,32 @@
using BlazorDatasheet.Data;
using BlazorDatasheet.Core.Data;

namespace BlazorDatasheet.Commands;
namespace BlazorDatasheet.Core.Commands;

public class CommandGroup : IUndoableCommand
{
private IEnumerable<IUndoableCommand> _commands;
private List<IUndoableCommand> _successfulCommands;
private readonly List<ICommand> _commands;
private readonly List<ICommand> _successfulCommands;

/// <summary>
/// Runs a series of commands sequentially, but stops if any fails.
/// </summary>
/// <param name="commands"></param>
public CommandGroup(params IUndoableCommand[] commands)
public CommandGroup(params ICommand[] commands)
{
_commands = commands;
_successfulCommands = new List<IUndoableCommand>();
_commands = commands.ToList();
_successfulCommands = new List<ICommand>();
}

public void AddCommand(ICommand command)
{
_commands.Add(command);
}

public bool Execute(Sheet sheet)
{
_successfulCommands.Clear();

sheet.BatchUpdates();
foreach (var command in _commands)
{
var run = command.Execute(sheet);
Expand All @@ -34,16 +40,22 @@ public bool Execute(Sheet sheet)
_successfulCommands.Add(command);
}

sheet.EndBatchUpdates();

return true;
}

public bool Undo(Sheet sheet)
{
var undo = true;
foreach (var command in _successfulCommands)
var undoCommands = _successfulCommands.Where(cmd => cmd is IUndoableCommand).Cast<IUndoableCommand>().ToList();
undoCommands.Reverse();
sheet.BatchUpdates();
foreach (var command in undoCommands)
{
undo &= command.Undo(sheet);
}
sheet.EndBatchUpdates();

return undo;
}
Expand Down
Loading

0 comments on commit 858d4e6

Please sign in to comment.