Compare commits

...

19 Commits

Author SHA1 Message Date
Sergey Chebotar
5fbfd3732f Version 1.8.5.0 2023-06-21 07:28:09 +03:00
Sergey Chebotar
9f4823378f Add Fill Sleeves Tests 2023-06-21 07:24:47 +03:00
Sergey Chebotar
6a5038246f Fix interface name 2023-06-21 07:11:31 +03:00
Sergey Chebotar
c17771a5d3 Fill not found sleeves with 0 2023-06-21 06:53:53 +03:00
Sergey Chebotar
101c1d4397 Add 'Add sleeves' button enabling 2023-06-21 06:39:15 +03:00
Sergey Chebotar
2224a98d32 Edit Fill Sleeves button 2023-06-21 06:35:28 +03:00
Sergey Chebotar
0da818da3e Implement sleeves calculator 2023-06-21 06:33:12 +03:00
Sergey Chebotar
5153e951e4 Edit sleeves tool 2023-06-20 11:52:42 +03:00
Sergey Chebotar
b9cdad649a Tools refactoring 2023-06-20 11:51:52 +03:00
Sergey Chebotar
2c44c5d2df Add Sleeves Calculator service 2023-06-20 11:50:11 +03:00
Sergey Chebotar
a9d7ac710e Implement CurrentPrice Writer service 2023-06-20 10:01:02 +03:00
Sergey Chebotar
a73cc46449 Add CurrentPrice Writer to Writer Factory 2023-06-20 09:58:39 +03:00
Sergey Chebotar
63a962f495 Implement fill sleeves tool 2023-06-20 09:51:44 +03:00
Sergey Chebotar
8c134644ba Fix filling filtered table 2023-06-20 09:51:25 +03:00
Sergey Chebotar
f570dd9a2d Add Fill Sleeves button 2023-06-20 09:50:24 +03:00
Sergey Chebotar
dc92388748 Fix guess tool button id 2023-06-20 09:50:03 +03:00
Sergey Chebotar
98bb135b62 Add CurrrentPriceWriter service 2023-06-20 09:46:52 +03:00
Sergey Chebotar
f89a3eb765 ExcelWriterBase Refactoring 2023-06-20 08:14:01 +03:00
Sergey Chebotar
7b8cab3e9e Extract Excel Write Base 2023-06-20 07:25:44 +03:00
27 changed files with 497 additions and 313 deletions

View File

@ -25,13 +25,16 @@ public sealed class RhSolutionsAddIn : IExcelAddIn
.AddSingleton<IAddInConfiguration, AddInConfiguration>()
.AddSingleton<IDatabaseClient, DatabaseClient>()
.AddSingleton<ICurrencyClient, CurrencyClient>()
.AddSingleton<ISleevesCalculator, SleevesCalculator>()
.AddTransient<IFileDialog, FileDialog>();
Services.AddSingleton<WriterFactory>();
Services.AddTransient<ExcelWriter>()
.AddTransient<IWriter, ExcelWriter>(s => s.GetService<ExcelWriter>());
Services.AddTransient<NewPriceWriter>()
.AddTransient<IWriter, NewPriceWriter>(s => s.GetService<NewPriceWriter>());
Services.AddTransient<DxfWriter>()
.AddTransient<IWriter, DxfWriter>(s => s.GetService<DxfWriter>());
Services.AddTransient<CurrentPriceWriter>()
.AddTransient<IWriter, CurrentPriceWriter>(s => s.GetService<CurrentPriceWriter>());
Services.AddSingleton<ReaderFactory>();
Services.AddTransient<ExcelReader>()

View File

@ -28,7 +28,8 @@ public class RibbonController : ExcelRibbon
<button id='export' getEnabled='GetExportEnabled' label='Экспорт в новый файл' size='normal' imageMso='PivotExportToExcel' onAction='OnToolPressed'/>
<button id='convert' getEnabled='GetConvertEnabled' label='Актуализировать' size='normal' imageMso='FileUpdate' onAction='OnToolPressed'/>
<button id='merge' label='Объединить' size='normal' imageMso='Copy' onAction='OnToolPressed'/>
<button id='Guessexport' getEnabled='GetGuessEnabled' label='Найти и экспортировать' size='normal' imageMso='ControlWizards' onAction='OnToolPressed'/>
<button id='guess' getEnabled='GetGuessEnabled' label='Найти и экспортировать' size='normal' imageMso='ControlWizards' onAction='OnToolPressed'/>
<button id='fillsleeves' getEnabled='GetSleevesEnabled' label='Подобрать гильзы' size='normal' imageMso='CreateQueryFromWizard' onAction='OnToolPressed'/>
<button id='dxfexport' getEnabled='GetDxfEnabled' label='Экспортировать в DXF' size='normal' imageMso='ExportExcel' onAction='OnToolPressed'/>
</group>
<group id='rausettings' getLabel='GetVersionLabel'>
@ -84,6 +85,7 @@ public class RibbonController : ExcelRibbon
public bool GetConvertEnabled(IRibbonControl control) => _workbookIsValid;
public bool GetDxfEnabled(IRibbonControl control) => _workbookIsValid;
public bool GetSleevesEnabled(IRibbonControl control) => _workbookIsValid;
public bool GetGuessEnabled(IRibbonControl control) => RhSolutionsAddIn.Excel.ActiveWorkbook != null && !_workbookIsValid;
public bool GetExportEnabled(IRibbonControl control)

View File

@ -31,5 +31,5 @@ using System.Runtime.InteropServices;
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.8.4.0")]
[assembly: AssemblyFileVersion("1.8.4.0")]
[assembly: AssemblyVersion("1.8.5.0")]
[assembly: AssemblyFileVersion("1.8.5.0")]

View File

@ -0,0 +1,19 @@
#if !NET472
using System.Runtime.Versioning;
using RhSolutions.Tools;
#endif
namespace RhSolutions.Services;
public class CurrentPriceWriter : ExcelWriterBase, IWriter, IDisposable
{
public CurrentPriceWriter(Application application, IAddInConfiguration configuration)
{
_application = application;
_resultBar = new();
_headers = configuration.GetPriceListHeaders();
_worksheet = _application.ActiveSheet;
_appendValues = false;
}
}

View File

@ -1,275 +0,0 @@
#if !NET472
using System.Runtime.Versioning;
using RhSolutions.Tools;
#endif
using System.Text.RegularExpressions;
namespace RhSolutions.Services;
#if !NET472
[SupportedOSPlatform("windows")]
#endif
public class ExcelWriter : IWriter, IDisposable
{
private readonly Application _application;
private Worksheet _worksheet;
private readonly ResultBar _resultBar;
private readonly Dictionary<string, string> _headers;
private readonly string _pricelistPath;
private ProgressBar _progressBar;
private Range _amountCell,
_skuCell,
_programLineCell,
_nameCell,
_oldSkuCell;
public ExcelWriter(Application application, IAddInConfiguration configuration)
{
_application = application;
_pricelistPath = configuration.GetPriceListPath();
_resultBar = new();
_headers = configuration.GetPriceListHeaders();
}
public void WriteProducts(Dictionary<Product, double> products)
{
WriteProducts(new[] { (string.Empty, products) });
}
public void WriteProducts(IEnumerable<(string, Dictionary<Product, double>)> products)
{
_worksheet = OpenNewPrice();
if (!_worksheet.IsValidSource())
{
_application.ActiveWorkbook.Close();
throw new ArgumentException(
$"Целевой файл {_application.ActiveWorkbook.Name} не является прайс-листом.");
}
_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"]);
_progressBar = new("Заполняю строки...", products
.Select(p => p.Item2)
.Sum(set => set.Count));
if (products.Count() == 1)
{
foreach (var kvp in products.First().Item2)
{
FillPositionAmountToColumns(kvp, _amountCell.Column);
_progressBar.Update();
}
FilterByAmount();
_resultBar.Update();
}
else
{
foreach (var product in products)
{
_worksheet.Columns[_amountCell.Column]
.EntireColumn
.Insert(XlInsertShiftDirection.xlShiftToRight, XlInsertFormatOrigin.xlFormatFromRightOrBelow);
Range newColumnHeader = _worksheet.Cells[_amountCell.Row, _amountCell.Column - 1];
newColumnHeader.Value2 = $"{product.Item1}";
newColumnHeader.WrapText = true;
foreach (var kvp in product.Item2)
{
FillPositionAmountToColumns(kvp, _amountCell.Column - 1, _amountCell.Column);
_progressBar.Update();
}
}
FilterByAmount();
_resultBar.Update();
}
}
private Worksheet OpenNewPrice()
{
if (_application.Workbooks
.Cast<Workbook>()
.FirstOrDefault(w => w.FullName == _pricelistPath) != null)
{
throw new ArgumentException("Шаблонный файл редактируется в другом месте");
}
return _application.Workbooks.Open(_pricelistPath, null, true).ActiveSheet;
}
private void FillPositionAmountToColumns(KeyValuePair<Product, double> positionAmount, params int[] columns)
{
Range worksheetCells = _worksheet.Cells;
Range skuColumn = _skuCell.EntireColumn;
int? row = GetPositionRow(skuColumn, positionAmount.Key.ProductSku, positionAmount.Key.ProductLines.FirstOrDefault());
if (row != null)
{
foreach (int column in columns)
{
Range cell = worksheetCells[row, column];
cell.AddValue(positionAmount.Value);
}
_resultBar.IncrementSuccess();
return;
}
if (_oldSkuCell != null)
{
row = GetPositionRow(_oldSkuCell.EntireColumn, positionAmount.Key.ProductSku, positionAmount.Key.ProductLines.FirstOrDefault());
if (row != null)
{
Range nameCell = worksheetCells[row, _nameCell.Column];
if (!Regex.IsMatch(nameCell.Value2, @"арт. \d{11}"))
{
nameCell.AddValue($"(замена арт. {positionAmount.Key.ProductSku})");
}
foreach (int column in columns)
{
Range cell = worksheetCells[row, column];
cell.AddValue(positionAmount.Value);
}
_resultBar.IncrementReplaced();
return;
}
}
row = GetPositionRow(skuColumn, positionAmount.Key.ProductSku, positionAmount.Key.ProductLines.FirstOrDefault(), true);
if (row != null)
{
Range nameCell = worksheetCells[row, _nameCell.Column];
if (!Regex.IsMatch(nameCell.Value2, @"арт. \d{11}"))
{
nameCell.AddValue($"(замена арт. {positionAmount.Key.ProductSku})");
}
foreach (int column in columns)
{
Range cell = worksheetCells[row, column];
cell.AddValue(positionAmount.Value);
}
_resultBar.IncrementReplaced();
return;
}
FillMissing(positionAmount, columns);
_resultBar.IncrementNotFound();
}
private void FillMissing(KeyValuePair<Product, double> positionAmount, params int[] columns)
{
Range worksheetCells = _worksheet.Cells;
Range worksheetRows = _worksheet.Rows;
int skuColumn = _skuCell.Column;
int groupColumn = _programLineCell.Column;
int nameColumn = _nameCell.Column;
Product product = positionAmount.Key;
int row = worksheetCells[worksheetRows.Count, skuColumn]
.End[XlDirection.xlUp]
.Row + 1;
worksheetRows[row]
.EntireRow
.Insert(XlInsertShiftDirection.xlShiftDown, XlInsertFormatOrigin.xlFormatFromLeftOrAbove);
Range previous = worksheetRows[row - 1];
Range current = worksheetRows[row];
previous.Copy(current);
current.ClearContents();
worksheetCells[row, groupColumn].Value2 = product.ProductLines.FirstOrDefault() ?? string.Empty;
worksheetCells[row, nameColumn].Value2 = $"{product.Name} (не найден арт. {product.ProductSku})";
worksheetCells[row, skuColumn].Value2 = "???";
if (_oldSkuCell != null)
{
worksheetCells[row, _oldSkuCell.Column].Value2 = product.ProductSku;
}
foreach (int column in columns)
{
Range cell = worksheetCells[row, column];
cell.AddValue(positionAmount.Value);
}
}
private int? GetPositionRow(Range range, ProductSku sku, string productLine, bool justArticle = false)
{
string lookupString = justArticle ? sku.Article : sku.ToString();
Range found = range.Find(lookupString);
string foundGroupValue;
string foundSkuValue;
if (found == null)
{
return null;
}
int firstFoundRow = found.Row;
while (true)
{
foundSkuValue = _worksheet.Cells[found.Row, range.Column].Value2.ToString();
foundGroupValue = _worksheet.Cells[found.Row, _programLineCell.Column].Value2.ToString();
if (ProductSku.TryParse(foundSkuValue, out var skus))
{
if (skus.Any(s => s.Article == sku.Article) &&
(string.IsNullOrEmpty(productLine) || productLine.Equals(foundGroupValue)) )
{
return found.Row;
}
else
{
found = range.FindNext(found);
if (found.Row == firstFoundRow)
{
return null;
}
}
}
else
{
found = range.FindNext(found);
if (found.Row == firstFoundRow)
{
return null;
}
}
}
}
private void FilterByAmount()
{
AutoFilter filter = _worksheet.AutoFilter;
int startColumn = filter.Range.Column;
filter.Range.AutoFilter(_amountCell.Column - startColumn + 1, "<>0", XlAutoFilterOperator.xlAnd, "<>");
_worksheet.Range["A1"].Activate();
}
public void Dispose()
{
_progressBar?.Dispose();
_resultBar?.Dispose();
}
}

View File

@ -0,0 +1,259 @@
using System.Text.RegularExpressions;
namespace RhSolutions.Services
{
public abstract class ExcelWriterBase
{
protected Application _application;
protected Dictionary<string, string> _headers;
protected ResultBar _resultBar;
protected ProgressBar _progressBar;
protected Worksheet _worksheet;
protected Range _amountCell;
protected Range _nameCell;
protected Range _oldSkuCell;
protected Range _programLineCell;
protected Range _skuCell;
protected bool _appendValues = true;
public void Dispose()
{
_progressBar?.Dispose();
_resultBar?.Dispose();
}
public void WriteProducts(Dictionary<Product, double> products)
{
WriteProducts(new[] { (string.Empty, products) });
}
public void WriteProducts(IEnumerable<(string, Dictionary<Product, double>)> products)
{
if (!_worksheet.IsValidSource())
{
_application.ActiveWorkbook.Close();
throw new ArgumentException(
$"Целевой файл {_application.ActiveWorkbook.Name} не является прайс-листом.");
}
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"]);
_progressBar = new("Заполняю строки...", products
.Select(p => p.Item2)
.Sum(set => set.Count));
if (products.Count() == 1)
{
foreach (var kvp in products.First().Item2)
{
FillAmounts(kvp, _amountCell.Column);
_progressBar.Update();
}
FilterByAmount();
_resultBar.Update();
}
else
{
foreach (var product in products)
{
_worksheet.Columns[_amountCell.Column]
.EntireColumn
.Insert(XlInsertShiftDirection.xlShiftToRight, XlInsertFormatOrigin.xlFormatFromRightOrBelow);
Range newColumnHeader = _worksheet.Cells[_amountCell.Row, _amountCell.Column - 1];
newColumnHeader.Value2 = $"{product.Item1}";
newColumnHeader.WrapText = true;
foreach (var kvp in product.Item2)
{
FillAmounts(kvp, _amountCell.Column - 1, _amountCell.Column);
_progressBar.Update();
}
}
FilterByAmount();
_resultBar.Update();
}
}
private void WriteOutMissing(KeyValuePair<Product, double> productAmount, params int[] columns)
{
Range worksheetCells = _worksheet.Cells;
Range worksheetRows = _worksheet.Rows;
int skuColumn = _skuCell.Column;
int groupColumn = _programLineCell.Column;
int nameColumn = _nameCell.Column;
Product product = productAmount.Key;
int row = worksheetCells[worksheetRows.Count, skuColumn]
.End[XlDirection.xlUp]
.Row + 1;
worksheetRows[row]
.EntireRow
.Insert(XlInsertShiftDirection.xlShiftDown, XlInsertFormatOrigin.xlFormatFromLeftOrAbove);
Range previous = worksheetRows[row - 1];
Range current = worksheetRows[row];
previous.Copy(current);
current.ClearContents();
worksheetCells[row, groupColumn].Value2 = product.ProductLines.FirstOrDefault() ?? string.Empty;
worksheetCells[row, nameColumn].Value2 = $"{product.Name} (не найден арт. {product.ProductSku})";
worksheetCells[row, skuColumn].Value2 = "???";
if (_oldSkuCell != null)
{
worksheetCells[row, _oldSkuCell.Column].Value2 = product.ProductSku;
}
foreach (int column in columns)
{
Range cell = worksheetCells[row, column];
cell.AddValue(productAmount.Value);
}
}
private void FillCells(double amount, int row, int[] columns)
{
foreach (int column in columns)
{
Range cell = _worksheet.Cells[row, column];
if (_appendValues)
{
cell.AddValue(amount);
}
else
{
cell.Value2 = amount;
}
}
}
private void FillAmounts(KeyValuePair<Product, double> positionAmount, params int[] columns)
{
Range range = _skuCell.EntireColumn;
ProductSku sku = positionAmount.Key.ProductSku;
string productLine = positionAmount.Key.ProductLines.FirstOrDefault();
int? row = GetPositionRow(range, sku, productLine);
if (row != null)
{
FillCells(positionAmount.Value, row.Value, columns);
_resultBar.IncrementSuccess();
}
else
{
if (_oldSkuCell != null)
{
range = _oldSkuCell.EntireColumn;
row = GetPositionRow(range, sku, productLine);
}
if (row == null)
{
range = _skuCell.EntireColumn;
row = GetPositionRow(range, sku, productLine, true);
}
if (row != null)
{
Range nameCell = _worksheet.Cells[row, _nameCell.Column];
if (!Regex.IsMatch(nameCell.Value2, @"арт. \d{11}"))
{
nameCell.AddValue($"(замена арт. {sku})");
}
FillCells(positionAmount.Value, row.Value, columns);
_resultBar.IncrementReplaced();
}
else
{
WriteOutMissing(positionAmount, columns);
_resultBar.IncrementNotFound();
}
}
}
private void FilterByAmount()
{
if (_worksheet.AutoFilterMode)
{
AutoFilter filter = _worksheet.AutoFilter;
int startColumn = filter.Range.Column;
filter.Range.AutoFilter(_amountCell.Column - startColumn + 1, "<>0", XlAutoFilterOperator.xlAnd, "<>");
_worksheet.Range["A1"].Activate();
}
}
private void ShowFilteredData()
{
if (_worksheet.AutoFilterMode)
{
AutoFilter filter = _worksheet.AutoFilter;
filter.ShowAllData();
}
}
private int? GetPositionRow(Range range, ProductSku sku, string productLine, bool justArticle = false)
{
string lookupString = justArticle ? sku.Article : sku.ToString();
Range found = range.Find(lookupString);
string foundGroupValue;
string foundSkuValue;
if (found == null)
{
return null;
}
int firstFoundRow = found.Row;
while (true)
{
foundSkuValue = _worksheet.Cells[found.Row, range.Column].Value2.ToString();
foundGroupValue = _worksheet.Cells[found.Row, _programLineCell.Column].Value2.ToString();
if (ProductSku.TryParse(foundSkuValue, out var skus))
{
if (skus.Any(s => s.Article == sku.Article) &&
(string.IsNullOrEmpty(productLine) || productLine.Equals(foundGroupValue)))
{
return found.Row;
}
else
{
found = range.FindNext(found);
if (found.Row == firstFoundRow)
{
return null;
}
}
}
else
{
found = range.FindNext(found);
if (found.Row == firstFoundRow)
{
return null;
}
}
}
}
}
}

View File

@ -0,0 +1,6 @@
namespace RhSolutions.Services;
public interface ISleevesCalculator
{
public Dictionary<Product, double> CalculateSleeves(Dictionary<Product, double> products);
}

View File

@ -0,0 +1,34 @@
#if !NET472
using System.Runtime.Versioning;
using RhSolutions.Tools;
#endif
using System.Text.RegularExpressions;
namespace RhSolutions.Services;
#if !NET472
[SupportedOSPlatform("windows")]
#endif
public class NewPriceWriter : ExcelWriterBase, IWriter, IDisposable
{
public NewPriceWriter(Application application, IAddInConfiguration configuration)
{
_application = application;
_resultBar = new();
_headers = configuration.GetPriceListHeaders();
_worksheet = OpenNewPrice(configuration.GetPriceListPath());
}
private Worksheet OpenNewPrice(string pricelistPath)
{
if (_application.Workbooks
.Cast<Workbook>()
.FirstOrDefault(w => w.FullName == pricelistPath) != null)
{
throw new ArgumentException("Шаблонный файл редактируется в другом месте");
}
return _application.Workbooks.Open(pricelistPath, null, true).ActiveSheet;
}
}

View File

@ -0,0 +1,63 @@
using System.Text.RegularExpressions;
namespace RhSolutions.Services;
public class SleevesCalculator : ISleevesCalculator
{
private const string doublePattern =
@"((?i)равнопроходная|угольник 90|угольник 45|Т-образная|Комплект трубок(?i))(.+?(?<Sleeve>\b16\b|\b20\b|\b25\b|\b32\b|\b40\b|\b50\b|\b63\b))+";
private const string singlePattern =
@"((?i)муфта|тройник|переходник|угольник|штуцер|Г-образная|заглушка(?i))(.+?(?<Sleeve>\b16\b|\b20\b|\b25\b|\b32\b|\b40\b|\b50\b|\b63\b))+";
public Dictionary<Product, double> CalculateSleeves(Dictionary<Product, double> products)
{
Dictionary<string, double> result = new()
{
["16"] = 0,
["20"] = 0,
["25"] = 0,
["32"] = 0,
["40"] = 0,
["50"] = 0,
["63"] = 0,
};
var rautitanProducts = products.Where(kvp => kvp.Key.ProductLines.Contains("RAUTITAN"));
foreach (var kvp in rautitanProducts)
{
var doubleCollection = Regex.Matches(kvp.Key.Name, doublePattern);
if (doubleCollection.Count != 0)
{
CaptureCollection collection = doubleCollection[0].Groups["Sleeve"].Captures;
foreach (Capture sleeve in collection)
{
result[sleeve.Value] += kvp.Value * 2;
}
continue;
}
var singleCollection = Regex.Matches(kvp.Key.Name, singlePattern);
if (singleCollection.Count != 0)
{
CaptureCollection collection = singleCollection[0].Groups["Sleeve"].Captures;
foreach (Capture sleeve in collection)
{
result[sleeve.Value] += kvp.Value;
}
}
}
return result
.ToDictionary(kvp =>
kvp.Key switch
{
"16" => new Product("11600011001"),
"20" => new Product("11600021001"),
"25" => new Product("11600031001"),
"32" => new Product("11600041001"),
"40" => new Product("11600051001"),
"50" => new Product("11397711002"),
"63" => new Product("11397811002"),
_ => throw new Exception($"Неизвестный диаметр {kvp.Key}")
}, kvp => kvp.Value);
}
}

View File

@ -13,7 +13,8 @@ public class WriterFactory
{
return writerName switch
{
"Excel" => (IWriter)_serviceProvider.GetService(typeof(ExcelWriter)),
"NewPrice" => (IWriter)_serviceProvider.GetService(typeof(NewPriceWriter)),
"CurrentPrice" => (IWriter)_serviceProvider.GetService(typeof(CurrentPriceWriter)),
"Dxf" => (IWriter)_serviceProvider.GetService(typeof(DxfWriter)),
_ => throw new ArgumentException($"Незвестный интерфейс {nameof(IWriter)}: {writerName}")
};

View File

@ -10,7 +10,7 @@ namespace RhSolutions.Tools;
#endif
internal class ConvertTool : Tool
{
public ConvertTool(IServiceProvider provider) : base(provider)
public ConvertTool(ReaderFactory readerFactory, WriterFactory writerFactory) : base(readerFactory, writerFactory)
{
}
@ -19,7 +19,7 @@ internal class ConvertTool : Tool
Application app = RhSolutionsAddIn.Excel.Application;
Worksheet worksheet = app.ActiveWorkbook.ActiveSheet;
_reader = _readerFactory.GetReader("Excel");
_writer = _writerFactory.GetWriter("Excel");
_writer = _writerFactory.GetWriter("NewPrice");
var products = _reader.ReadProducts(new[] { worksheet });
_writer.WriteProducts(products);
}

View File

@ -9,7 +9,7 @@ namespace RhSolutions.Tools;
#endif
internal class DxfTool : Tool
{
public DxfTool(IServiceProvider provider) : base(provider)
public DxfTool(ReaderFactory readerFactory, WriterFactory writerFactory) : base(readerFactory, writerFactory)
{
}

View File

@ -34,7 +34,8 @@ namespace RhSolutions.Tools
RibbonController.UpdateWorkbookValidation();
RibbonController.RefreshControl("convert");
RibbonController.RefreshControl("dxfexport");
RibbonController.RefreshControl("Guessexport");
RibbonController.RefreshControl("guess");
RibbonController.RefreshControl("fillsleeves");
}
private static void RefreshExportButton(object sh, Range target)

View File

@ -9,7 +9,7 @@ namespace RhSolutions.Tools;
#endif
internal class ExportTool : Tool
{
public ExportTool(IServiceProvider provider) : base(provider)
public ExportTool(ReaderFactory readerFactory, WriterFactory writerFactory) : base(readerFactory, writerFactory)
{
}
@ -17,7 +17,7 @@ internal class ExportTool : Tool
{
Application app = RhSolutionsAddIn.Excel.Application;
_reader = _readerFactory.GetReader("Excel");
_writer = _writerFactory.GetWriter("Excel");
_writer = _writerFactory.GetWriter("NewPrice");
var products = _reader.ReadProducts(app.Selection);
_writer.WriteProducts(products);
}

View File

@ -6,7 +6,7 @@ namespace RhSolutions.Tools;
internal class GuessTool : Tool
{
public GuessTool(IServiceProvider provider) : base(provider)
public GuessTool(ReaderFactory readerFactory, WriterFactory writerFactory) : base(readerFactory, writerFactory)
{
}
@ -15,7 +15,7 @@ internal class GuessTool : Tool
Application app = RhSolutionsAddIn.Excel.Application;
Worksheet worksheet = app.ActiveWorkbook.ActiveSheet;
_reader = _readerFactory.GetReader("Guess");
_writer = _writerFactory.GetWriter("Excel");
_writer = _writerFactory.GetWriter("NewPrice");
var products = _reader.ReadProducts(new[] { worksheet });
_writer.WriteProducts(products);
}

View File

@ -9,7 +9,7 @@ namespace RhSolutions.Tools;
#endif
internal class MergeTool : Tool
{
public MergeTool(IServiceProvider provider) : base(provider)
public MergeTool(ReaderFactory readerFactory, WriterFactory writerFactory) : base(readerFactory, writerFactory)
{
}
@ -18,7 +18,7 @@ internal class MergeTool : Tool
IFileDialog dialog = RhSolutionsAddIn.ServiceProvider.GetRequiredService<IFileDialog>();
string[] files = dialog.GetFiles();
_reader = _readerFactory.GetReader("Excel");
_writer = _writerFactory.GetWriter("Excel");
_writer = _writerFactory.GetWriter("NewPrice");
var products = _reader.ReadProducts(files);
_writer.WriteProducts(products);
}

View File

@ -7,7 +7,7 @@ namespace RhSolutions.Tools;
#if !NET472
[SupportedOSPlatform("windows")]
#endif
internal class ProgressBar : StatusbarBase
public class ProgressBar : StatusbarBase
{
private double CurrentProgress { get; set; }
private readonly double TaskWeight;

View File

@ -8,7 +8,7 @@ namespace RhSolutions.Tools;
#if !NET472
[SupportedOSPlatform("windows")]
#endif
internal class ResultBar : StatusbarBase
public class ResultBar : StatusbarBase
{
private int Success { get; set; }
private int Replaced { get; set; }

View File

@ -0,0 +1,22 @@
namespace RhSolutions.Tools;
internal class SleevesTool : Tool
{
private readonly ISleevesCalculator _sleevesCaluculator;
public SleevesTool(ReaderFactory readerFactory, WriterFactory writerFactory, ISleevesCalculator sleevesCaluculator) : base(readerFactory, writerFactory)
{
_sleevesCaluculator = sleevesCaluculator;
}
public override void Execute()
{
Application app = RhSolutionsAddIn.Excel.Application;
Worksheet worksheet = app.ActiveWorkbook.ActiveSheet;
_reader = _readerFactory.GetReader("Excel");
_writer = _writerFactory.GetWriter("CurrentPrice");
var products = _reader.ReadProducts(new[] { worksheet });
var sleeves = _sleevesCaluculator.CalculateSleeves(products.Select(p => p.Item2).First());
_writer.WriteProducts(sleeves);
}
}

View File

@ -11,7 +11,7 @@ namespace RhSolutions.Tools;
#if !NET472
[SupportedOSPlatform("windows")]
#endif
internal abstract class StatusbarBase : IDisposable
public abstract class StatusbarBase : IDisposable
{
protected Application Excel = RhSolutionsAddIn.Excel;

View File

@ -14,16 +14,16 @@ internal abstract class Tool : IDisposable
protected IReader _reader;
protected IWriter _writer;
public Tool(IServiceProvider provider)
public Tool(ReaderFactory readerFactory, WriterFactory writerFactory)
{
_readerFactory = provider.GetRequiredService<ReaderFactory>();
_writerFactory = provider.GetRequiredService<WriterFactory>();
_readerFactory = readerFactory;
_writerFactory = writerFactory;
}
public void Dispose()
{
_reader.Dispose();
_writer.Dispose();
_reader?.Dispose();
_writer?.Dispose();
}
public abstract void Execute();

View File

@ -2,15 +2,20 @@
internal class ToolFactory
{
static ReaderFactory readerFactory = RhSolutionsAddIn.ServiceProvider.GetService<ReaderFactory>();
static WriterFactory writerFactory = RhSolutionsAddIn.ServiceProvider.GetService<WriterFactory>();
static ISleevesCalculator sleevesCaluculator = RhSolutionsAddIn.ServiceProvider.GetService<ISleevesCalculator>();
public Tool GetTool(string toolName)
{
Tool tool = toolName switch
{
"export" => new ExportTool(RhSolutionsAddIn.ServiceProvider),
"convert" => new ConvertTool(RhSolutionsAddIn.ServiceProvider),
"merge" => new MergeTool(RhSolutionsAddIn.ServiceProvider),
"dxfexport" => new DxfTool(RhSolutionsAddIn.ServiceProvider),
"Guessexport" => new GuessTool(RhSolutionsAddIn.ServiceProvider),
"export" => new ExportTool(readerFactory, writerFactory),
"convert" => new ConvertTool(readerFactory, writerFactory),
"merge" => new MergeTool(readerFactory, writerFactory),
"dxfexport" => new DxfTool(readerFactory, writerFactory),
"guess" => new GuessTool(readerFactory, writerFactory),
"fillsleeves" => new SleevesTool(readerFactory, writerFactory, sleevesCaluculator),
_ => throw new Exception($"Неизвестный инструмент {toolName}"),
};
return tool;

View File

@ -25,7 +25,7 @@ public class CanDoGuess : IDisposable
Worksheet sourceSheet = Util.Workbook.Worksheets[1];
RhSolutionsAddIn.Configuration.SetPriceListPath(Path.GetFullPath(@"..\..\..\TestWorkbooks\TargetSpecificationGuess.xlsx"));
var products = _guessReader.ReadProducts(new[] { sourceSheet });
var _writer = new ExcelWriter(Util.Application, RhSolutionsAddIn.Configuration);
var _writer = new NewPriceWriter(Util.Application, RhSolutionsAddIn.Configuration);
_writer.WriteProducts(products);
Worksheet targetSheet = Util.Application.ActiveWindow.ActiveSheet;
var targetProducts = _reader.ReadProducts(new[] { targetSheet });
@ -42,7 +42,7 @@ public class CanDoGuess : IDisposable
Worksheet sourceSheet = Util.Workbook.Worksheets[1];
RhSolutionsAddIn.Configuration.SetPriceListPath(Path.GetFullPath(@"..\..\..\TestWorkbooks\TargetSpecificationGuessOneRow.xlsx"));
var products = _guessReader.ReadProducts(new[] { sourceSheet });
var _writer = new ExcelWriter(Util.Application, RhSolutionsAddIn.Configuration);
var _writer = new NewPriceWriter(Util.Application, RhSolutionsAddIn.Configuration);
_writer.WriteProducts(products);
Worksheet targetSheet = Util.Application.ActiveWindow.ActiveSheet;
var targetProducts = _reader.ReadProducts(new[] { targetSheet });

View File

@ -0,0 +1,44 @@
using RhSolutions.AddIn;
namespace RhSolutions.Tests;
[ExcelTestSettings(OutOfProcess = true)]
public class CanFillSleeves : IDisposable
{
private RhSolutionsAddIn _addIn;
private ISleevesCalculator _calculator;
private IReader _reader;
private IWriter _writer;
private Worksheet _worksheet;
public CanFillSleeves()
{
_addIn = new();
_addIn.AutoOpen();
_calculator = new SleevesCalculator();
_reader = new ExcelReader(Util.Application, RhSolutionsAddIn.Configuration);
_writer = new CurrentPriceWriter(Util.Application, RhSolutionsAddIn.Configuration);
_worksheet = Util.Workbook.Worksheets[1];
}
[ExcelFact(Workbook = @"..\..\..\TestWorkbooks\TestSpecificationSleeves.xlsx")]
public void CanCalculateSleeves()
{
var products = _reader.ReadProducts(new[] { _worksheet });
var sleeves = _calculator.CalculateSleeves(products.First().Item2);
_writer.WriteProducts(sleeves);
Assert.Equal(25, _worksheet.Range["E2"].Value);
Assert.Equal(15, _worksheet.Range["E3"].Value);
Assert.Equal(7, _worksheet.Range["E4"].Value);
Assert.Equal(8, _worksheet.Range["E5"].Value);
Assert.Equal(1, _worksheet.Range["E6"].Value);
Assert.Equal(3, _worksheet.Range["E7"].Value);
Assert.Equal(4, _worksheet.Range["E8"].Value);
}
public void Dispose()
{
_addIn.AutoClose();
}
}

View File

@ -23,7 +23,7 @@ public class CanWriteProducts : IDisposable
Worksheet sourceSheet = Util.Workbook.Worksheets[1];
RhSolutionsAddIn.Configuration.SetPriceListPath(Path.GetFullPath(@"..\..\..\TestWorkbooks\TargetSpecification.xlsx"));
var products = _reader.ReadProducts(new[] { sourceSheet });
var _writer = new ExcelWriter(Util.Application, RhSolutionsAddIn.Configuration);
var _writer = new NewPriceWriter(Util.Application, RhSolutionsAddIn.Configuration);
_writer.WriteProducts(products);
Worksheet targetSheet = Util.Application.ActiveWindow.ActiveSheet;
var targetProducts = _reader.ReadProducts(new[] { targetSheet });
@ -40,7 +40,7 @@ public class CanWriteProducts : IDisposable
Worksheet sourceSheet = Util.Workbook.Worksheets[1];
RhSolutionsAddIn.Configuration.SetPriceListPath(Path.GetFullPath(@"..\..\..\TestWorkbooks\TargetSpecificationMultipleProductLines.xlsx"));
var products = _reader.ReadProducts(new[] { sourceSheet });
var _writer = new ExcelWriter(Util.Application, RhSolutionsAddIn.Configuration);
var _writer = new NewPriceWriter(Util.Application, RhSolutionsAddIn.Configuration);
_writer.WriteProducts(products);
Worksheet targetSheet = Util.Application.ActiveWindow.ActiveSheet;
var targetProducts = _reader.ReadProducts(new[] { targetSheet });
@ -57,7 +57,7 @@ public class CanWriteProducts : IDisposable
Worksheet sourceSheet = Util.Workbook.Worksheets[1];
RhSolutionsAddIn.Configuration.SetPriceListPath(Path.GetFullPath(@"..\..\..\TestWorkbooks\TargetSpecificationNotFound.xlsx"));
var products = _reader.ReadProducts(new[] { sourceSheet });
var _writer = new ExcelWriter(Util.Application, RhSolutionsAddIn.Configuration);
var _writer = new NewPriceWriter(Util.Application, RhSolutionsAddIn.Configuration);
_writer.WriteProducts(products);
Worksheet targetSheet = Util.Application.ActiveWindow.ActiveSheet;
@ -72,7 +72,7 @@ public class CanWriteProducts : IDisposable
Worksheet sourceSheet = Util.Workbook.Worksheets[1];
RhSolutionsAddIn.Configuration.SetPriceListPath(Path.GetFullPath(@"..\..\..\TestWorkbooks\TargetSpecificationReplaced.xlsx"));
var products = _reader.ReadProducts(new[] { sourceSheet });
var _writer = new ExcelWriter(Util.Application, RhSolutionsAddIn.Configuration);
var _writer = new NewPriceWriter(Util.Application, RhSolutionsAddIn.Configuration);
_writer.WriteProducts(products);
Worksheet targetSheet = Util.Application.ActiveWindow.ActiveSheet;
var targetProducts = _reader.ReadProducts(new[] { targetSheet });
@ -94,7 +94,7 @@ public class CanWriteProducts : IDisposable
Worksheet sourceSheet = Util.Workbook.Worksheets[1];
RhSolutionsAddIn.Configuration.SetPriceListPath(Path.GetFullPath(@"..\..\..\TestWorkbooks\TargetSpecificationNewVariant.xlsx"));
var products = _reader.ReadProducts(new[] { sourceSheet });
var _writer = new ExcelWriter(Util.Application, RhSolutionsAddIn.Configuration);
var _writer = new NewPriceWriter(Util.Application, RhSolutionsAddIn.Configuration);
_writer.WriteProducts(products);
Worksheet targetSheet = Util.Application.ActiveWindow.ActiveSheet;
var targetProducts = _reader.ReadProducts(new[] { targetSheet });

View File

@ -23,7 +23,7 @@ public class RealPricelistTest : IDisposable
Worksheet sourceSheet = Util.Workbook.Worksheets[1];
RhSolutionsAddIn.Configuration.SetPriceListPath(Path.GetFullPath(@"..\..\..\TestWorkbooks\RealTargetSpecification.xlsx"));
var products = _reader.ReadProducts(new[] { sourceSheet });
var _writer = new ExcelWriter(Util.Application, RhSolutionsAddIn.Configuration);
var _writer = new NewPriceWriter(Util.Application, RhSolutionsAddIn.Configuration);
_writer.WriteProducts(products);
Worksheet targetSheet = Util.Application.ActiveWindow.ActiveSheet;
targetSheet.Range["A1"].Formula = "=SUM(H:H)";