Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor DataCell for ReadOnlyMemory<char> support #48

Merged
merged 10 commits into from
Apr 20, 2024
18 changes: 11 additions & 7 deletions SpreadCheetah.Benchmark/Benchmarks/MixedDataTypeCells.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,32 +18,35 @@ public class MixedDataTypeCells
[GlobalSetup]
public void GlobalSetup()
{
var random = new Random(42);
var baseDateTime = new DateTime(2000, 1, 1, 0, 0, 0, DateTimeKind.Utc);

_rows = Enumerable.Range(0, NumberOfRows)
.Select(row =>
{
var even = row % 2 == 0;
return new RowItem(
row,
"Ola-" + row,
row + "-Nordmann",
1950 + row / 1000,
random.NextBoolean() ? row + "-Nordmann" : null,
random.NextBoolean() ? null : 1950 + row / 1000,
5.67 + row % 10,
even,
even ? "Norway" : "Sweden",
!even,
0.991f + row / 10000.0,
even ? -23 : 23);
random.NextBoolean() ? 0.991f + row / 10000.0 : null,
even ? -23 : 23,
random.NextBoolean() ? baseDateTime.AddSeconds(random.Next()) : null);
}).ToArray();
}

[Benchmark]
public async Task SpreadCheetah()
{
var options = new SpreadCheetahOptions { DefaultDateTimeFormat = null };
await using var spreadsheet = await Spreadsheet.CreateNewAsync(Stream.Null, options);
await using var spreadsheet = await Spreadsheet.CreateNewAsync(Stream.Null);
await spreadsheet.StartWorksheetAsync("Book1");

var cells = new DataCell[10];
var cells = new DataCell[11];
var rows = _rows;

for (var row = 0; row < rows.Length; ++row)
Expand All @@ -59,6 +62,7 @@ public async Task SpreadCheetah()
cells[7] = new DataCell(rowItem.H);
cells[8] = new DataCell(rowItem.I);
cells[9] = new DataCell(rowItem.J);
cells[10] = new DataCell(rowItem.K);

await spreadsheet.AddRowAsync(cells);
}
Expand Down
3 changes: 2 additions & 1 deletion SpreadCheetah.Benchmark/Benchmarks/StringCells.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using OfficeOpenXml;
using System.IO.Packaging;
using OpenXmlCell = DocumentFormat.OpenXml.Spreadsheet.Cell;
using OpenXmlCellValue = DocumentFormat.OpenXml.Spreadsheet.CellValues;

namespace SpreadCheetah.Benchmark.Benchmarks;

Expand Down Expand Up @@ -185,7 +186,7 @@ public void OpenXmlDom()
{
var inlineString = new InlineString();
inlineString.AppendChild(new Text(rowValues[col]));
var cell = new OpenXmlCell { DataType = CellValues.InlineString };
var cell = new OpenXmlCell { DataType = OpenXmlCellValue.InlineString };
cell.AppendChild(inlineString);
cells[col] = cell;
}
Expand Down
2 changes: 1 addition & 1 deletion SpreadCheetah.Benchmark/Helpers/RowItem.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
namespace SpreadCheetah.Benchmark.Helpers;

public record RowItem(int A, string B, string C, int D, double E, bool F, string G, bool H, double I, int J);
public record RowItem(int A, string B, string? C, int? D, double E, bool F, string G, bool H, double? I, int J, DateTime? K);
6 changes: 3 additions & 3 deletions SpreadCheetah.Test/Helpers/OpenXmlCellExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
using DocumentFormat.OpenXml.Spreadsheet;
using OpenXmlCellValue = DocumentFormat.OpenXml.Spreadsheet.CellValues;

namespace SpreadCheetah.Test.Helpers;

internal static class OpenXmlCellExtensions
{
public static CellValues GetDataType(this DocumentFormat.OpenXml.Spreadsheet.Cell cell)
public static OpenXmlCellValue GetDataType(this DocumentFormat.OpenXml.Spreadsheet.Cell cell)
{
// The "t" attribute on cells is optional and defaults to "n" (number)
return cell.DataType?.Value ?? CellValues.Number;
return cell.DataType?.Value ?? OpenXmlCellValue.Number;
}
}
22 changes: 11 additions & 11 deletions SpreadCheetah.Test/Tests/SpreadsheetRowTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
using SpreadCheetah.Test.Helpers;
using SpreadCheetah.Worksheets;
using System.Globalization;
using Xunit;
using CellType = SpreadCheetah.Test.Helpers.CellType;
using OpenXmlCell = DocumentFormat.OpenXml.Spreadsheet.Cell;
using OpenXmlCellValue = DocumentFormat.OpenXml.Spreadsheet.CellValues;

namespace SpreadCheetah.Test.Tests;

Expand Down Expand Up @@ -143,7 +143,7 @@ public async Task Spreadsheet_AddRow_CellWithStringValue(string? value, CellType
using var actual = SpreadsheetDocument.Open(stream, true);
var sheetPart = actual.WorkbookPart!.WorksheetParts.Single();
var actualCell = sheetPart.Worksheet.Descendants<OpenXmlCell>().Single();
CellValues? expectedDataType = value is null ? null : CellValues.InlineString;
OpenXmlCellValue? expectedDataType = value is null ? null : OpenXmlCellValue.InlineString;
Assert.Equal(expectedDataType, actualCell.DataType?.Value);
Assert.Equal(value ?? string.Empty, actualCell.InnerText);
}
Expand All @@ -168,7 +168,7 @@ public async Task Spreadsheet_AddRow_CellWithInvalidControlCharacterStringValue(
using var actual = SpreadsheetDocument.Open(stream, true);
var sheetPart = actual.WorkbookPart!.WorksheetParts.Single();
var actualCell = sheetPart.Worksheet.Descendants<OpenXmlCell>().Single();
Assert.Equal(CellValues.InlineString, actualCell.DataType?.Value);
Assert.Equal(OpenXmlCellValue.InlineString, actualCell.DataType?.Value);
Assert.Equal("WithControlCharacters", actualCell.InnerText);
}

Expand Down Expand Up @@ -203,7 +203,7 @@ public async Task Spreadsheet_AddRow_CellWithVeryLongStringValue(int length, Cel
using var actual = SpreadsheetDocument.Open(stream, true);
var sheetPart = actual.WorkbookPart!.WorksheetParts.Single();
var actualCell = sheetPart.Worksheet.Descendants<OpenXmlCell>().Single();
Assert.Equal(CellValues.InlineString, actualCell.DataType?.Value);
Assert.Equal(OpenXmlCellValue.InlineString, actualCell.DataType?.Value);
Assert.Equal(value, actualCell.InnerText);
}

Expand Down Expand Up @@ -236,7 +236,7 @@ public async Task Spreadsheet_AddRow_CellWithIntegerValue(int? value, CellType t
using var actual = SpreadsheetDocument.Open(stream, true);
var sheetPart = actual.WorkbookPart!.WorksheetParts.Single();
var actualCell = sheetPart.Worksheet.Descendants<OpenXmlCell>().Single();
Assert.Equal(CellValues.Number, actualCell.GetDataType());
Assert.Equal(OpenXmlCellValue.Number, actualCell.GetDataType());
Assert.Equal(value?.ToString() ?? string.Empty, actualCell.InnerText);
}

Expand Down Expand Up @@ -275,7 +275,7 @@ public async Task Spreadsheet_AddRow_CellWithLongValue(long? initialValue, strin
using var actual = SpreadsheetDocument.Open(stream, true);
var sheetPart = actual.WorkbookPart!.WorksheetParts.Single();
var actualCell = sheetPart.Worksheet.Descendants<OpenXmlCell>().Single();
Assert.Equal(CellValues.Number, actualCell.GetDataType());
Assert.Equal(OpenXmlCellValue.Number, actualCell.GetDataType());
Assert.Equal(expectedValue, actualCell.InnerText);
}

Expand Down Expand Up @@ -311,7 +311,7 @@ public async Task Spreadsheet_AddRow_CellWithFloatValue(float? initialValue, str
using var actual = SpreadsheetDocument.Open(stream, true);
var sheetPart = actual.WorkbookPart!.WorksheetParts.Single();
var actualCell = sheetPart.Worksheet.Descendants<OpenXmlCell>().Single();
Assert.Equal(CellValues.Number, actualCell.GetDataType());
Assert.Equal(OpenXmlCellValue.Number, actualCell.GetDataType());
Assert.Equal(expectedValue, actualCell.InnerText);
}

Expand Down Expand Up @@ -352,7 +352,7 @@ public async Task Spreadsheet_AddRow_CellWithDoubleValue(double? initialValue, s
using var actual = SpreadsheetDocument.Open(stream, true);
var sheetPart = actual.WorkbookPart!.WorksheetParts.Single();
var actualCell = sheetPart.Worksheet.Descendants<OpenXmlCell>().Single();
Assert.Equal(CellValues.Number, actualCell.GetDataType());
Assert.Equal(OpenXmlCellValue.Number, actualCell.GetDataType());
Assert.Equal(expectedValue, actualCell.InnerText);
}

Expand Down Expand Up @@ -395,7 +395,7 @@ public async Task Spreadsheet_AddRow_CellWithDecimalValue(string? initialValue,
using var actual = SpreadsheetDocument.Open(stream, true);
var sheetPart = actual.WorkbookPart!.WorksheetParts.Single();
var actualCell = sheetPart.Worksheet.Descendants<OpenXmlCell>().Single();
Assert.Equal(CellValues.Number, actualCell.GetDataType());
Assert.Equal(OpenXmlCellValue.Number, actualCell.GetDataType());
Assert.Equal(expectedValue, actualCell.InnerText);
}

Expand Down Expand Up @@ -498,7 +498,7 @@ public async Task Spreadsheet_AddRow_CellWithBooleanValue(bool? initialValue, st
using var actual = SpreadsheetDocument.Open(stream, true);
var sheetPart = actual.WorkbookPart!.WorksheetParts.Single();
var actualCell = sheetPart.Worksheet.Descendants<OpenXmlCell>().Single();
var expectedDataType = initialValue is null ? CellValues.Number : CellValues.Boolean;
var expectedDataType = initialValue is null ? OpenXmlCellValue.Number : OpenXmlCellValue.Boolean;
Assert.Equal(expectedDataType, actualCell.GetDataType());
Assert.Equal(expectedValue, actualCell.InnerText);
}
Expand Down Expand Up @@ -598,7 +598,7 @@ public async Task Spreadsheet_AddRow_MultipleRowsWithCharactersToEscape(CellType
}

await spreadsheet.FinishAsync();


// Assert
SpreadsheetAssert.Valid(stream);
Expand Down
35 changes: 0 additions & 35 deletions SpreadCheetah/CellValue.cs

This file was deleted.

6 changes: 3 additions & 3 deletions SpreadCheetah/CellValueWriters/CellValueWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@ internal abstract class CellValueWriter
private static readonly CellValueWriter[] Writers =
[
new NullValueWriter(),
new StringCellValueWriter(),
new IntegerCellValueWriter(),
new FloatCellValueWriter(),
new DoubleCellValueWriter(),
new DateTimeCellValueWriter(),
new NullDateTimeCellValueWriter(),
new TrueBooleanCellValueWriter(),
new DateTimeCellValueWriter(),
new FalseBooleanCellValueWriter(),
new StringCellValueWriter()
new TrueBooleanCellValueWriter()
];

[MethodImpl(MethodImplOptions.AggressiveInlining)]
Expand Down
16 changes: 8 additions & 8 deletions SpreadCheetah/CellValueWriters/CellWriterType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ namespace SpreadCheetah.CellValueWriters;
internal enum CellWriterType : byte
{
Null,
Integer,
Float,
Double,
DateTime,
NullDateTime,
TrueBoolean,
FalseBoolean,
String
String = 1,
Integer = 2,
Float = 3,
Double = 4,
NullDateTime = 5,
DateTime = 6,
FalseBoolean = 7,
TrueBoolean = 8
}
18 changes: 9 additions & 9 deletions SpreadCheetah/CellValueWriters/Number/DoubleCellValueWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@ internal sealed class DoubleCellValueWriter : NumberCellValueWriter
{
public override bool TryWriteCell(in DataCell cell, DefaultStyling? defaultStyling, SpreadsheetBuffer buffer)
{
return buffer.TryWrite($"{BeginDataCell}{cell.NumberValue.DoubleValue}{EndDefaultCell}");
return buffer.TryWrite($"{BeginDataCell}{cell.Value.StringOrPrimitive.PrimitiveValue.DoubleValue}{EndDefaultCell}");
}

public override bool TryWriteCell(in DataCell cell, StyleId styleId, SpreadsheetBuffer buffer)
{
return buffer.TryWrite(
$"{StyledCellHelper.BeginStyledNumberCell}{styleId.Id}{EndQuoteBeginValue}" +
$"{cell.NumberValue.DoubleValue}" +
$"{cell.Value.StringOrPrimitive.PrimitiveValue.DoubleValue}" +
$"{EndDefaultCell}");
}

Expand All @@ -28,31 +28,31 @@ public override bool TryWriteCell(string formulaText, in DataCell cachedValue, S
$"{StyledCellHelper.BeginStyledNumberCell}{style.Id}{FormulaCellHelper.EndQuoteBeginFormula}" +
$"{formulaText}" +
$"{FormulaCellHelper.EndFormulaBeginCachedValue}" +
$"{cachedValue.NumberValue.DoubleValue}" +
$"{cachedValue.Value.StringOrPrimitive.PrimitiveValue.DoubleValue}" +
$"{FormulaCellHelper.EndCachedValueEndCell}");
}

return buffer.TryWrite(
$"{FormulaCellHelper.BeginNumberFormulaCell}" +
$"{formulaText}" +
$"{FormulaCellHelper.EndFormulaBeginCachedValue}" +
$"{cachedValue.NumberValue.DoubleValue}" +
$"{cachedValue.Value.StringOrPrimitive.PrimitiveValue.DoubleValue}" +
$"{FormulaCellHelper.EndCachedValueEndCell}");
}

public override bool TryWriteCellWithReference(in DataCell cell, DefaultStyling? defaultStyling, CellWriterState state)
{
return state.Buffer.TryWrite(
$"{state}{EndQuoteBeginValue}" +
$"{cell.NumberValue.DoubleValue}" +
$"{cell.Value.StringOrPrimitive.PrimitiveValue.DoubleValue}" +
$"{EndDefaultCell}");
}

public override bool TryWriteCellWithReference(in DataCell cell, StyleId styleId, CellWriterState state)
{
return state.Buffer.TryWrite(
$"{state}{StyledCellHelper.EndReferenceBeginStyleId}{styleId.Id}{EndQuoteBeginValue}" +
$"{cell.NumberValue.DoubleValue}" +
$"{cell.Value.StringOrPrimitive.PrimitiveValue.DoubleValue}" +
$"{EndDefaultCell}");
}

Expand All @@ -64,20 +64,20 @@ public override bool TryWriteCellWithReference(string formulaText, in DataCell c
$"{state}{StyledCellHelper.EndReferenceBeginStyleId}{style.Id}{FormulaCellHelper.EndQuoteBeginFormula}" +
$"{formulaText}" +
$"{FormulaCellHelper.EndFormulaBeginCachedValue}" +
$"{cachedValue.NumberValue.DoubleValue}" +
$"{cachedValue.Value.StringOrPrimitive.PrimitiveValue.DoubleValue}" +
$"{FormulaCellHelper.EndCachedValueEndCell}");
}

return state.Buffer.TryWrite(
$"{state}{FormulaCellHelper.EndQuoteBeginFormula}" +
$"{formulaText}" +
$"{FormulaCellHelper.EndFormulaBeginCachedValue}" +
$"{cachedValue.NumberValue.DoubleValue}" +
$"{cachedValue.Value.StringOrPrimitive.PrimitiveValue.DoubleValue}" +
$"{FormulaCellHelper.EndCachedValueEndCell}");
}

public override bool WriteValuePieceByPiece(in DataCell cell, SpreadsheetBuffer buffer, ref int valueIndex)
{
return buffer.TryWrite($"{cell.NumberValue.DoubleValue}");
return buffer.TryWrite($"{cell.Value.StringOrPrimitive.PrimitiveValue.DoubleValue}");
}
}
Loading
Loading