From eda66858e0411a5b43fab52f23f69d9fba3eeb00 Mon Sep 17 00:00:00 2001 From: Alexander Dobrynin Date: Wed, 18 Jan 2023 22:45:02 +0400 Subject: [PATCH] don't read cell's value to change it's type: set cell type and value at the same time --- Benchmark/Benchmark.csproj | 1 + .../FakeCell.cs | 11 ++++++++-- .../FakeTable.cs | 2 +- .../FakeDocumentPrimitivesTests.cs | 6 +++--- .../ObjectFakePrintingTests.cs | 4 ++-- .../ExcelDocumentPrimitives/ICell.cs | 4 ++-- .../Implementations/ExcelCell.cs | 21 ++++++++----------- .../TableBuilder/TableBuilder.cs | 3 +-- 8 files changed, 28 insertions(+), 24 deletions(-) diff --git a/Benchmark/Benchmark.csproj b/Benchmark/Benchmark.csproj index abcd895..e650df9 100644 --- a/Benchmark/Benchmark.csproj +++ b/Benchmark/Benchmark.csproj @@ -3,6 +3,7 @@ Exe net472;net6.0 + false diff --git a/Excel.TemplateEngine.Tests/ObjectPrintingTests/FakeDocumentPrimitivesImplementation/FakeCell.cs b/Excel.TemplateEngine.Tests/ObjectPrintingTests/FakeDocumentPrimitivesImplementation/FakeCell.cs index 8f2223e..64c49fa 100644 --- a/Excel.TemplateEngine.Tests/ObjectPrintingTests/FakeDocumentPrimitivesImplementation/FakeCell.cs +++ b/Excel.TemplateEngine.Tests/ObjectPrintingTests/FakeDocumentPrimitivesImplementation/FakeCell.cs @@ -10,8 +10,15 @@ public FakeCell(ICellPosition position) CellPosition = position; } - public string StringValue { get; set; } - public CellType CellType { get; set; } + public string StringValue { get; private set; } + public CellType CellType { get; private set; } + + public void SetValue(string stringValue, CellType cellType) + { + StringValue = stringValue; + CellType = cellType; + } + public ICellPosition CellPosition { get; } public void CopyStyle(ICell templateCell) diff --git a/Excel.TemplateEngine.Tests/ObjectPrintingTests/FakeDocumentPrimitivesImplementation/FakeTable.cs b/Excel.TemplateEngine.Tests/ObjectPrintingTests/FakeDocumentPrimitivesImplementation/FakeTable.cs index fd83f6e..3b05289 100644 --- a/Excel.TemplateEngine.Tests/ObjectPrintingTests/FakeDocumentPrimitivesImplementation/FakeTable.cs +++ b/Excel.TemplateEngine.Tests/ObjectPrintingTests/FakeDocumentPrimitivesImplementation/FakeTable.cs @@ -116,7 +116,7 @@ public static FakeTable GenerateFromStringArray(string[][] template) for (var x = 0; x < width; ++x) { var cell = table.InsertCell(new CellPosition(y + 1, x + 1)); - cell.StringValue = template[y][x]; + cell.SetValue(template[y][x], CellType.String); } } return table; diff --git a/Excel.TemplateEngine.Tests/ObjectPrintingTests/FakeDocumentPrimitivesTests.cs b/Excel.TemplateEngine.Tests/ObjectPrintingTests/FakeDocumentPrimitivesTests.cs index caff868..a356bc8 100644 --- a/Excel.TemplateEngine.Tests/ObjectPrintingTests/FakeDocumentPrimitivesTests.cs +++ b/Excel.TemplateEngine.Tests/ObjectPrintingTests/FakeDocumentPrimitivesTests.cs @@ -25,7 +25,7 @@ public void FakeTableCellManipulationTest() cell.Should().NotBeNull(); const string testValue = "Test Value"; - cell.StringValue = testValue; + cell.SetValue(testValue); cell = table.GetCell(position); @@ -89,7 +89,7 @@ public void FakeTableCellSearchTest() for (var i = 0; i < positions.Count(); ++i) { var cell = table.InsertCell(positions[i]); - cell.StringValue = stringValues[i]; + cell.SetValue(stringValues[i]); } var cells = table.SearchCellByText("Test"); @@ -124,7 +124,7 @@ public void FakeTablePartExtractionTest() for (var i = 0; i < positions.Count(); ++i) { var cell = table.InsertCell(positions[i]); - cell.StringValue = stringValues[i]; + cell.SetValue(stringValues[i]); } var tablePart = table.GetTablePart(new Rectangle(new CellPosition(9, 9), new CellPosition(40, 20))); diff --git a/Excel.TemplateEngine.Tests/ObjectPrintingTests/ObjectFakePrintingTests.cs b/Excel.TemplateEngine.Tests/ObjectPrintingTests/ObjectFakePrintingTests.cs index 573fbc3..b2cc02c 100644 --- a/Excel.TemplateEngine.Tests/ObjectPrintingTests/ObjectFakePrintingTests.cs +++ b/Excel.TemplateEngine.Tests/ObjectPrintingTests/ObjectFakePrintingTests.cs @@ -24,7 +24,7 @@ public void StringValuePrintingTest() { var template = new FakeTable(1, 1); var cell = template.InsertCell(new CellPosition(1, 1)); - cell.StringValue = "Template:RootTemplate:A1:A1"; + cell.SetValue("Template:RootTemplate:A1:A1"); var templateEngine = new TemplateEngine(template, logger); @@ -48,7 +48,7 @@ public void SimpleTestWithArray() var template = new FakeTable(1, 1); var cell = template.InsertCell(new CellPosition(1, 1)); - cell.StringValue = "Template:RootTemplate:A1:A1"; + cell.SetValue("Template:RootTemplate:A1:A1"); var templateEngine = new TemplateEngine(template, logger); diff --git a/Excel.TemplateEngine/ObjectPrinting/ExcelDocumentPrimitives/ICell.cs b/Excel.TemplateEngine/ObjectPrinting/ExcelDocumentPrimitives/ICell.cs index e436061..f354232 100644 --- a/Excel.TemplateEngine/ObjectPrinting/ExcelDocumentPrimitives/ICell.cs +++ b/Excel.TemplateEngine/ObjectPrinting/ExcelDocumentPrimitives/ICell.cs @@ -5,8 +5,8 @@ namespace SkbKontur.Excel.TemplateEngine.ObjectPrinting.ExcelDocumentPrimitives public interface ICell { void CopyStyle(ICell templateCell); - string StringValue { get; set; } - CellType CellType { set; } + string StringValue { get; } + void SetValue(string stringValue, CellType cellType = CellType.String); ICellPosition CellPosition { get; } } } \ No newline at end of file diff --git a/Excel.TemplateEngine/ObjectPrinting/ExcelDocumentPrimitives/Implementations/ExcelCell.cs b/Excel.TemplateEngine/ObjectPrinting/ExcelDocumentPrimitives/Implementations/ExcelCell.cs index e3f0f5a..74cb116 100644 --- a/Excel.TemplateEngine/ObjectPrinting/ExcelDocumentPrimitives/Implementations/ExcelCell.cs +++ b/Excel.TemplateEngine/ObjectPrinting/ExcelDocumentPrimitives/Implementations/ExcelCell.cs @@ -11,18 +11,7 @@ public ExcelCell(IExcelCell excelCell) internalCell = excelCell; } - public string StringValue { get => internalCell.GetStringValue(); set => internalCell.SetStringValue(value); } - - public CellType CellType - { - set - { - if (value == CellType.String) - internalCell.SetStringValue(StringValue); - else - internalCell.SetNumericValue(StringValue); - } - } + public string StringValue => internalCell.GetStringValue(); public void CopyStyle(ICell templateCell) { @@ -30,6 +19,14 @@ public void CopyStyle(ICell templateCell) internalCell.SetStyle(excelCell.internalCell.GetStyle()); } + public void SetValue(string stringValue, CellType cellType) + { + if (cellType == CellType.String) + internalCell.SetStringValue(stringValue); + else + internalCell.SetNumericValue(stringValue); + } + public ICellPosition CellPosition => new CellPosition(internalCell.GetCellIndex()); private readonly IExcelCell internalCell; diff --git a/Excel.TemplateEngine/ObjectPrinting/TableBuilder/TableBuilder.cs b/Excel.TemplateEngine/ObjectPrinting/TableBuilder/TableBuilder.cs index 46014ea..15c8595 100644 --- a/Excel.TemplateEngine/ObjectPrinting/TableBuilder/TableBuilder.cs +++ b/Excel.TemplateEngine/ObjectPrinting/TableBuilder/TableBuilder.cs @@ -151,8 +151,7 @@ public ITableBuilder CopyCommentsFrom([NotNull] ITable template) private ITableBuilder RenderAtomicValue(string value, CellType cellType) { var cell = target.GetCell(navigator.CurrentState.Cursor) ?? target.InsertCell(navigator.CurrentState.Cursor); - cell.StringValue = value; - cell.CellType = cellType; + cell.SetValue(value, cellType); return this; }