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 Microsoft.Office.Interop.Excel;
using System; using System.Collections.Generic;
using System.Linq;
namespace RehauSku.PriceListTools namespace RehauSku.PriceListTools
{ {
internal class CombineTool : PriceListTool internal class CombineTool : PriceListTool
{ {
public override void FillTarget() public List<Source> SourceFiles;
{
int exportedValues = 0;
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; continue;
NewPriceList.Sheet.Columns[NewPriceList.amountCell.Column] TargetFile.Sheet.Columns[TargetFile.amountCell.Column]
.EntireColumn .EntireColumn
.Insert(XlInsertShiftDirection.xlShiftToRight, XlInsertFormatOrigin.xlFormatFromRightOrBelow); .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) if (cell == null)
{ {
System.Windows.Forms.MessageBox.Show continue;
($"Артикул {kvp.Key} отсутствует в таблице заказов {RegistryUtil.PriceListPath}",
"Отсутствует позиция в конечной таблице заказов",
System.Windows.Forms.MessageBoxButtons.OK,
System.Windows.Forms.MessageBoxIcon.Information);
} }
else else
{ {
NewPriceList.Sheet.Cells[cell.Row, NewPriceList.amountCell.Column - 1].Value2 = kvp.Value; TargetFile.Sheet.Cells[cell.Row, TargetFile.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}";
} }
} }
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 RehauSku.Assistant;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
@ -11,24 +10,28 @@ namespace RehauSku.PriceListTools
private Dictionary<string, double> SkuAmount { get; set; } private Dictionary<string, double> SkuAmount { get; set; }
private Range Selection; private Range Selection;
public ExportTool() public void TryGetSelection()
{ {
ExcelApp = (Application)ExcelDnaUtil.Application;
Selection = ExcelApp.Selection; Selection = ExcelApp.Selection;
}
public override void GetSource() if (Selection == null || Selection.Columns.Count != 2)
{ {
if (Selection != null && Selection.Columns.Count == 2) throw new Exception("Неверный диапазон");
FillSkuAmountDict(); }
else throw new Exception("Неверный диапазон");
} }
public override void GetSourceLists(string[] files) public void FillTarget()
=> GetSource(); {
ExcelApp.ScreenUpdating = false;
GetSelected();
FillAmountColumn(new [] {SkuAmount});
FilterByAmount();
ExcelApp.ScreenUpdating = true;
private void FillSkuAmountDict() Forms.Dialog.SaveWorkbookAs();
}
private void GetSelected()
{ {
object[,] cells = Selection.Value2; object[,] cells = Selection.Value2;
SkuAmount = new Dictionary<string, double>(); SkuAmount = new Dictionary<string, double>();
@ -72,48 +75,6 @@ namespace RehauSku.PriceListTools
SkuAmount.Add(sku, amount.Value); 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 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 namespace RehauSku.PriceListTools
{ {
internal abstract class PriceListTool : IDisposable internal abstract class PriceListTool
{ {
protected private Application ExcelApp; protected private Application ExcelApp = (Application)ExcelDnaUtil.Application;
protected private Target NewPriceList; protected private Target TargetFile;
protected private List<Source> sourcePriceLists;
public PriceListTool() public void OpenNewPrice()
{ {
ExcelApp = (Application)ExcelDnaUtil.Application; Workbook wb = ExcelApp.Workbooks.Open(RegistryUtil.PriceListPath);
sourcePriceLists = new List<Source>();
}
public void OpenNewPrice(string path)
{
Workbook wb = ExcelApp.Workbooks.Open(path);
try try
{ {
NewPriceList = new Target(wb); TargetFile = new Target(wb);
} }
catch (Exception ex) catch (Exception ex)
@ -34,60 +27,20 @@ namespace RehauSku.PriceListTools
System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxButtons.OK,
System.Windows.Forms.MessageBoxIcon.Information); System.Windows.Forms.MessageBoxIcon.Information);
wb.Close(); wb.Close();
throw ex;
} }
} }
public virtual void GetSource() protected private void FillAmountColumn(Dictionary<string, double>[] dictionaries)
{ {
throw new NotImplementedException(); foreach (var dictionary in dictionaries)
}
public virtual void GetSourceLists(string[] files)
{ {
ExcelApp.ScreenUpdating = false; if (dictionary.Count == 0)
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)
continue; 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) if (cell == null)
{ {
@ -100,14 +53,12 @@ namespace RehauSku.PriceListTools
else 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) if (sumCell.Value2 == null)
sumCell.Value2 = kvp.Value; sumCell.Value2 = kvp.Value;
else else
sumCell.Value2 += kvp.Value; sumCell.Value2 += kvp.Value;
exportedValues++;
} }
} }
} }
@ -115,15 +66,10 @@ namespace RehauSku.PriceListTools
protected private void FilterByAmount() protected private void FilterByAmount()
{ {
AutoFilter filter = NewPriceList.Sheet.AutoFilter; AutoFilter filter = TargetFile.Sheet.AutoFilter;
filter.Range.AutoFilter(NewPriceList.amountCell.Column, "<>"); filter.Range.AutoFilter(TargetFile.amountCell.Column, "<>");
NewPriceList.Sheet.Range["A1"].Activate(); TargetFile.Sheet.Range["A1"].Activate();
}
public void Dispose()
{
GC.SuppressFinalize(this);
} }
} }
} }

View File

@ -19,7 +19,7 @@ namespace RehauSku.PriceListTools
if (amountCell == null || skuCell == null || groupCell == null) if (amountCell == null || skuCell == null || groupCell == null)
{ {
throw new ArgumentException($"Лист { Name } не распознан"); throw new ArgumentException($"Файл {Name} не распознан");
} }
CreateAmountDict(); 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 Microsoft.Office.Interop.Excel;
using System; using System;
using System.Collections.Generic;
namespace RehauSku.PriceListTools namespace RehauSku.PriceListTools
{ {
internal class Target : PriceList internal class Target : PriceList
{ {
public Dictionary<PriceListPosition, Range> Map { get; private set; }
public Target(Workbook workbook) public Target(Workbook workbook)
{ {
Sheet = workbook.ActiveSheet; Sheet = workbook.ActiveSheet;
Name = workbook.Name; Name = workbook.FullName;
amountCell = Sheet.Cells.Find(amountHeader); amountCell = Sheet.Cells.Find(amountHeader);
skuCell = Sheet.Cells.Find(skuHeader); skuCell = Sheet.Cells.Find(skuHeader);
@ -19,27 +16,8 @@ namespace RehauSku.PriceListTools
if (amountCell == null || skuCell == null || groupCell == null) 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\PriceListTool.cs" />
<Compile Include="PriceListTools\MergeTool.cs" /> <Compile Include="PriceListTools\MergeTool.cs" />
<Compile Include="PriceListTools\PriceList.cs" /> <Compile Include="PriceListTools\PriceList.cs" />
<Compile Include="PriceListTools\PriceListPosition.cs" />
<Compile Include="PriceListTools\Source.cs" /> <Compile Include="PriceListTools\Source.cs" />
<Compile Include="PriceListTools\SourceUtil.cs" />
<Compile Include="PriceListTools\Target.cs" /> <Compile Include="PriceListTools\Target.cs" />
<Compile Include="Ribbon\RibbonController.cs" /> <Compile Include="Ribbon\RibbonController.cs" />
<Compile Include="Assistant\HttpClientUtil.cs" /> <Compile Include="Assistant\HttpClientUtil.cs" />

View File

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