Skip to content

Commit fbacbfa

Browse files
authored
Merge pull request #224 from anmcgrath/formula-fixes
Formula fixes
2 parents 053cb1d + ca7b2bd commit fbacbfa

File tree

17 files changed

+121
-133
lines changed

17 files changed

+121
-133
lines changed

src/BlazorDatasheet.Core/Edit/Editor.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -205,8 +205,11 @@ public bool AcceptEdit()
205205
}
206206

207207
var formulaResult = isFormula ? Sheet.FormulaEngine.Evaluate(parsedFormula) : CellValue.Empty;
208+
208209
var editValue =
209-
isFormula ? formulaResult : Sheet.Cells.ConvertToCellValue(EditCell.Row, EditCell.Col, EditValue);
210+
isFormula ? formulaResult :
211+
Sheet.Cells.ConvertToCellValue(EditCell.Row, EditCell.Col,
212+
string.IsNullOrEmpty(EditValue) ? null : EditValue); // set empty string to null to clear cell
210213

211214
// don't accept edit if not a formula and the value is the same as previous.
212215
if (!isFormula && editValue.IsEqualTo(EditCell.CellValue))

src/BlazorDatasheet.Formula.Core/CellValue.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public class CellValue : IComparable, IComparable<CellValue>
1515

1616
public CellValue(object? data)
1717
{
18-
if (data == null)
18+
if (data == null || (data is string str && str.Length == 0))
1919
{
2020
Data = null;
2121
IsEmpty = true;
@@ -289,6 +289,11 @@ public bool IsCellReference()
289289

290290
public bool IsEqualTo(CellValue value)
291291
{
292+
// special handling of empty cell vs empty string.
293+
if (value.IsEmpty && string.IsNullOrEmpty(Data?.ToString()) ||
294+
IsEmpty && string.IsNullOrEmpty(value.Data?.ToString()))
295+
return true;
296+
292297
if (ValueType != value.ValueType)
293298
return false;
294299

src/BlazorDatasheet.Formula.Core/Interpreter/Evaluation/CellValueCoercer.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ public bool TryCoerceBool(CellValue cellValue, out bool val)
9898

9999

100100
if (cellValue.IsEmpty)
101-
return false;
101+
return true; // val = false
102102

103103
if (cellValue.ValueType == CellValueType.Logical)
104104
{
@@ -146,10 +146,10 @@ public bool TryCoerceString(CellValue cellValue, out string str)
146146
return false;
147147
}
148148

149-
if (cellValue.Data == null)
149+
if (cellValue.IsEmpty)
150150
str = string.Empty;
151151
else
152-
str = cellValue.Data.ToString()!;
152+
str = cellValue.Data!.ToString()!;
153153

154154
return true;
155155
}

src/BlazorDatasheet.Formula.Core/Interpreter/Evaluation/Evaluator.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -195,10 +195,10 @@ private CellValue EvaluateFunctionCall(FunctionExpression node)
195195
var paramDefinition = paramDefinitions[paramIndex];
196196
var arg = EvaluateExpression(node.Args[argIndex]);
197197

198-
if (arg.IsError() && !func.AcceptsErrors)
199-
return arg;
200-
201198
convertedArgs[argIndex] = _parameterConverter.ConvertVal(arg, paramDefinition.Type);
199+
200+
if (convertedArgs[argIndex].IsError() && !func.AcceptsErrors)
201+
return convertedArgs[argIndex];
202202

203203
if (IsConsumable(paramDefinition))
204204
paramIndex++;
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
using BlazorDatasheet.Formula.Core;
2+
3+
namespace BlazorDatasheet.Formula.Functions.Logical;
4+
5+
public class NotFunction : ISheetFunction
6+
{
7+
public ParameterDefinition[] GetParameterDefinitions()
8+
{
9+
return new ParameterDefinition[]
10+
{
11+
new("value", ParameterType.Logical, ParameterRequirement.Required)
12+
};
13+
}
14+
15+
public CellValue Call(CellValue[] args, FunctionCallMetaData metaData)
16+
{
17+
var val = args[0].GetValue<bool>();
18+
return CellValue.Logical(!val);
19+
}
20+
21+
public bool AcceptsErrors => false;
22+
public bool IsVolatile => false;
23+
}

src/BlazorDatasheet.Formula.Functions/Math/PowerFunction.cs

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,16 +21,10 @@ public ParameterDefinition[] GetParameterDefinitions()
2121

2222
public CellValue Call(CellValue[] args, FunctionCallMetaData metaData)
2323
{
24-
var number = args[0];
25-
var exponent = args[1];
24+
var number = args[0].GetValue<double>();
25+
var exponent = args[1].GetValue<double>();
2626

27-
if (number.ValueType != CellValueType.Number)
28-
return CellValue.Error(ErrorType.Value);
29-
30-
if (exponent.ValueType != CellValueType.Number)
31-
return CellValue.Error(ErrorType.Value);
32-
33-
return CellValue.Number(System.Math.Pow((double)number.Data!, (double)exponent.Data!));
27+
return CellValue.Number(System.Math.Pow(number, exponent));
3428
}
3529

3630
public bool AcceptsErrors => false;

src/BlazorDatasheet.Formula.Functions/Math/SinFunction.cs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,6 @@ public ParameterDefinition[] GetParameterDefinitions()
1818
public CellValue Call(CellValue[] args, FunctionCallMetaData metaData)
1919
{
2020
var val = args[0];
21-
22-
if (val.ValueType != CellValueType.Number)
23-
return CellValue.Error(ErrorType.Value);
24-
2521
return CellValue.Number(System.Math.Sin((double)val.Data!));
2622
}
2723

src/BlazorDatasheet.Formula.Functions/RegisterExtensions.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ public static void RegisterLogicalFunctions(this IEnvironment e)
1212
e.RegisterFunction("AND", new AndFunction());
1313
e.RegisterFunction("IF", new IfFunction());
1414
e.RegisterFunction("OR", new OrFunction());
15+
e.RegisterFunction("NOT", new NotFunction());
1516
}
1617

1718
public static void RegisterMathFunctions(this IEnvironment e)

src/BlazorDatasheet.SharedPages/Components/Pages/Home.razor

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626

2727
<div style="width: 600px; height: 400px; overflow-y: scroll;">
2828
<Datasheet
29-
UseAutoFill="_useAutoFIll"
3029
FrozenBottomCount="1"
3130
Sheet="_sheet" @ref="_datasheet"/>
3231
</div>
@@ -72,7 +71,6 @@
7271

7372
private Sheet _sheet = null!;
7473
private Datasheet? _datasheet;
75-
private bool _useAutoFIll;
7674

7775
protected override void OnInitialized()
7876
{
Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
1-
using BlazorDatasheet.Portal;
2-
using BlazorDatasheet.Services;
1+
using BlazorDatasheet.Services;
32
using Microsoft.Extensions.DependencyInjection;
4-
using Microsoft.Extensions.DependencyInjection.Extensions;
53

64
namespace BlazorDatasheet.Extensions;
75

@@ -10,7 +8,6 @@ public static class ServiceCollectionExtensions
108
public static IServiceCollection AddBlazorDatasheet(this IServiceCollection services)
119
{
1210
services.AddScoped<IMenuService, MenuService>();
13-
services.TryAddScoped<PortalService>();
1411
return services;
1512
}
1613
}

0 commit comments

Comments
 (0)