Extract Excel Write Base
This commit is contained in:
parent
50543ae697
commit
7b8cab3e9e
@ -28,8 +28,8 @@ public sealed class RhSolutionsAddIn : IExcelAddIn
|
|||||||
.AddTransient<IFileDialog, FileDialog>();
|
.AddTransient<IFileDialog, FileDialog>();
|
||||||
|
|
||||||
Services.AddSingleton<WriterFactory>();
|
Services.AddSingleton<WriterFactory>();
|
||||||
Services.AddTransient<ExcelWriter>()
|
Services.AddTransient<NewPriceWriter>()
|
||||||
.AddTransient<IWriter, ExcelWriter>(s => s.GetService<ExcelWriter>());
|
.AddTransient<IWriter, NewPriceWriter>(s => s.GetService<NewPriceWriter>());
|
||||||
Services.AddTransient<DxfWriter>()
|
Services.AddTransient<DxfWriter>()
|
||||||
.AddTransient<IWriter, DxfWriter>(s => s.GetService<DxfWriter>());
|
.AddTransient<IWriter, DxfWriter>(s => s.GetService<DxfWriter>());
|
||||||
|
|
||||||
|
@ -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();
|
|
||||||
}
|
|
||||||
}
|
|
245
RhSolutions.AddIn/Services/ExcelWriterBase.cs
Normal file
245
RhSolutions.AddIn/Services/ExcelWriterBase.cs
Normal file
@ -0,0 +1,245 @@
|
|||||||
|
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 Range _amountCell;
|
||||||
|
protected Range _nameCell;
|
||||||
|
protected Range _oldSkuCell;
|
||||||
|
protected Range _programLineCell;
|
||||||
|
protected Range _skuCell;
|
||||||
|
protected Worksheet _worksheet;
|
||||||
|
|
||||||
|
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} не является прайс-листом.");
|
||||||
|
}
|
||||||
|
|
||||||
|
_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 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 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 FilterByAmount()
|
||||||
|
{
|
||||||
|
AutoFilter filter = _worksheet.AutoFilter;
|
||||||
|
int startColumn = filter.Range.Column;
|
||||||
|
|
||||||
|
filter.Range.AutoFilter(_amountCell.Column - startColumn + 1, "<>0", XlAutoFilterOperator.xlAnd, "<>");
|
||||||
|
_worksheet.Range["A1"].Activate();
|
||||||
|
}
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
34
RhSolutions.AddIn/Services/NewPriceWriter.cs
Normal file
34
RhSolutions.AddIn/Services/NewPriceWriter.cs
Normal 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;
|
||||||
|
}
|
||||||
|
}
|
@ -13,7 +13,7 @@ public class WriterFactory
|
|||||||
{
|
{
|
||||||
return writerName switch
|
return writerName switch
|
||||||
{
|
{
|
||||||
"Excel" => (IWriter)_serviceProvider.GetService(typeof(ExcelWriter)),
|
"NewPrice" => (IWriter)_serviceProvider.GetService(typeof(NewPriceWriter)),
|
||||||
"Dxf" => (IWriter)_serviceProvider.GetService(typeof(DxfWriter)),
|
"Dxf" => (IWriter)_serviceProvider.GetService(typeof(DxfWriter)),
|
||||||
_ => throw new ArgumentException($"Незвестный интерфейс {nameof(IWriter)}: {writerName}")
|
_ => throw new ArgumentException($"Незвестный интерфейс {nameof(IWriter)}: {writerName}")
|
||||||
};
|
};
|
||||||
|
@ -19,7 +19,7 @@ internal class ConvertTool : Tool
|
|||||||
Application app = RhSolutionsAddIn.Excel.Application;
|
Application app = RhSolutionsAddIn.Excel.Application;
|
||||||
Worksheet worksheet = app.ActiveWorkbook.ActiveSheet;
|
Worksheet worksheet = app.ActiveWorkbook.ActiveSheet;
|
||||||
_reader = _readerFactory.GetReader("Excel");
|
_reader = _readerFactory.GetReader("Excel");
|
||||||
_writer = _writerFactory.GetWriter("Excel");
|
_writer = _writerFactory.GetWriter("NewPrice");
|
||||||
var products = _reader.ReadProducts(new[] { worksheet });
|
var products = _reader.ReadProducts(new[] { worksheet });
|
||||||
_writer.WriteProducts(products);
|
_writer.WriteProducts(products);
|
||||||
}
|
}
|
||||||
|
@ -17,7 +17,7 @@ internal class ExportTool : Tool
|
|||||||
{
|
{
|
||||||
Application app = RhSolutionsAddIn.Excel.Application;
|
Application app = RhSolutionsAddIn.Excel.Application;
|
||||||
_reader = _readerFactory.GetReader("Excel");
|
_reader = _readerFactory.GetReader("Excel");
|
||||||
_writer = _writerFactory.GetWriter("Excel");
|
_writer = _writerFactory.GetWriter("NewPrice");
|
||||||
var products = _reader.ReadProducts(app.Selection);
|
var products = _reader.ReadProducts(app.Selection);
|
||||||
_writer.WriteProducts(products);
|
_writer.WriteProducts(products);
|
||||||
}
|
}
|
||||||
|
@ -15,7 +15,7 @@ internal class GuessTool : Tool
|
|||||||
Application app = RhSolutionsAddIn.Excel.Application;
|
Application app = RhSolutionsAddIn.Excel.Application;
|
||||||
Worksheet worksheet = app.ActiveWorkbook.ActiveSheet;
|
Worksheet worksheet = app.ActiveWorkbook.ActiveSheet;
|
||||||
_reader = _readerFactory.GetReader("Guess");
|
_reader = _readerFactory.GetReader("Guess");
|
||||||
_writer = _writerFactory.GetWriter("Excel");
|
_writer = _writerFactory.GetWriter("NewPrice");
|
||||||
var products = _reader.ReadProducts(new[] { worksheet });
|
var products = _reader.ReadProducts(new[] { worksheet });
|
||||||
_writer.WriteProducts(products);
|
_writer.WriteProducts(products);
|
||||||
}
|
}
|
||||||
|
@ -18,7 +18,7 @@ internal class MergeTool : Tool
|
|||||||
IFileDialog dialog = RhSolutionsAddIn.ServiceProvider.GetRequiredService<IFileDialog>();
|
IFileDialog dialog = RhSolutionsAddIn.ServiceProvider.GetRequiredService<IFileDialog>();
|
||||||
string[] files = dialog.GetFiles();
|
string[] files = dialog.GetFiles();
|
||||||
_reader = _readerFactory.GetReader("Excel");
|
_reader = _readerFactory.GetReader("Excel");
|
||||||
_writer = _writerFactory.GetWriter("Excel");
|
_writer = _writerFactory.GetWriter("NewPrice");
|
||||||
var products = _reader.ReadProducts(files);
|
var products = _reader.ReadProducts(files);
|
||||||
_writer.WriteProducts(products);
|
_writer.WriteProducts(products);
|
||||||
}
|
}
|
||||||
|
@ -7,7 +7,7 @@ namespace RhSolutions.Tools;
|
|||||||
#if !NET472
|
#if !NET472
|
||||||
[SupportedOSPlatform("windows")]
|
[SupportedOSPlatform("windows")]
|
||||||
#endif
|
#endif
|
||||||
internal class ProgressBar : StatusbarBase
|
public class ProgressBar : StatusbarBase
|
||||||
{
|
{
|
||||||
private double CurrentProgress { get; set; }
|
private double CurrentProgress { get; set; }
|
||||||
private readonly double TaskWeight;
|
private readonly double TaskWeight;
|
||||||
|
@ -8,7 +8,7 @@ namespace RhSolutions.Tools;
|
|||||||
#if !NET472
|
#if !NET472
|
||||||
[SupportedOSPlatform("windows")]
|
[SupportedOSPlatform("windows")]
|
||||||
#endif
|
#endif
|
||||||
internal class ResultBar : StatusbarBase
|
public class ResultBar : StatusbarBase
|
||||||
{
|
{
|
||||||
private int Success { get; set; }
|
private int Success { get; set; }
|
||||||
private int Replaced { get; set; }
|
private int Replaced { get; set; }
|
||||||
|
@ -11,7 +11,7 @@ namespace RhSolutions.Tools;
|
|||||||
#if !NET472
|
#if !NET472
|
||||||
[SupportedOSPlatform("windows")]
|
[SupportedOSPlatform("windows")]
|
||||||
#endif
|
#endif
|
||||||
internal abstract class StatusbarBase : IDisposable
|
public abstract class StatusbarBase : IDisposable
|
||||||
{
|
{
|
||||||
protected Application Excel = RhSolutionsAddIn.Excel;
|
protected Application Excel = RhSolutionsAddIn.Excel;
|
||||||
|
|
||||||
|
@ -25,7 +25,7 @@ public class CanDoGuess : IDisposable
|
|||||||
Worksheet sourceSheet = Util.Workbook.Worksheets[1];
|
Worksheet sourceSheet = Util.Workbook.Worksheets[1];
|
||||||
RhSolutionsAddIn.Configuration.SetPriceListPath(Path.GetFullPath(@"..\..\..\TestWorkbooks\TargetSpecificationGuess.xlsx"));
|
RhSolutionsAddIn.Configuration.SetPriceListPath(Path.GetFullPath(@"..\..\..\TestWorkbooks\TargetSpecificationGuess.xlsx"));
|
||||||
var products = _guessReader.ReadProducts(new[] { sourceSheet });
|
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);
|
_writer.WriteProducts(products);
|
||||||
Worksheet targetSheet = Util.Application.ActiveWindow.ActiveSheet;
|
Worksheet targetSheet = Util.Application.ActiveWindow.ActiveSheet;
|
||||||
var targetProducts = _reader.ReadProducts(new[] { targetSheet });
|
var targetProducts = _reader.ReadProducts(new[] { targetSheet });
|
||||||
@ -42,7 +42,7 @@ public class CanDoGuess : IDisposable
|
|||||||
Worksheet sourceSheet = Util.Workbook.Worksheets[1];
|
Worksheet sourceSheet = Util.Workbook.Worksheets[1];
|
||||||
RhSolutionsAddIn.Configuration.SetPriceListPath(Path.GetFullPath(@"..\..\..\TestWorkbooks\TargetSpecificationGuessOneRow.xlsx"));
|
RhSolutionsAddIn.Configuration.SetPriceListPath(Path.GetFullPath(@"..\..\..\TestWorkbooks\TargetSpecificationGuessOneRow.xlsx"));
|
||||||
var products = _guessReader.ReadProducts(new[] { sourceSheet });
|
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);
|
_writer.WriteProducts(products);
|
||||||
Worksheet targetSheet = Util.Application.ActiveWindow.ActiveSheet;
|
Worksheet targetSheet = Util.Application.ActiveWindow.ActiveSheet;
|
||||||
var targetProducts = _reader.ReadProducts(new[] { targetSheet });
|
var targetProducts = _reader.ReadProducts(new[] { targetSheet });
|
||||||
|
@ -23,7 +23,7 @@ public class CanWriteProducts : IDisposable
|
|||||||
Worksheet sourceSheet = Util.Workbook.Worksheets[1];
|
Worksheet sourceSheet = Util.Workbook.Worksheets[1];
|
||||||
RhSolutionsAddIn.Configuration.SetPriceListPath(Path.GetFullPath(@"..\..\..\TestWorkbooks\TargetSpecification.xlsx"));
|
RhSolutionsAddIn.Configuration.SetPriceListPath(Path.GetFullPath(@"..\..\..\TestWorkbooks\TargetSpecification.xlsx"));
|
||||||
var products = _reader.ReadProducts(new[] { sourceSheet });
|
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);
|
_writer.WriteProducts(products);
|
||||||
Worksheet targetSheet = Util.Application.ActiveWindow.ActiveSheet;
|
Worksheet targetSheet = Util.Application.ActiveWindow.ActiveSheet;
|
||||||
var targetProducts = _reader.ReadProducts(new[] { targetSheet });
|
var targetProducts = _reader.ReadProducts(new[] { targetSheet });
|
||||||
@ -40,7 +40,7 @@ public class CanWriteProducts : IDisposable
|
|||||||
Worksheet sourceSheet = Util.Workbook.Worksheets[1];
|
Worksheet sourceSheet = Util.Workbook.Worksheets[1];
|
||||||
RhSolutionsAddIn.Configuration.SetPriceListPath(Path.GetFullPath(@"..\..\..\TestWorkbooks\TargetSpecificationMultipleProductLines.xlsx"));
|
RhSolutionsAddIn.Configuration.SetPriceListPath(Path.GetFullPath(@"..\..\..\TestWorkbooks\TargetSpecificationMultipleProductLines.xlsx"));
|
||||||
var products = _reader.ReadProducts(new[] { sourceSheet });
|
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);
|
_writer.WriteProducts(products);
|
||||||
Worksheet targetSheet = Util.Application.ActiveWindow.ActiveSheet;
|
Worksheet targetSheet = Util.Application.ActiveWindow.ActiveSheet;
|
||||||
var targetProducts = _reader.ReadProducts(new[] { targetSheet });
|
var targetProducts = _reader.ReadProducts(new[] { targetSheet });
|
||||||
@ -57,7 +57,7 @@ public class CanWriteProducts : IDisposable
|
|||||||
Worksheet sourceSheet = Util.Workbook.Worksheets[1];
|
Worksheet sourceSheet = Util.Workbook.Worksheets[1];
|
||||||
RhSolutionsAddIn.Configuration.SetPriceListPath(Path.GetFullPath(@"..\..\..\TestWorkbooks\TargetSpecificationNotFound.xlsx"));
|
RhSolutionsAddIn.Configuration.SetPriceListPath(Path.GetFullPath(@"..\..\..\TestWorkbooks\TargetSpecificationNotFound.xlsx"));
|
||||||
var products = _reader.ReadProducts(new[] { sourceSheet });
|
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);
|
_writer.WriteProducts(products);
|
||||||
Worksheet targetSheet = Util.Application.ActiveWindow.ActiveSheet;
|
Worksheet targetSheet = Util.Application.ActiveWindow.ActiveSheet;
|
||||||
|
|
||||||
@ -72,7 +72,7 @@ public class CanWriteProducts : IDisposable
|
|||||||
Worksheet sourceSheet = Util.Workbook.Worksheets[1];
|
Worksheet sourceSheet = Util.Workbook.Worksheets[1];
|
||||||
RhSolutionsAddIn.Configuration.SetPriceListPath(Path.GetFullPath(@"..\..\..\TestWorkbooks\TargetSpecificationReplaced.xlsx"));
|
RhSolutionsAddIn.Configuration.SetPriceListPath(Path.GetFullPath(@"..\..\..\TestWorkbooks\TargetSpecificationReplaced.xlsx"));
|
||||||
var products = _reader.ReadProducts(new[] { sourceSheet });
|
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);
|
_writer.WriteProducts(products);
|
||||||
Worksheet targetSheet = Util.Application.ActiveWindow.ActiveSheet;
|
Worksheet targetSheet = Util.Application.ActiveWindow.ActiveSheet;
|
||||||
var targetProducts = _reader.ReadProducts(new[] { targetSheet });
|
var targetProducts = _reader.ReadProducts(new[] { targetSheet });
|
||||||
@ -94,7 +94,7 @@ public class CanWriteProducts : IDisposable
|
|||||||
Worksheet sourceSheet = Util.Workbook.Worksheets[1];
|
Worksheet sourceSheet = Util.Workbook.Worksheets[1];
|
||||||
RhSolutionsAddIn.Configuration.SetPriceListPath(Path.GetFullPath(@"..\..\..\TestWorkbooks\TargetSpecificationNewVariant.xlsx"));
|
RhSolutionsAddIn.Configuration.SetPriceListPath(Path.GetFullPath(@"..\..\..\TestWorkbooks\TargetSpecificationNewVariant.xlsx"));
|
||||||
var products = _reader.ReadProducts(new[] { sourceSheet });
|
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);
|
_writer.WriteProducts(products);
|
||||||
Worksheet targetSheet = Util.Application.ActiveWindow.ActiveSheet;
|
Worksheet targetSheet = Util.Application.ActiveWindow.ActiveSheet;
|
||||||
var targetProducts = _reader.ReadProducts(new[] { targetSheet });
|
var targetProducts = _reader.ReadProducts(new[] { targetSheet });
|
||||||
|
@ -23,7 +23,7 @@ public class RealPricelistTest : IDisposable
|
|||||||
Worksheet sourceSheet = Util.Workbook.Worksheets[1];
|
Worksheet sourceSheet = Util.Workbook.Worksheets[1];
|
||||||
RhSolutionsAddIn.Configuration.SetPriceListPath(Path.GetFullPath(@"..\..\..\TestWorkbooks\RealTargetSpecification.xlsx"));
|
RhSolutionsAddIn.Configuration.SetPriceListPath(Path.GetFullPath(@"..\..\..\TestWorkbooks\RealTargetSpecification.xlsx"));
|
||||||
var products = _reader.ReadProducts(new[] { sourceSheet });
|
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);
|
_writer.WriteProducts(products);
|
||||||
Worksheet targetSheet = Util.Application.ActiveWindow.ActiveSheet;
|
Worksheet targetSheet = Util.Application.ActiveWindow.ActiveSheet;
|
||||||
targetSheet.Range["A1"].Formula = "=SUM(H:H)";
|
targetSheet.Range["A1"].Formula = "=SUM(H:H)";
|
||||||
|
Loading…
Reference in New Issue
Block a user