Complete tools refactoring

This commit is contained in:
Sergey Chebotar 2022-01-27 17:34:03 +03:00
parent 72ac236b15
commit 935d48fc5f
10 changed files with 135 additions and 231 deletions

View File

@ -1,56 +1,52 @@
using Microsoft.Office.Interop.Excel;
using System;
using System.Collections.Generic;
using System.Linq;
namespace RehauSku.PriceListTools
{
internal class CombineTool : PriceListTool
{
public override void FillTarget()
{
int exportedValues = 0;
public List<Source> SourceFiles;
foreach (var sheet in sourcePriceLists)
public void FillTarget()
{
if (sheet.SkuAmount.Count == 0)
ExcelApp.ScreenUpdating = false;
FillAmountColumn(SourceFiles.Select(x => x.SkuAmount).ToArray());
AddAndFillSourceColumns();
FilterByAmount();
ExcelApp.ScreenUpdating = true;
Forms.Dialog.SaveWorkbookAs();
}
private void AddAndFillSourceColumns()
{
foreach (var source in SourceFiles)
{
if (source.SkuAmount.Count == 0)
continue;
NewPriceList.Sheet.Columns[NewPriceList.amountCell.Column]
TargetFile.Sheet.Columns[TargetFile.amountCell.Column]
.EntireColumn
.Insert(XlInsertShiftDirection.xlShiftToRight, XlInsertFormatOrigin.xlFormatFromRightOrBelow);
foreach (var kvp in sheet.SkuAmount)
TargetFile.Sheet.Cells[TargetFile.amountCell.Row, TargetFile.amountCell.Column - 1].Value2 = $"{source.Name}";
foreach (var kvp in source.SkuAmount)
{
Range cell = NewPriceList.Sheet.Columns[NewPriceList.skuCell.Column].Find(kvp.Key);
Range cell = TargetFile.Sheet.Columns[TargetFile.skuCell.Column].Find(kvp.Key);
if (cell == null)
{
System.Windows.Forms.MessageBox.Show
($"Артикул {kvp.Key} отсутствует в таблице заказов {RegistryUtil.PriceListPath}",
"Отсутствует позиция в конечной таблице заказов",
System.Windows.Forms.MessageBoxButtons.OK,
System.Windows.Forms.MessageBoxIcon.Information);
continue;
}
else
{
NewPriceList.Sheet.Cells[cell.Row, NewPriceList.amountCell.Column - 1].Value2 = kvp.Value;
Range sumCell = NewPriceList.Sheet.Cells[cell.Row, NewPriceList.amountCell.Column];
if (sumCell.Value2 == null)
sumCell.Value2 = kvp.Value;
else
sumCell.Value2 += kvp.Value;
exportedValues++;
}
NewPriceList.Sheet.Cells[NewPriceList.amountCell.Row, NewPriceList.amountCell.Column - 1].Value2 = $"{sheet.Name}";
TargetFile.Sheet.Cells[cell.Row, TargetFile.amountCell.Column - 1].Value2 = kvp.Value;
}
}
}
FilterByAmount();
AddIn.Excel.StatusBar = $"Экспортировано {exportedValues} строк из {sourcePriceLists.Count} файлов";
Forms.Dialog.SaveWorkbookAs();
}
}
}

View File

@ -1,5 +1,4 @@
using ExcelDna.Integration;
using Microsoft.Office.Interop.Excel;
using Microsoft.Office.Interop.Excel;
using RehauSku.Assistant;
using System;
using System.Collections.Generic;
@ -11,24 +10,28 @@ namespace RehauSku.PriceListTools
private Dictionary<string, double> SkuAmount { get; set; }
private Range Selection;
public ExportTool()
public void TryGetSelection()
{
ExcelApp = (Application)ExcelDnaUtil.Application;
Selection = ExcelApp.Selection;
}
public override void GetSource()
if (Selection == null || Selection.Columns.Count != 2)
{
if (Selection != null && Selection.Columns.Count == 2)
FillSkuAmountDict();
else throw new Exception("Неверный диапазон");
throw new Exception("Неверный диапазон");
}
}
public override void GetSourceLists(string[] files)
=> GetSource();
public void FillTarget()
{
ExcelApp.ScreenUpdating = false;
GetSelected();
FillAmountColumn(new [] {SkuAmount});
FilterByAmount();
ExcelApp.ScreenUpdating = true;
private void FillSkuAmountDict()
Forms.Dialog.SaveWorkbookAs();
}
private void GetSelected()
{
object[,] cells = Selection.Value2;
SkuAmount = new Dictionary<string, double>();
@ -72,48 +75,6 @@ namespace RehauSku.PriceListTools
SkuAmount.Add(sku, amount.Value);
}
}
public override void FillTarget()
{
if (SkuAmount.Count < 1)
return;
int exportedValues = 0;
ExcelApp.ScreenUpdating = false;
foreach (var kvp in SkuAmount)
{
Range cell = NewPriceList.Sheet.Columns[NewPriceList.skuCell.Column].Find(kvp.Key);
if (cell == null)
{
System.Windows.Forms.MessageBox.Show
($"Артикул {kvp.Key} отсутствует в таблице заказов {RegistryUtil.PriceListPath}",
"Отсутствует позиция в конечной таблице заказов",
System.Windows.Forms.MessageBoxButtons.OK,
System.Windows.Forms.MessageBoxIcon.Information);
}
else
{
Range sumCell = NewPriceList.Sheet.Cells[cell.Row, NewPriceList.amountCell.Column];
if (sumCell.Value2 == null)
sumCell.Value2 = kvp.Value;
else
sumCell.Value2 += kvp.Value;
exportedValues++;
}
}
FilterByAmount();
ExcelApp.ScreenUpdating = true;
AddIn.Excel.StatusBar = $"Экспортировано {exportedValues} строк из {SkuAmount.Count}";
Forms.Dialog.SaveWorkbookAs();
}
}
}

View File

@ -1,7 +1,20 @@
namespace RehauSku.PriceListTools
using System.Collections.Generic;
using System.Linq;
namespace RehauSku.PriceListTools
{
internal class MergeTool : PriceListTool
{
public List<Source> SourceFiles;
public void FillTarget()
{
ExcelApp.ScreenUpdating = false;
FillAmountColumn(SourceFiles.Select(x => x.SkuAmount).ToArray());
FilterByAmount();
ExcelApp.ScreenUpdating = true;
Forms.Dialog.SaveWorkbookAs();
}
}
}

View File

@ -1,23 +0,0 @@
using RehauSku.Assistant;
using System;
namespace RehauSku.PriceListTools
{
internal class PriceListPosition
{
public readonly string Group;
public readonly string Sku;
public PriceListPosition(string group, string sku)
{
if (!sku.IsRehauSku())
throw new ArgumentException("Wrong SKU");
else
{
Group = group;
Sku = sku;
}
}
}
}

View File

@ -5,25 +5,18 @@ using System.Collections.Generic;
namespace RehauSku.PriceListTools
{
internal abstract class PriceListTool : IDisposable
internal abstract class PriceListTool
{
protected private Application ExcelApp;
protected private Target NewPriceList;
protected private List<Source> sourcePriceLists;
protected private Application ExcelApp = (Application)ExcelDnaUtil.Application;
protected private Target TargetFile;
public PriceListTool()
public void OpenNewPrice()
{
ExcelApp = (Application)ExcelDnaUtil.Application;
sourcePriceLists = new List<Source>();
}
public void OpenNewPrice(string path)
{
Workbook wb = ExcelApp.Workbooks.Open(path);
Workbook wb = ExcelApp.Workbooks.Open(RegistryUtil.PriceListPath);
try
{
NewPriceList = new Target(wb);
TargetFile = new Target(wb);
}
catch (Exception ex)
@ -34,60 +27,20 @@ namespace RehauSku.PriceListTools
System.Windows.Forms.MessageBoxButtons.OK,
System.Windows.Forms.MessageBoxIcon.Information);
wb.Close();
throw ex;
}
}
public virtual void GetSource()
protected private void FillAmountColumn(Dictionary<string, double>[] dictionaries)
{
throw new NotImplementedException();
}
public virtual void GetSourceLists(string[] files)
foreach (var dictionary in dictionaries)
{
ExcelApp.ScreenUpdating = false;
foreach (string file in files)
{
Workbook wb = ExcelApp.Workbooks.Open(file);
try
{
Source priceList = new Source(wb);
sourcePriceLists.Add(priceList);
wb.Close();
}
catch (Exception ex)
{
System.Windows.Forms.MessageBox.Show
(ex.Message,
"Ошибка открытия исходного прайс-листа",
System.Windows.Forms.MessageBoxButtons.OK,
System.Windows.Forms.MessageBoxIcon.Information);
wb.Close();
}
}
ExcelApp.ScreenUpdating = true;
}
public virtual void FillTarget()
{
ExcelApp.ScreenUpdating = false;
FillAmountColumn();
FilterByAmount();
ExcelApp.ScreenUpdating = true;
Forms.Dialog.SaveWorkbookAs();
}
protected private void FillAmountColumn()
{
int exportedValues = 0;
foreach (var sheet in sourcePriceLists)
{
if (sheet.SkuAmount.Count == 0)
if (dictionary.Count == 0)
continue;
foreach (var kvp in sheet.SkuAmount)
foreach (var kvp in dictionary)
{
Range cell = NewPriceList.Sheet.Columns[NewPriceList.skuCell.Column].Find(kvp.Key);
Range cell = TargetFile.Sheet.Columns[TargetFile.skuCell.Column].Find(kvp.Key);
if (cell == null)
{
@ -100,14 +53,12 @@ namespace RehauSku.PriceListTools
else
{
Range sumCell = NewPriceList.Sheet.Cells[cell.Row, NewPriceList.amountCell.Column];
Range sumCell = TargetFile.Sheet.Cells[cell.Row, TargetFile.amountCell.Column];
if (sumCell.Value2 == null)
sumCell.Value2 = kvp.Value;
else
sumCell.Value2 += kvp.Value;
exportedValues++;
}
}
}
@ -115,15 +66,10 @@ namespace RehauSku.PriceListTools
protected private void FilterByAmount()
{
AutoFilter filter = NewPriceList.Sheet.AutoFilter;
AutoFilter filter = TargetFile.Sheet.AutoFilter;
filter.Range.AutoFilter(NewPriceList.amountCell.Column, "<>");
NewPriceList.Sheet.Range["A1"].Activate();
}
public void Dispose()
{
GC.SuppressFinalize(this);
filter.Range.AutoFilter(TargetFile.amountCell.Column, "<>");
TargetFile.Sheet.Range["A1"].Activate();
}
}
}

View File

@ -19,7 +19,7 @@ namespace RehauSku.PriceListTools
if (amountCell == null || skuCell == null || groupCell == null)
{
throw new ArgumentException($"Лист { Name } не распознан");
throw new ArgumentException($"Файл {Name} не распознан");
}
CreateAmountDict();

View File

@ -0,0 +1,41 @@
using ExcelDna.Integration;
using Microsoft.Office.Interop.Excel;
using System;
using System.Collections.Generic;
namespace RehauSku.PriceListTools
{
internal static class SourceUtil
{
public static List<Source> GetSourceLists(string[] files)
{
var ExcelApp = (Application)ExcelDnaUtil.Application;
List<Source> sourceFiles = new List<Source>();
ExcelApp.ScreenUpdating = false;
foreach (string file in files)
{
Workbook wb = ExcelApp.Workbooks.Open(file);
try
{
Source priceList = new Source(wb);
sourceFiles.Add(priceList);
wb.Close();
}
catch (Exception ex)
{
System.Windows.Forms.MessageBox.Show
(ex.Message,
"Ошибка открытия исходного прайс-листа",
System.Windows.Forms.MessageBoxButtons.OK,
System.Windows.Forms.MessageBoxIcon.Information);
wb.Close();
}
}
ExcelApp.ScreenUpdating = true;
return sourceFiles;
}
}
}

View File

@ -1,17 +1,14 @@
using Microsoft.Office.Interop.Excel;
using System;
using System.Collections.Generic;
namespace RehauSku.PriceListTools
{
internal class Target : PriceList
{
public Dictionary<PriceListPosition, Range> Map { get; private set; }
public Target(Workbook workbook)
{
Sheet = workbook.ActiveSheet;
Name = workbook.Name;
Name = workbook.FullName;
amountCell = Sheet.Cells.Find(amountHeader);
skuCell = Sheet.Cells.Find(skuHeader);
@ -19,27 +16,8 @@ namespace RehauSku.PriceListTools
if (amountCell == null || skuCell == null || groupCell == null)
{
throw new ArgumentException($"Лист { Name } не распознан");
throw new ArgumentException($"Шаблон { Name } не является прайс-листом");
}
CreateMap();
}
private void CreateMap()
{
Range amountCell = Sheet.Cells.Find(amountHeader);
Range skuCell = Sheet.Cells.Find(skuHeader);
Range groupCell = Sheet.Cells.Find(groupHeader);
//headerRowNumber = amountCell.Row;
//skuColumnNumber = skuCell.Column;
//amountColumnNumber = amountCell.Column;
//groupColumnNumber = groupCell.Column;
//for (int row = headerRowNumber + 1; row < skuCell.Rows.Count; row++)
//{
// string sku =
//}
}
}
}

View File

@ -125,8 +125,8 @@
<Compile Include="PriceListTools\PriceListTool.cs" />
<Compile Include="PriceListTools\MergeTool.cs" />
<Compile Include="PriceListTools\PriceList.cs" />
<Compile Include="PriceListTools\PriceListPosition.cs" />
<Compile Include="PriceListTools\Source.cs" />
<Compile Include="PriceListTools\SourceUtil.cs" />
<Compile Include="PriceListTools\Target.cs" />
<Compile Include="Ribbon\RibbonController.cs" />
<Compile Include="Assistant\HttpClientUtil.cs" />

View File

@ -4,6 +4,7 @@ using ExcelDna.Integration.CustomUI;
using RehauSku.PriceListTools;
using RehauSku.Forms;
using System;
using System.Collections.Generic;
namespace RehauSku.Ribbon
{
@ -33,50 +34,42 @@ namespace RehauSku.Ribbon
</customUI>";
}
// <dropDown id = 'dd1' label = 'Drop dynamic' getItemCount = 'fncGetItemCountDrop' getItemLabel = 'fncGetItemLabelDrop' onAction = 'fncOnActionDrop'/>
public void OnMergePressed(IRibbonControl control)
{
using (MergeTool mergeTool = new MergeTool())
{
MergeTool mergeTool = new MergeTool();
string[] files = Dialog.GetMultiplyFiles();
if (files.Length != 0)
{
mergeTool.GetSourceLists(files);
string exportFile = RegistryUtil.PriceListPath;
mergeTool.OpenNewPrice(exportFile);
mergeTool.SourceFiles = SourceUtil.GetSourceLists(files);
mergeTool.OpenNewPrice();
mergeTool.FillTarget();
}
}
}
public void OnCombinePressed(IRibbonControl control)
{
using (CombineTool combineTool = new CombineTool())
{
CombineTool combineTool = new CombineTool();
string[] files = Dialog.GetMultiplyFiles();
if (files.Length != 0)
{
combineTool.GetSourceLists(files);
string exportFile = RegistryUtil.PriceListPath;
combineTool.OpenNewPrice(exportFile);
combineTool.SourceFiles = SourceUtil.GetSourceLists(files);
combineTool.OpenNewPrice();
combineTool.FillTarget();
}
}
}
public void OnExportPressed(IRibbonControl control)
{
try
{
using (ExportTool exportTool = new ExportTool())
{
exportTool.GetSource();
string exportFile = RegistryUtil.PriceListPath;
exportTool.OpenNewPrice(exportFile);
ExportTool exportTool = new ExportTool();
exportTool.TryGetSelection();
exportTool.OpenNewPrice();
exportTool.FillTarget();
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message,
@ -85,7 +78,6 @@ namespace RehauSku.Ribbon
MessageBoxIcon.Information);
return;
}
}
public void OnSetPricePressed(IRibbonControl control)