From e14d714811df1cc5d48543940042fd7481a86970 Mon Sep 17 00:00:00 2001 From: Serghei Cebotari Date: Wed, 26 Jul 2023 17:17:09 +0300 Subject: [PATCH] Move Price List Validation to separate static class --- RhSolutions.AddIn/AddIn/RhSolutionsAddIn.cs | 1 + .../Controllers/RibbonController.cs | 4 +- RhSolutions.AddIn/Services/ExcelReader.cs | 13 +++-- RhSolutions.AddIn/Services/ExcelWriterBase.cs | 12 ++--- .../Services/WorksheetValidator.cs | 50 +++++++++++++++++++ .../Tools/WorksheetExtensions.cs | 33 ------------ RhSolutions.Tests/CanDoGuess.cs | 4 ++ RhSolutions.Tests/CanFillSleeves.cs | 1 + RhSolutions.Tests/CanReadProducts.cs | 2 + RhSolutions.Tests/CanWriteProducts.cs | 10 ++++ RhSolutions.Tests/RealPricelistTest.cs | 2 + RhSolutions.Tests/WorkbookValidationTests.cs | 11 ++-- 12 files changed, 90 insertions(+), 53 deletions(-) create mode 100644 RhSolutions.AddIn/Services/WorksheetValidator.cs diff --git a/RhSolutions.AddIn/AddIn/RhSolutionsAddIn.cs b/RhSolutions.AddIn/AddIn/RhSolutionsAddIn.cs index b7634a9..f7a6c69 100644 --- a/RhSolutions.AddIn/AddIn/RhSolutionsAddIn.cs +++ b/RhSolutions.AddIn/AddIn/RhSolutionsAddIn.cs @@ -1,5 +1,6 @@ using System.Net; using ExcelDna.IntelliSense; +using Microsoft.Office.Interop.Excel; #if !NET472 using System.Runtime.Versioning; #endif diff --git a/RhSolutions.AddIn/Controllers/RibbonController.cs b/RhSolutions.AddIn/Controllers/RibbonController.cs index 6d234e8..1d8a8f7 100644 --- a/RhSolutions.AddIn/Controllers/RibbonController.cs +++ b/RhSolutions.AddIn/Controllers/RibbonController.cs @@ -119,8 +119,8 @@ public class RibbonController : ExcelRibbon else { - Worksheet worksheet = RhSolutionsAddIn.Excel.ActiveWorkbook.ActiveSheet; - _workbookIsValid = worksheet.IsValidSource(); + WorksheetValidator.Validate(RhSolutionsAddIn.Excel.ActiveSheet); + _workbookIsValid = WorksheetValidator.IsValid(); } } } diff --git a/RhSolutions.AddIn/Services/ExcelReader.cs b/RhSolutions.AddIn/Services/ExcelReader.cs index 207d043..281ebc3 100644 --- a/RhSolutions.AddIn/Services/ExcelReader.cs +++ b/RhSolutions.AddIn/Services/ExcelReader.cs @@ -85,7 +85,7 @@ public class ExcelReader : IReader, IDisposable List<(string, Dictionary)> result = new(); foreach (Worksheet worksheet in worksheets) { - if (!worksheet.IsValidSource()) + if (!WorksheetValidator.IsValid()) { continue; } @@ -93,12 +93,11 @@ public class ExcelReader : IReader, IDisposable string wbName = Path.GetFileNameWithoutExtension( worksheet.Parent.Name); - Range amountCell = worksheet.Cells.Find(headers["Amount"]); - Range headerRow = amountCell.EntireRow; - Range skuCell = headerRow.Find(headers["Sku"]), - productLineCell = headerRow.Find(headers["ProductLine"]), - nameCell = headerRow.Find(headers["Name"]), - measureCell = headerRow.Find(headers["Measure"]); + Range amountCell = WorksheetValidator.HeaderCells["Amount"], + skuCell = WorksheetValidator.HeaderCells["Sku"], + productLineCell = WorksheetValidator.HeaderCells["ProductLine"], + nameCell = WorksheetValidator.HeaderCells["Name"], + measureCell = WorksheetValidator.HeaderCells["Measure"]; var lastRowIndex = worksheet.Cells[worksheet.Rows.Count, skuCell.Column] .End[XlDirection.xlUp].Row; diff --git a/RhSolutions.AddIn/Services/ExcelWriterBase.cs b/RhSolutions.AddIn/Services/ExcelWriterBase.cs index 3ec5348..70b4823 100644 --- a/RhSolutions.AddIn/Services/ExcelWriterBase.cs +++ b/RhSolutions.AddIn/Services/ExcelWriterBase.cs @@ -31,7 +31,7 @@ namespace RhSolutions.Services public void WriteProducts(IEnumerable<(string, Dictionary)> products) { - if (!_worksheet.IsValidSource()) + if (!WorksheetValidator.IsValid()) { _application.ActiveWorkbook.Close(); throw new ArgumentException( @@ -40,11 +40,11 @@ namespace RhSolutions.Services ShowFilteredData(); - _amountCell = _worksheet.Cells.Find(_headers["Amount"]); - _skuCell = _worksheet.Cells.Find(_headers["Sku"]); - _programLineCell = _worksheet.Cells.Find(_headers["ProductLine"]); - _nameCell = _worksheet.Cells.Find(_headers["Name"]); - _oldSkuCell = _worksheet.Cells.Find(_headers["OldSku"]); + _amountCell = WorksheetValidator.HeaderCells["Amount"]; + _skuCell = WorksheetValidator.HeaderCells["Sku"]; + _programLineCell = WorksheetValidator.HeaderCells["ProductLine"]; + _nameCell = WorksheetValidator.HeaderCells["Name"]; + _oldSkuCell = WorksheetValidator.HeaderCells["OldSku"]; _progressBar = new("Заполняю строки...", products .Select(p => p.Item2) diff --git a/RhSolutions.AddIn/Services/WorksheetValidator.cs b/RhSolutions.AddIn/Services/WorksheetValidator.cs new file mode 100644 index 0000000..557bfc0 --- /dev/null +++ b/RhSolutions.AddIn/Services/WorksheetValidator.cs @@ -0,0 +1,50 @@ +#if !NET472 +using System.Runtime.Versioning; +#endif + +namespace RhSolutions.Services; + +public static class WorksheetValidator +{ + public static Dictionary HeaderCells { get; private set; } + + public static void Validate(this Worksheet worksheet) + { + Range headerRow = null; + HeaderCells = new(); + var headers = RhSolutionsAddIn.Configuration.GetPriceListHeaders(); + + foreach (var kvp in headers) + { + Range cell; + + if (headerRow == null) + { + cell = worksheet.Cells.Find(kvp.Value); + if (cell == null) + { + continue; + } + headerRow = cell.EntireRow; + } + else + { + cell = headerRow.Cells.Find(kvp.Value); + } + + if (HeaderCells.ContainsKey(kvp.Key)) + { + HeaderCells[kvp.Key] = cell; + } + else + { + HeaderCells.Add(kvp.Key, cell); + } + } + } + + public static bool IsValid() + { + return HeaderCells.Count >= RhSolutionsAddIn.Configuration.GetPriceListHeaders().Count - 1; + } +} diff --git a/RhSolutions.AddIn/Tools/WorksheetExtensions.cs b/RhSolutions.AddIn/Tools/WorksheetExtensions.cs index ee2fcdf..62fff71 100644 --- a/RhSolutions.AddIn/Tools/WorksheetExtensions.cs +++ b/RhSolutions.AddIn/Tools/WorksheetExtensions.cs @@ -9,39 +9,6 @@ namespace RhSolutions.Tools; #endif public static class WorksheetExtensions { - private static readonly Dictionary pricelistParameters = - RhSolutionsAddIn.Configuration.GetPriceListHeaders(); - - public static bool IsValidSource(this Worksheet worksheet) - { - Range headerRow; - - string[] fields = pricelistParameters.Values - .Where(v => v != "Прежний материал") - .ToArray(); - - var value = worksheet.Cells.Find(fields[0]); - - if (value == null) - { - return false; - } - else - { - headerRow = value.EntireRow; - } - - for (int i = 1; i < fields.Length; i++) - { - if (headerRow.Find(fields[i]) == null) - { - return false; - } - } - - return true; - } - public static void AddValue(this Range range, double value) { if (range.Value2 == null) diff --git a/RhSolutions.Tests/CanDoGuess.cs b/RhSolutions.Tests/CanDoGuess.cs index a43c92e..a0ac882 100644 --- a/RhSolutions.Tests/CanDoGuess.cs +++ b/RhSolutions.Tests/CanDoGuess.cs @@ -23,11 +23,13 @@ public class CanDoGuess : IDisposable public void CanWriteMultiplyRows() { Worksheet sourceSheet = Util.Workbook.Worksheets[1]; + sourceSheet.Validate(); RhSolutionsAddIn.Configuration.SetPriceListPath(Path.GetFullPath(@"..\..\..\TestWorkbooks\TargetSpecificationGuess.xlsx")); var products = _guessReader.ReadProducts(new[] { sourceSheet }); var _writer = new NewPriceWriter(Util.Application, RhSolutionsAddIn.Configuration); _writer.WriteProducts(products); Worksheet targetSheet = Util.Application.ActiveWindow.ActiveSheet; + targetSheet.Validate(); var targetProducts = _reader.ReadProducts(new[] { targetSheet }); Assert.Equal("TestSpecificationGuess", products.First().Item1); @@ -40,11 +42,13 @@ public class CanDoGuess : IDisposable public void CanWriteOneRow() { Worksheet sourceSheet = Util.Workbook.Worksheets[1]; + sourceSheet.Validate(); RhSolutionsAddIn.Configuration.SetPriceListPath(Path.GetFullPath(@"..\..\..\TestWorkbooks\TargetSpecificationGuessOneRow.xlsx")); var products = _guessReader.ReadProducts(new[] { sourceSheet }); var _writer = new NewPriceWriter(Util.Application, RhSolutionsAddIn.Configuration); _writer.WriteProducts(products); Worksheet targetSheet = Util.Application.ActiveWindow.ActiveSheet; + targetSheet.Validate(); var targetProducts = _reader.ReadProducts(new[] { targetSheet }); Assert.Equal("TestSpecificationGuessOneRow", products.First().Item1); diff --git a/RhSolutions.Tests/CanFillSleeves.cs b/RhSolutions.Tests/CanFillSleeves.cs index aedad09..aa0aa5d 100644 --- a/RhSolutions.Tests/CanFillSleeves.cs +++ b/RhSolutions.Tests/CanFillSleeves.cs @@ -24,6 +24,7 @@ public class CanFillSleeves : IDisposable [ExcelFact(Workbook = @"..\..\..\TestWorkbooks\TestSpecificationSleeves.xlsx")] public void CanCalculateSleeves() { + _worksheet.Validate(); var products = _reader.ReadProducts(new[] { _worksheet }); var sleeves = _calculator.CalculateSleeves(products.First().Item2); _writer.WriteProducts(sleeves); diff --git a/RhSolutions.Tests/CanReadProducts.cs b/RhSolutions.Tests/CanReadProducts.cs index e796da0..745c243 100644 --- a/RhSolutions.Tests/CanReadProducts.cs +++ b/RhSolutions.Tests/CanReadProducts.cs @@ -22,6 +22,7 @@ public class CanReadProducts : IDisposable public void CanReadRange() { Worksheet worksheet = _testWorkbook.Sheets[1]; + worksheet.Validate(); worksheet.Range["A1"].Value = "11600011001"; worksheet.Range["A2"].Value = "11600011001"; worksheet.Range["A3"].Value = "160002-001"; @@ -49,6 +50,7 @@ public class CanReadProducts : IDisposable public void CanReadWorkbook() { Worksheet worksheet = Util.Workbook.Worksheets[1]; + worksheet.Validate(); var result = _reader.ReadProducts(new[] { worksheet }); Assert.NotNull(result); Assert.NotEmpty(result); diff --git a/RhSolutions.Tests/CanWriteProducts.cs b/RhSolutions.Tests/CanWriteProducts.cs index 50ce662..7c72903 100644 --- a/RhSolutions.Tests/CanWriteProducts.cs +++ b/RhSolutions.Tests/CanWriteProducts.cs @@ -21,11 +21,13 @@ public class CanWriteProducts : IDisposable public void CanWriteSingle() { Worksheet sourceSheet = Util.Workbook.Worksheets[1]; + sourceSheet.Validate(); RhSolutionsAddIn.Configuration.SetPriceListPath(Path.GetFullPath(@"..\..\..\TestWorkbooks\TargetSpecification.xlsx")); var products = _reader.ReadProducts(new[] { sourceSheet }); var _writer = new NewPriceWriter(Util.Application, RhSolutionsAddIn.Configuration); _writer.WriteProducts(products); Worksheet targetSheet = Util.Application.ActiveWindow.ActiveSheet; + targetSheet.Validate(); var targetProducts = _reader.ReadProducts(new[] { targetSheet }); Assert.Equal("TestSpecification", products.First().Item1); @@ -38,11 +40,13 @@ public class CanWriteProducts : IDisposable public void CanWriteMultipleProductLines() { Worksheet sourceSheet = Util.Workbook.Worksheets[1]; + sourceSheet.Validate(); RhSolutionsAddIn.Configuration.SetPriceListPath(Path.GetFullPath(@"..\..\..\TestWorkbooks\TargetSpecificationMultipleProductLines.xlsx")); var products = _reader.ReadProducts(new[] { sourceSheet }); var _writer = new NewPriceWriter(Util.Application, RhSolutionsAddIn.Configuration); _writer.WriteProducts(products); Worksheet targetSheet = Util.Application.ActiveWindow.ActiveSheet; + targetSheet.Validate(); var targetProducts = _reader.ReadProducts(new[] { targetSheet }); Assert.Equal("TestSpecificationMultipleProductLines", products.First().Item1); @@ -55,11 +59,13 @@ public class CanWriteProducts : IDisposable public void CanWriteNotFound() { Worksheet sourceSheet = Util.Workbook.Worksheets[1]; + sourceSheet.Validate(); RhSolutionsAddIn.Configuration.SetPriceListPath(Path.GetFullPath(@"..\..\..\TestWorkbooks\TargetSpecificationNotFound.xlsx")); var products = _reader.ReadProducts(new[] { sourceSheet }); var _writer = new NewPriceWriter(Util.Application, RhSolutionsAddIn.Configuration); _writer.WriteProducts(products); Worksheet targetSheet = Util.Application.ActiveWindow.ActiveSheet; + targetSheet.Validate(); Assert.Equal("???", targetSheet.Range["B4"].Value2); Assert.Contains("Молот Тора", targetSheet.Range["C4"].Value2); @@ -70,11 +76,13 @@ public class CanWriteProducts : IDisposable public void CanWriteReplaced() { Worksheet sourceSheet = Util.Workbook.Worksheets[1]; + sourceSheet.Validate(); RhSolutionsAddIn.Configuration.SetPriceListPath(Path.GetFullPath(@"..\..\..\TestWorkbooks\TargetSpecificationReplaced.xlsx")); var products = _reader.ReadProducts(new[] { sourceSheet }); var _writer = new NewPriceWriter(Util.Application, RhSolutionsAddIn.Configuration); _writer.WriteProducts(products); Worksheet targetSheet = Util.Application.ActiveWindow.ActiveSheet; + targetSheet.Validate(); var targetProducts = _reader.ReadProducts(new[] { targetSheet }); Assert.Equal("TestSpecificationReplaced", products.First().Item1); @@ -92,11 +100,13 @@ public class CanWriteProducts : IDisposable public void CanWriteNewVariant() { Worksheet sourceSheet = Util.Workbook.Worksheets[1]; + sourceSheet.Validate(); RhSolutionsAddIn.Configuration.SetPriceListPath(Path.GetFullPath(@"..\..\..\TestWorkbooks\TargetSpecificationNewVariant.xlsx")); var products = _reader.ReadProducts(new[] { sourceSheet }); var _writer = new NewPriceWriter(Util.Application, RhSolutionsAddIn.Configuration); _writer.WriteProducts(products); Worksheet targetSheet = Util.Application.ActiveWindow.ActiveSheet; + targetSheet.Validate(); var targetProducts = _reader.ReadProducts(new[] { targetSheet }); Assert.Equal("TestSpecificationNewVariant", products.First().Item1); diff --git a/RhSolutions.Tests/RealPricelistTest.cs b/RhSolutions.Tests/RealPricelistTest.cs index 9ff3a25..cb99908 100644 --- a/RhSolutions.Tests/RealPricelistTest.cs +++ b/RhSolutions.Tests/RealPricelistTest.cs @@ -21,11 +21,13 @@ public class RealPricelistTest : IDisposable public void CanWrite() { Worksheet sourceSheet = Util.Workbook.Worksheets[1]; + sourceSheet.Validate(); RhSolutionsAddIn.Configuration.SetPriceListPath(Path.GetFullPath(@"..\..\..\TestWorkbooks\RealTargetSpecification.xlsx")); var products = _reader.ReadProducts(new[] { sourceSheet }); var _writer = new NewPriceWriter(Util.Application, RhSolutionsAddIn.Configuration); _writer.WriteProducts(products); Worksheet targetSheet = Util.Application.ActiveWindow.ActiveSheet; + targetSheet.Validate(); targetSheet.Range["A1"].Formula = "=SUM(H:H)"; Assert.Equal("RealTestSpecification", products.First().Item1); diff --git a/RhSolutions.Tests/WorkbookValidationTests.cs b/RhSolutions.Tests/WorkbookValidationTests.cs index f5b338a..5a6700a 100644 --- a/RhSolutions.Tests/WorkbookValidationTests.cs +++ b/RhSolutions.Tests/WorkbookValidationTests.cs @@ -1,5 +1,4 @@ using RhSolutions.AddIn; -using RhSolutions.Tools; namespace RhSolutions.Tests; @@ -18,15 +17,17 @@ public class WorkbookValidationTests : IDisposable [ExcelFact(Workbook = @"..\..\..\TestWorkbooks\EmptyTestTable.xlsx")] public void WorksheetIsCorrect() { - Worksheet worksheet = Util.Workbook.Sheets[1]; - Assert.True(worksheet.IsValidSource()); + Worksheet ws = Util.Workbook.Sheets[1]; + ws.Validate(); + Assert.True(WorksheetValidator.IsValid()); } [ExcelFact(Workbook = @"..\..\..\TestWorkbooks\EmptyWorkbook.xlsx")] public void EmptyWorkbookIsNotCorrect() { - Worksheet worksheet = Util.Workbook.Sheets[1]; - Assert.False(worksheet.IsValidSource()); + Worksheet ws = Util.Workbook.Sheets[1]; + ws.Validate(); + Assert.False(WorksheetValidator.IsValid()); } public void Dispose()