diff --git a/src/PriceListTools/CombineTool.cs b/src/PriceListTools/CombineTool.cs index 39d3b98..f6eac8a 100644 --- a/src/PriceListTools/CombineTool.cs +++ b/src/PriceListTools/CombineTool.cs @@ -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 SourceFiles; - foreach (var sheet in sourcePriceLists) + public void FillTarget() + { + 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 (sheet.SkuAmount.Count == 0) + 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++; + TargetFile.Sheet.Cells[cell.Row, TargetFile.amountCell.Column - 1].Value2 = kvp.Value; } - - NewPriceList.Sheet.Cells[NewPriceList.amountCell.Row, NewPriceList.amountCell.Column - 1].Value2 = $"{sheet.Name}"; } } - - FilterByAmount(); - AddIn.Excel.StatusBar = $"Экспортировано {exportedValues} строк из {sourcePriceLists.Count} файлов"; - Forms.Dialog.SaveWorkbookAs(); } } } diff --git a/src/PriceListTools/ExportTool.cs b/src/PriceListTools/ExportTool.cs index b36fd13..10d66b4 100644 --- a/src/PriceListTools/ExportTool.cs +++ b/src/PriceListTools/ExportTool.cs @@ -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 SkuAmount { get; set; } private Range Selection; - public ExportTool() + public void TryGetSelection() { - ExcelApp = (Application)ExcelDnaUtil.Application; Selection = ExcelApp.Selection; + + if (Selection == null || Selection.Columns.Count != 2) + { + throw new Exception("Неверный диапазон"); + } } - public override void GetSource() + public void FillTarget() { - if (Selection != null && Selection.Columns.Count == 2) - FillSkuAmountDict(); + ExcelApp.ScreenUpdating = false; + GetSelected(); + FillAmountColumn(new [] {SkuAmount}); + FilterByAmount(); + ExcelApp.ScreenUpdating = true; - else throw new Exception("Неверный диапазон"); + Forms.Dialog.SaveWorkbookAs(); } - public override void GetSourceLists(string[] files) - => GetSource(); - - private void FillSkuAmountDict() + private void GetSelected() { object[,] cells = Selection.Value2; SkuAmount = new Dictionary(); @@ -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(); - } } } diff --git a/src/PriceListTools/MergeTool.cs b/src/PriceListTools/MergeTool.cs index 51884dd..20ace85 100644 --- a/src/PriceListTools/MergeTool.cs +++ b/src/PriceListTools/MergeTool.cs @@ -1,7 +1,20 @@ -namespace RehauSku.PriceListTools +using System.Collections.Generic; +using System.Linq; + +namespace RehauSku.PriceListTools { internal class MergeTool : PriceListTool { + public List SourceFiles; + public void FillTarget() + { + ExcelApp.ScreenUpdating = false; + FillAmountColumn(SourceFiles.Select(x => x.SkuAmount).ToArray()); + FilterByAmount(); + ExcelApp.ScreenUpdating = true; + + Forms.Dialog.SaveWorkbookAs(); + } } } diff --git a/src/PriceListTools/PriceListPosition.cs b/src/PriceListTools/PriceListPosition.cs deleted file mode 100644 index 30be153..0000000 --- a/src/PriceListTools/PriceListPosition.cs +++ /dev/null @@ -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; - } - } - } -} diff --git a/src/PriceListTools/PriceListTool.cs b/src/PriceListTools/PriceListTool.cs index 5ccd31a..09315a2 100644 --- a/src/PriceListTools/PriceListTool.cs +++ b/src/PriceListTools/PriceListTool.cs @@ -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 sourcePriceLists; + protected private Application ExcelApp = (Application)ExcelDnaUtil.Application; + protected private Target TargetFile; - public PriceListTool() + public void OpenNewPrice() { - ExcelApp = (Application)ExcelDnaUtil.Application; - sourcePriceLists = new List(); - } - - 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[] dictionaries) { - throw new NotImplementedException(); - } - - public virtual void GetSourceLists(string[] files) - { - ExcelApp.ScreenUpdating = false; - foreach (string file in files) + foreach (var dictionary in dictionaries) { - 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(); } } } \ No newline at end of file diff --git a/src/PriceListTools/Source.cs b/src/PriceListTools/Source.cs index 40ac257..92de551 100644 --- a/src/PriceListTools/Source.cs +++ b/src/PriceListTools/Source.cs @@ -19,7 +19,7 @@ namespace RehauSku.PriceListTools if (amountCell == null || skuCell == null || groupCell == null) { - throw new ArgumentException($"Лист { Name } не распознан"); + throw new ArgumentException($"Файл {Name} не распознан"); } CreateAmountDict(); diff --git a/src/PriceListTools/SourceUtil.cs b/src/PriceListTools/SourceUtil.cs new file mode 100644 index 0000000..5c575f6 --- /dev/null +++ b/src/PriceListTools/SourceUtil.cs @@ -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 GetSourceLists(string[] files) + { + var ExcelApp = (Application)ExcelDnaUtil.Application; + + List sourceFiles = new List(); + + 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; + } + } +} diff --git a/src/PriceListTools/Target.cs b/src/PriceListTools/Target.cs index b9eb856..7abe9e2 100644 --- a/src/PriceListTools/Target.cs +++ b/src/PriceListTools/Target.cs @@ -1,17 +1,14 @@ using Microsoft.Office.Interop.Excel; using System; -using System.Collections.Generic; namespace RehauSku.PriceListTools { internal class Target : PriceList { - public Dictionary 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 = - //} } } } diff --git a/src/RehauSku.Assist.csproj b/src/RehauSku.Assist.csproj index 74d6df8..8953924 100644 --- a/src/RehauSku.Assist.csproj +++ b/src/RehauSku.Assist.csproj @@ -125,8 +125,8 @@ - + diff --git a/src/Ribbon/RibbonController.cs b/src/Ribbon/RibbonController.cs index d17426b..b00e23a 100644 --- a/src/Ribbon/RibbonController.cs +++ b/src/Ribbon/RibbonController.cs @@ -4,6 +4,7 @@ using ExcelDna.Integration.CustomUI; using RehauSku.PriceListTools; using RehauSku.Forms; using System; +using System.Collections.Generic; namespace RehauSku.Ribbon { @@ -33,35 +34,29 @@ namespace RehauSku.Ribbon "; } - // - public void OnMergePressed(IRibbonControl control) { - using (MergeTool mergeTool = new MergeTool()) + MergeTool mergeTool = new MergeTool(); + string[] files = Dialog.GetMultiplyFiles(); + + if (files.Length != 0) { - string[] files = Dialog.GetMultiplyFiles(); - if (files.Length != 0) - { - mergeTool.GetSourceLists(files); - string exportFile = RegistryUtil.PriceListPath; - mergeTool.OpenNewPrice(exportFile); - mergeTool.FillTarget(); - } + 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) { - string[] files = Dialog.GetMultiplyFiles(); - if (files.Length != 0) - { - combineTool.GetSourceLists(files); - string exportFile = RegistryUtil.PriceListPath; - combineTool.OpenNewPrice(exportFile); - combineTool.FillTarget(); - } + combineTool.SourceFiles = SourceUtil.GetSourceLists(files); + combineTool.OpenNewPrice(); + combineTool.FillTarget(); } } @@ -69,14 +64,12 @@ namespace RehauSku.Ribbon { try { - using (ExportTool exportTool = new ExportTool()) - { - exportTool.GetSource(); - string exportFile = RegistryUtil.PriceListPath; - exportTool.OpenNewPrice(exportFile); - exportTool.FillTarget(); - } + 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)