From 4a2ca16d8b4aa34041adb558b2db91709908aff5 Mon Sep 17 00:00:00 2001 From: Sergey Chebotar Date: Wed, 26 Jan 2022 17:41:46 +0300 Subject: [PATCH 01/33] Watch only current sheets in files --- src/PriceListTools/AbstractPriceListTool.cs | 8 +-- src/PriceListTools/CombineTool.cs | 71 ++++++++++----------- src/PriceListTools/ExportTool.cs | 2 +- src/PriceListTools/MergeTool.cs | 45 +++++++------ src/PriceListTools/PriceList.cs | 32 ++++++---- src/PriceListTools/PriceListPosition.cs | 23 +++++++ src/PriceListTools/PriceListSheet.cs | 55 ++++++++++++---- src/RehauSku.Assist.csproj | 1 + 8 files changed, 148 insertions(+), 89 deletions(-) create mode 100644 src/PriceListTools/PriceListPosition.cs diff --git a/src/PriceListTools/AbstractPriceListTool.cs b/src/PriceListTools/AbstractPriceListTool.cs index 1aef0be..65ddb3f 100644 --- a/src/PriceListTools/AbstractPriceListTool.cs +++ b/src/PriceListTools/AbstractPriceListTool.cs @@ -25,11 +25,11 @@ namespace RehauSku.PriceListTools { NewPriceList = new PriceList(wb); - if (NewPriceList.Sheets.Count == 0) - throw new ArgumentException($"Не найдены листы с артикулами в {wb.Name}"); + //if (NewPriceList.Sheet.Count == 0) + // throw new ArgumentException($"Не найдены листы с артикулами в {wb.Name}"); - if (NewPriceList.OfferSheet == null) - throw new ArgumentException($"Нет листа для коммерческого предложения в {wb.Name}"); + //if (NewPriceList.OfferSheet == null) + // throw new ArgumentException($"Нет листа для коммерческого предложения в {wb.Name}"); } catch (Exception ex) diff --git a/src/PriceListTools/CombineTool.cs b/src/PriceListTools/CombineTool.cs index cf02059..59259ac 100644 --- a/src/PriceListTools/CombineTool.cs +++ b/src/PriceListTools/CombineTool.cs @@ -7,7 +7,7 @@ namespace RehauSku.PriceListTools { public override void FillPriceList() { - PriceListSheet offer = NewPriceList.OfferSheet; + PriceListSheet offer = NewPriceList.Sheet; offer.Sheet.Activate(); int exportedValues = 0; @@ -15,45 +15,44 @@ namespace RehauSku.PriceListTools foreach (var priceList in sourcePriceLists) { - foreach (var sheet in priceList.Sheets) + PriceListSheet sheet = priceList.Sheet; + + if (sheet.SkuAmount.Count == 0) + continue; + + offer.Sheet.Columns[offer.amountColumnNumber] + .EntireColumn + .Insert(XlInsertShiftDirection.xlShiftToRight, XlInsertFormatOrigin.xlFormatFromRightOrBelow); + + exportedLists++; + + foreach (var kvp in sheet.SkuAmount) { - if (sheet.SkuAmount.Count == 0) - continue; + Range cell = offer.Sheet.Columns[offer.skuColumnNumber].Find(kvp.Key); - offer.Sheet.Columns[offer.amountColumnNumber] - .EntireColumn - .Insert(XlInsertShiftDirection.xlShiftToRight, XlInsertFormatOrigin.xlFormatFromRightOrBelow); - - exportedLists++; - - foreach (var kvp in sheet.SkuAmount) + if (cell == null) { - Range cell = offer.Sheet.Columns[offer.skuColumnNumber].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 - { - offer.Sheet.Cells[cell.Row, offer.amountColumnNumber].Value2 = kvp.Value; - Range sumCell = offer.Sheet.Cells[cell.Row, offer.amountColumnNumber + exportedLists]; - - if (sumCell.Value2 == null) - sumCell.Value2 = kvp.Value; - else - sumCell.Value2 += kvp.Value; - - exportedValues++; - } - - offer.Sheet.Cells[offer.headerRowNumber, offer.amountColumnNumber].Value2 = $"{priceList.Name}\n{sheet.Name}"; + System.Windows.Forms.MessageBox.Show + ($"Артикул {kvp.Key} отсутствует в таблице заказов {RegistryUtil.PriceListPath}", + "Отсутствует позиция в конечной таблице заказов", + System.Windows.Forms.MessageBoxButtons.OK, + System.Windows.Forms.MessageBoxIcon.Information); } + + else + { + offer.Sheet.Cells[cell.Row, offer.amountColumnNumber].Value2 = kvp.Value; + Range sumCell = offer.Sheet.Cells[cell.Row, offer.amountColumnNumber + exportedLists]; + + if (sumCell.Value2 == null) + sumCell.Value2 = kvp.Value; + else + sumCell.Value2 += kvp.Value; + + exportedValues++; + } + + offer.Sheet.Cells[offer.headerRowNumber, offer.amountColumnNumber].Value2 = $"{priceList.Name}\n{sheet.Name}"; } } diff --git a/src/PriceListTools/ExportTool.cs b/src/PriceListTools/ExportTool.cs index a93097d..9bf165b 100644 --- a/src/PriceListTools/ExportTool.cs +++ b/src/PriceListTools/ExportTool.cs @@ -77,7 +77,7 @@ namespace RehauSku.PriceListTools { if (SkuAmount.Count < 1) return; - PriceListSheet offer = NewPriceList.OfferSheet; + PriceListSheet offer = NewPriceList.Sheet; offer.Sheet.Activate(); int exportedValues = 0; diff --git a/src/PriceListTools/MergeTool.cs b/src/PriceListTools/MergeTool.cs index 493f8a8..4f5b727 100644 --- a/src/PriceListTools/MergeTool.cs +++ b/src/PriceListTools/MergeTool.cs @@ -7,42 +7,41 @@ namespace RehauSku.PriceListTools { public override void FillPriceList() { - PriceListSheet offer = NewPriceList.OfferSheet; + PriceListSheet offer = NewPriceList.Sheet; offer.Sheet.Activate(); int exportedValues = 0; foreach (var priceList in sourcePriceLists) { - foreach (var sheet in priceList.Sheets) + PriceListSheet sheet = priceList.Sheet; + + if (sheet.SkuAmount.Count == 0) + continue; + + foreach (var kvp in sheet.SkuAmount) { - if (sheet.SkuAmount.Count == 0) - continue; + Range cell = offer.Sheet.Columns[offer.skuColumnNumber].Find(kvp.Key); - foreach (var kvp in sheet.SkuAmount) + if (cell == null) { - Range cell = offer.Sheet.Columns[offer.skuColumnNumber].Find(kvp.Key); + System.Windows.Forms.MessageBox.Show + ($"Артикул {kvp.Key} отсутствует в таблице заказов {RegistryUtil.PriceListPath}", + "Отсутствует позиция в конечной таблице заказов", + System.Windows.Forms.MessageBoxButtons.OK, + System.Windows.Forms.MessageBoxIcon.Information); + } - 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 = offer.Sheet.Cells[cell.Row, offer.amountColumnNumber]; + if (sumCell.Value2 == null) + sumCell.Value2 = kvp.Value; else - { - Range sumCell = offer.Sheet.Cells[cell.Row, offer.amountColumnNumber]; + sumCell.Value2 += kvp.Value; - if (sumCell.Value2 == null) - sumCell.Value2 = kvp.Value; - else - sumCell.Value2 += kvp.Value; - - exportedValues++; - } + exportedValues++; } } } diff --git a/src/PriceListTools/PriceList.cs b/src/PriceListTools/PriceList.cs index bc11a17..b8bd7b7 100644 --- a/src/PriceListTools/PriceList.cs +++ b/src/PriceListTools/PriceList.cs @@ -1,4 +1,5 @@ using Microsoft.Office.Interop.Excel; +using System; using System.Collections.Generic; using System.IO; using System.Linq; @@ -8,25 +9,34 @@ namespace RehauSku.PriceListTools internal class PriceList { public readonly string Name; - public readonly PriceListSheet OfferSheet; - public List Sheets { get; private set; } + //public readonly PriceListSheet OfferSheet; + public PriceListSheet Sheet { get; private set; } - private const string offerSheetHeader = "КП"; + + //private const string offerSheetHeader = "КП"; public PriceList(Workbook workbook) { Name = workbook.Name; - Sheets = new List(); + Sheet = new PriceListSheet(workbook.ActiveSheet); - foreach (Worksheet worksheet in workbook.Sheets) - { - PriceListSheet priceListSheet = new PriceListSheet(worksheet); + //foreach (Worksheet worksheet in workbook.Sheets) + //{ + // try + // { + // PriceListSheet priceListSheet = new PriceListSheet(worksheet); + // //priceListSheet.FillSkuAmount(); + // Sheets.Add(priceListSheet); + // } + // catch (Exception ex) + // { + // throw ex; + // } + //} - if (priceListSheet.FillSkuAmount()) - Sheets.Add(priceListSheet); - } - OfferSheet = Sheets.Where(s => s.Name == offerSheetHeader).FirstOrDefault(); + + //OfferSheet = Sheet.Where(s => s.Name == offerSheetHeader).FirstOrDefault(); } public static string CreateNewFile() diff --git a/src/PriceListTools/PriceListPosition.cs b/src/PriceListTools/PriceListPosition.cs new file mode 100644 index 0000000..30be153 --- /dev/null +++ b/src/PriceListTools/PriceListPosition.cs @@ -0,0 +1,23 @@ +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/PriceListSheet.cs b/src/PriceListTools/PriceListSheet.cs index 8a34c2f..9ac2af1 100644 --- a/src/PriceListTools/PriceListSheet.cs +++ b/src/PriceListTools/PriceListSheet.cs @@ -1,5 +1,6 @@ using Microsoft.Office.Interop.Excel; using System.Collections.Generic; +using System; namespace RehauSku.PriceListTools { @@ -7,13 +8,22 @@ namespace RehauSku.PriceListTools { private const string amountHeader = "Кол-во"; private const string skuHeader = "Актуальный материал"; + private const string groupHeader = "Программа"; public readonly Worksheet Sheet; public readonly string Name; public Dictionary SkuAmount { get; private set; } + + Range amountCell { get; set; } + Range skuCell { get; set; } + Range groupCell { get; set; } + public int headerRowNumber { get; private set; } public int amountColumnNumber { get; private set; } public int skuColumnNumber { get; private set; } + public int groupColumnNumber { get; private set; } + public Dictionary Map { get; private set; } + public PriceListSheet(Worksheet sheet) { @@ -21,28 +31,28 @@ namespace RehauSku.PriceListTools Name = sheet.Name; SkuAmount = new Dictionary(); + amountCell = Sheet.Cells.Find(amountHeader); + skuCell = Sheet.Cells.Find(skuHeader); + groupCell = Sheet.Cells.Find(groupHeader); + + if (amountCell == null || skuCell == null || groupCell == null) + { + throw new ArgumentException($"Лист { Name } не распознан"); + } + FillSkuAmount(); } - public bool FillSkuAmount() + private void FillSkuAmount() { - Range amountCell = Sheet.Cells.Find(amountHeader); - Range skuCell = Sheet.Cells.Find(skuHeader); - - if (amountCell == null || skuCell == null) - { - AddIn.Excel.StatusBar = $"Лист {Name} не распознан"; - return false; - } - headerRowNumber = amountCell.Row; skuColumnNumber = skuCell.Column; amountColumnNumber = amountCell.Column; - object[,] amountColumn = Sheet.Columns[amountColumnNumber].Value2; - object[,] skuColumn = Sheet.Columns[skuColumnNumber].Value2; + object[,] amountColumn = Sheet.Columns[amountCell.Column].Value2; + object[,] skuColumn = Sheet.Columns[skuCell.Column].Value2; - for (int row = headerRowNumber + 1; row < amountColumn.GetLength(0); row++) + for (int row = amountCell.Row + 1; row < amountColumn.GetLength(0); row++) { object amount = amountColumn[row, 1]; object sku = skuColumn[row, 1]; @@ -56,8 +66,25 @@ namespace RehauSku.PriceListTools SkuAmount.Add(sku.ToString(), (double)amount); } } - return true; } + + //public 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 2c6792c..d5d7947 100644 --- a/src/RehauSku.Assist.csproj +++ b/src/RehauSku.Assist.csproj @@ -125,6 +125,7 @@ + From ec1059ed5d4c158e29587af8a35bd9b5c783b325 Mon Sep 17 00:00:00 2001 From: Sergey Chebotar Date: Wed, 26 Jan 2022 17:43:09 +0300 Subject: [PATCH 02/33] Remove Create new file method in PriceList class --- src/PriceListTools/PriceList.cs | 9 --------- 1 file changed, 9 deletions(-) diff --git a/src/PriceListTools/PriceList.cs b/src/PriceListTools/PriceList.cs index b8bd7b7..e6aa826 100644 --- a/src/PriceListTools/PriceList.cs +++ b/src/PriceListTools/PriceList.cs @@ -38,15 +38,6 @@ namespace RehauSku.PriceListTools //OfferSheet = Sheet.Where(s => s.Name == offerSheetHeader).FirstOrDefault(); } - - public static string CreateNewFile() - { - string fileExtension = Path.GetExtension(RegistryUtil.PriceListPath); - string path = Path.GetTempFileName() + fileExtension; - - File.Copy(RegistryUtil.PriceListPath, path); - return path; - } } } From c0139ca228bdca91df0a66201f59f8074c6a0db6 Mon Sep 17 00:00:00 2001 From: Sergey Chebotar Date: Wed, 26 Jan 2022 17:50:07 +0300 Subject: [PATCH 03/33] Remove unnecessary fields --- src/PriceListTools/CombineTool.cs | 12 ++++++------ src/PriceListTools/ExportTool.cs | 6 +++--- src/PriceListTools/MergeTool.cs | 6 +++--- src/PriceListTools/PriceList.cs | 22 ---------------------- src/PriceListTools/PriceListSheet.cs | 15 +++------------ 5 files changed, 15 insertions(+), 46 deletions(-) diff --git a/src/PriceListTools/CombineTool.cs b/src/PriceListTools/CombineTool.cs index 59259ac..d285ab0 100644 --- a/src/PriceListTools/CombineTool.cs +++ b/src/PriceListTools/CombineTool.cs @@ -20,7 +20,7 @@ namespace RehauSku.PriceListTools if (sheet.SkuAmount.Count == 0) continue; - offer.Sheet.Columns[offer.amountColumnNumber] + offer.Sheet.Columns[offer.amountCell.Column] .EntireColumn .Insert(XlInsertShiftDirection.xlShiftToRight, XlInsertFormatOrigin.xlFormatFromRightOrBelow); @@ -28,7 +28,7 @@ namespace RehauSku.PriceListTools foreach (var kvp in sheet.SkuAmount) { - Range cell = offer.Sheet.Columns[offer.skuColumnNumber].Find(kvp.Key); + Range cell = offer.Sheet.Columns[offer.skuCell.Column].Find(kvp.Key); if (cell == null) { @@ -41,8 +41,8 @@ namespace RehauSku.PriceListTools else { - offer.Sheet.Cells[cell.Row, offer.amountColumnNumber].Value2 = kvp.Value; - Range sumCell = offer.Sheet.Cells[cell.Row, offer.amountColumnNumber + exportedLists]; + offer.Sheet.Cells[cell.Row, offer.amountCell.Column].Value2 = kvp.Value; + Range sumCell = offer.Sheet.Cells[cell.Row, offer.amountCell.Column + exportedLists]; if (sumCell.Value2 == null) sumCell.Value2 = kvp.Value; @@ -52,14 +52,14 @@ namespace RehauSku.PriceListTools exportedValues++; } - offer.Sheet.Cells[offer.headerRowNumber, offer.amountColumnNumber].Value2 = $"{priceList.Name}\n{sheet.Name}"; + offer.Sheet.Cells[offer.amountCell.Row, offer.amountCell.Column].Value2 = $"{priceList.Name}\n{sheet.Name}"; } } AutoFilter filter = offer.Sheet.AutoFilter; int firstFilterColumn = filter.Range.Column; - filter.Range.AutoFilter(offer.amountColumnNumber - firstFilterColumn + 1 + exportedLists, "<>"); + filter.Range.AutoFilter(offer.amountCell.Column - firstFilterColumn + 1 + exportedLists, "<>"); offer.Sheet.Range["A1"].Activate(); AddIn.Excel.StatusBar = $"Экспортировано {exportedValues} строк из {sourcePriceLists.Count} файлов"; diff --git a/src/PriceListTools/ExportTool.cs b/src/PriceListTools/ExportTool.cs index 9bf165b..76a5c6f 100644 --- a/src/PriceListTools/ExportTool.cs +++ b/src/PriceListTools/ExportTool.cs @@ -84,7 +84,7 @@ namespace RehauSku.PriceListTools foreach (var kvp in SkuAmount) { - Range cell = offer.Sheet.Columns[offer.skuColumnNumber].Find(kvp.Key); + Range cell = offer.Sheet.Columns[offer.skuCell.Column].Find(kvp.Key); if (cell == null) { @@ -97,7 +97,7 @@ namespace RehauSku.PriceListTools else { - Range sumCell = offer.Sheet.Cells[cell.Row, offer.amountColumnNumber]; + Range sumCell = offer.Sheet.Cells[cell.Row, offer.amountCell.Column]; if (sumCell.Value2 == null) sumCell.Value2 = kvp.Value; @@ -111,7 +111,7 @@ namespace RehauSku.PriceListTools AutoFilter filter = offer.Sheet.AutoFilter; int firstFilterColumn = filter.Range.Column; - filter.Range.AutoFilter(offer.amountColumnNumber - firstFilterColumn + 1, "<>"); + filter.Range.AutoFilter(offer.amountCell.Column - firstFilterColumn + 1, "<>"); offer.Sheet.Range["A1"].Activate(); AddIn.Excel.StatusBar = $"Экспортировано {exportedValues} строк из {SkuAmount.Count}"; diff --git a/src/PriceListTools/MergeTool.cs b/src/PriceListTools/MergeTool.cs index 4f5b727..76df6ee 100644 --- a/src/PriceListTools/MergeTool.cs +++ b/src/PriceListTools/MergeTool.cs @@ -21,7 +21,7 @@ namespace RehauSku.PriceListTools foreach (var kvp in sheet.SkuAmount) { - Range cell = offer.Sheet.Columns[offer.skuColumnNumber].Find(kvp.Key); + Range cell = offer.Sheet.Columns[offer.skuCell.Column].Find(kvp.Key); if (cell == null) { @@ -34,7 +34,7 @@ namespace RehauSku.PriceListTools else { - Range sumCell = offer.Sheet.Cells[cell.Row, offer.amountColumnNumber]; + Range sumCell = offer.Sheet.Cells[cell.Row, offer.amountCell.Column]; if (sumCell.Value2 == null) sumCell.Value2 = kvp.Value; @@ -49,7 +49,7 @@ namespace RehauSku.PriceListTools AutoFilter filter = offer.Sheet.AutoFilter; int firstFilterColumn = filter.Range.Column; - filter.Range.AutoFilter(offer.amountColumnNumber - firstFilterColumn + 1, "<>"); + filter.Range.AutoFilter(offer.amountCell.Column - firstFilterColumn + 1, "<>"); offer.Sheet.Range["A1"].Activate(); AddIn.Excel.StatusBar = $"Экспортировано {exportedValues} строк из {sourcePriceLists.Count} файлов"; diff --git a/src/PriceListTools/PriceList.cs b/src/PriceListTools/PriceList.cs index e6aa826..e2fd142 100644 --- a/src/PriceListTools/PriceList.cs +++ b/src/PriceListTools/PriceList.cs @@ -9,34 +9,12 @@ namespace RehauSku.PriceListTools internal class PriceList { public readonly string Name; - //public readonly PriceListSheet OfferSheet; public PriceListSheet Sheet { get; private set; } - - //private const string offerSheetHeader = "КП"; - public PriceList(Workbook workbook) { Name = workbook.Name; Sheet = new PriceListSheet(workbook.ActiveSheet); - - //foreach (Worksheet worksheet in workbook.Sheets) - //{ - // try - // { - // PriceListSheet priceListSheet = new PriceListSheet(worksheet); - // //priceListSheet.FillSkuAmount(); - // Sheets.Add(priceListSheet); - // } - // catch (Exception ex) - // { - // throw ex; - // } - //} - - - - //OfferSheet = Sheet.Where(s => s.Name == offerSheetHeader).FirstOrDefault(); } } } diff --git a/src/PriceListTools/PriceListSheet.cs b/src/PriceListTools/PriceListSheet.cs index 9ac2af1..13bb37c 100644 --- a/src/PriceListTools/PriceListSheet.cs +++ b/src/PriceListTools/PriceListSheet.cs @@ -14,17 +14,12 @@ namespace RehauSku.PriceListTools public readonly string Name; public Dictionary SkuAmount { get; private set; } - Range amountCell { get; set; } - Range skuCell { get; set; } - Range groupCell { get; set; } + public readonly Range amountCell; + public readonly Range skuCell; + public readonly Range groupCell; - public int headerRowNumber { get; private set; } - public int amountColumnNumber { get; private set; } - public int skuColumnNumber { get; private set; } - public int groupColumnNumber { get; private set; } public Dictionary Map { get; private set; } - public PriceListSheet(Worksheet sheet) { Sheet = sheet; @@ -45,10 +40,6 @@ namespace RehauSku.PriceListTools private void FillSkuAmount() { - headerRowNumber = amountCell.Row; - skuColumnNumber = skuCell.Column; - amountColumnNumber = amountCell.Column; - object[,] amountColumn = Sheet.Columns[amountCell.Column].Value2; object[,] skuColumn = Sheet.Columns[skuCell.Column].Value2; From 55bbd801a5593512921ccd4671a50069896affa0 Mon Sep 17 00:00:00 2001 From: Sergey Chebotar Date: Wed, 26 Jan 2022 18:17:44 +0300 Subject: [PATCH 04/33] Remove PriceList class --- src/PriceListTools/AbstractPriceListTool.cs | 6 -- src/PriceListTools/CombineTool.cs | 18 ++-- src/PriceListTools/ExportTool.cs | 2 +- src/PriceListTools/MergeTool.cs | 9 +- src/PriceListTools/PriceList.cs | 91 +++++++++++++++++++-- src/PriceListTools/PriceListSheet.cs | 82 ------------------- src/RehauSku.Assist.csproj | 3 +- 7 files changed, 96 insertions(+), 115 deletions(-) delete mode 100644 src/PriceListTools/PriceListSheet.cs diff --git a/src/PriceListTools/AbstractPriceListTool.cs b/src/PriceListTools/AbstractPriceListTool.cs index 65ddb3f..3511a13 100644 --- a/src/PriceListTools/AbstractPriceListTool.cs +++ b/src/PriceListTools/AbstractPriceListTool.cs @@ -24,12 +24,6 @@ namespace RehauSku.PriceListTools try { NewPriceList = new PriceList(wb); - - //if (NewPriceList.Sheet.Count == 0) - // throw new ArgumentException($"Не найдены листы с артикулами в {wb.Name}"); - - //if (NewPriceList.OfferSheet == null) - // throw new ArgumentException($"Нет листа для коммерческого предложения в {wb.Name}"); } catch (Exception ex) diff --git a/src/PriceListTools/CombineTool.cs b/src/PriceListTools/CombineTool.cs index d285ab0..a9c02a9 100644 --- a/src/PriceListTools/CombineTool.cs +++ b/src/PriceListTools/CombineTool.cs @@ -7,16 +7,13 @@ namespace RehauSku.PriceListTools { public override void FillPriceList() { - PriceListSheet offer = NewPriceList.Sheet; + PriceList offer = NewPriceList; offer.Sheet.Activate(); int exportedValues = 0; - int exportedLists = 0; - foreach (var priceList in sourcePriceLists) + foreach (var sheet in sourcePriceLists) { - PriceListSheet sheet = priceList.Sheet; - if (sheet.SkuAmount.Count == 0) continue; @@ -24,8 +21,6 @@ namespace RehauSku.PriceListTools .EntireColumn .Insert(XlInsertShiftDirection.xlShiftToRight, XlInsertFormatOrigin.xlFormatFromRightOrBelow); - exportedLists++; - foreach (var kvp in sheet.SkuAmount) { Range cell = offer.Sheet.Columns[offer.skuCell.Column].Find(kvp.Key); @@ -41,8 +36,8 @@ namespace RehauSku.PriceListTools else { - offer.Sheet.Cells[cell.Row, offer.amountCell.Column].Value2 = kvp.Value; - Range sumCell = offer.Sheet.Cells[cell.Row, offer.amountCell.Column + exportedLists]; + offer.Sheet.Cells[cell.Row, offer.amountCell.Column - 1].Value2 = kvp.Value; + Range sumCell = offer.Sheet.Cells[cell.Row, offer.amountCell.Column]; if (sumCell.Value2 == null) sumCell.Value2 = kvp.Value; @@ -52,14 +47,13 @@ namespace RehauSku.PriceListTools exportedValues++; } - offer.Sheet.Cells[offer.amountCell.Row, offer.amountCell.Column].Value2 = $"{priceList.Name}\n{sheet.Name}"; + offer.Sheet.Cells[offer.amountCell.Row, offer.amountCell.Column - 1].Value2 = $"{sheet.Name}"; } } AutoFilter filter = offer.Sheet.AutoFilter; - int firstFilterColumn = filter.Range.Column; - filter.Range.AutoFilter(offer.amountCell.Column - firstFilterColumn + 1 + exportedLists, "<>"); + filter.Range.AutoFilter(offer.amountCell.Column, "<>"); offer.Sheet.Range["A1"].Activate(); AddIn.Excel.StatusBar = $"Экспортировано {exportedValues} строк из {sourcePriceLists.Count} файлов"; diff --git a/src/PriceListTools/ExportTool.cs b/src/PriceListTools/ExportTool.cs index 76a5c6f..813df03 100644 --- a/src/PriceListTools/ExportTool.cs +++ b/src/PriceListTools/ExportTool.cs @@ -77,7 +77,7 @@ namespace RehauSku.PriceListTools { if (SkuAmount.Count < 1) return; - PriceListSheet offer = NewPriceList.Sheet; + PriceList offer = NewPriceList; offer.Sheet.Activate(); int exportedValues = 0; diff --git a/src/PriceListTools/MergeTool.cs b/src/PriceListTools/MergeTool.cs index 76df6ee..57f49a0 100644 --- a/src/PriceListTools/MergeTool.cs +++ b/src/PriceListTools/MergeTool.cs @@ -7,15 +7,13 @@ namespace RehauSku.PriceListTools { public override void FillPriceList() { - PriceListSheet offer = NewPriceList.Sheet; + PriceList offer = NewPriceList; offer.Sheet.Activate(); int exportedValues = 0; - foreach (var priceList in sourcePriceLists) + foreach (var sheet in sourcePriceLists) { - PriceListSheet sheet = priceList.Sheet; - if (sheet.SkuAmount.Count == 0) continue; @@ -47,9 +45,8 @@ namespace RehauSku.PriceListTools } AutoFilter filter = offer.Sheet.AutoFilter; - int firstFilterColumn = filter.Range.Column; - filter.Range.AutoFilter(offer.amountCell.Column - firstFilterColumn + 1, "<>"); + filter.Range.AutoFilter(offer.amountCell.Column, "<>"); offer.Sheet.Range["A1"].Activate(); AddIn.Excel.StatusBar = $"Экспортировано {exportedValues} строк из {sourcePriceLists.Count} файлов"; diff --git a/src/PriceListTools/PriceList.cs b/src/PriceListTools/PriceList.cs index e2fd142..588a0bc 100644 --- a/src/PriceListTools/PriceList.cs +++ b/src/PriceListTools/PriceList.cs @@ -1,21 +1,100 @@ using Microsoft.Office.Interop.Excel; -using System; using System.Collections.Generic; -using System.IO; -using System.Linq; +using System; namespace RehauSku.PriceListTools { internal class PriceList { + private const string amountHeader = "Кол-во"; + private const string skuHeader = "Актуальный материал"; + private const string groupHeader = "Программа"; + + public readonly Worksheet Sheet; public readonly string Name; - public PriceListSheet Sheet { get; private set; } + public Dictionary SkuAmount { get; private set; } + + public readonly Range amountCell; + public readonly Range skuCell; + public readonly Range groupCell; + + public Dictionary Map { get; private set; } + + public PriceList(Worksheet sheet) + { + Sheet = sheet; + Name = sheet.Name; + + amountCell = Sheet.Cells.Find(amountHeader); + skuCell = Sheet.Cells.Find(skuHeader); + groupCell = Sheet.Cells.Find(groupHeader); + + if (amountCell == null || skuCell == null || groupCell == null) + { + throw new ArgumentException($"Лист { Name } не распознан"); + } + + FillSkuAmount(); + } public PriceList(Workbook workbook) { - Name = workbook.Name; - Sheet = new PriceListSheet(workbook.ActiveSheet); + Sheet = workbook.ActiveSheet; + Name = workbook.Name + '\n' + Sheet.Name; + + amountCell = Sheet.Cells.Find(amountHeader); + skuCell = Sheet.Cells.Find(skuHeader); + groupCell = Sheet.Cells.Find(groupHeader); + + if (amountCell == null || skuCell == null || groupCell == null) + { + throw new ArgumentException($"Лист { Name } не распознан"); + } + + FillSkuAmount(); } + + private void FillSkuAmount() + { + SkuAmount = new Dictionary(); + + object[,] amountColumn = Sheet.Columns[amountCell.Column].Value2; + object[,] skuColumn = Sheet.Columns[skuCell.Column].Value2; + + for (int row = amountCell.Row + 1; row < amountColumn.GetLength(0); row++) + { + object amount = amountColumn[row, 1]; + object sku = skuColumn[row, 1]; + + if (amount != null && (double)amount != 0) + { + if (SkuAmount.ContainsKey(sku.ToString())) + SkuAmount[sku.ToString()] += (double)amount; + + else + SkuAmount.Add(sku.ToString(), (double)amount); + } + } + } + + //public 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/PriceListTools/PriceListSheet.cs b/src/PriceListTools/PriceListSheet.cs deleted file mode 100644 index 13bb37c..0000000 --- a/src/PriceListTools/PriceListSheet.cs +++ /dev/null @@ -1,82 +0,0 @@ -using Microsoft.Office.Interop.Excel; -using System.Collections.Generic; -using System; - -namespace RehauSku.PriceListTools -{ - internal class PriceListSheet - { - private const string amountHeader = "Кол-во"; - private const string skuHeader = "Актуальный материал"; - private const string groupHeader = "Программа"; - - public readonly Worksheet Sheet; - public readonly string Name; - public Dictionary SkuAmount { get; private set; } - - public readonly Range amountCell; - public readonly Range skuCell; - public readonly Range groupCell; - - public Dictionary Map { get; private set; } - - public PriceListSheet(Worksheet sheet) - { - Sheet = sheet; - Name = sheet.Name; - SkuAmount = new Dictionary(); - - amountCell = Sheet.Cells.Find(amountHeader); - skuCell = Sheet.Cells.Find(skuHeader); - groupCell = Sheet.Cells.Find(groupHeader); - - if (amountCell == null || skuCell == null || groupCell == null) - { - throw new ArgumentException($"Лист { Name } не распознан"); - } - - FillSkuAmount(); - } - - private void FillSkuAmount() - { - object[,] amountColumn = Sheet.Columns[amountCell.Column].Value2; - object[,] skuColumn = Sheet.Columns[skuCell.Column].Value2; - - for (int row = amountCell.Row + 1; row < amountColumn.GetLength(0); row++) - { - object amount = amountColumn[row, 1]; - object sku = skuColumn[row, 1]; - - if (amount != null && (double)amount != 0) - { - if (SkuAmount.ContainsKey(sku.ToString())) - SkuAmount[sku.ToString()] += (double)amount; - - else - SkuAmount.Add(sku.ToString(), (double)amount); - } - } - } - - //public 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 d5d7947..968e8c0 100644 --- a/src/RehauSku.Assist.csproj +++ b/src/RehauSku.Assist.csproj @@ -124,9 +124,8 @@ - - + From 94e0c84ce1e62826d963c0809be1c4d242694444 Mon Sep 17 00:00:00 2001 From: Sergey Chebotar Date: Wed, 26 Jan 2022 18:37:24 +0300 Subject: [PATCH 05/33] Move Autofilter method to abstract class --- src/PriceListTools/AbstractPriceListTool.cs | 8 ++++++++ src/PriceListTools/CombineTool.cs | 20 ++++++-------------- src/PriceListTools/ExportTool.cs | 10 ++-------- src/PriceListTools/MergeTool.cs | 13 +++---------- src/PriceListTools/PriceList.cs | 19 +------------------ 5 files changed, 20 insertions(+), 50 deletions(-) diff --git a/src/PriceListTools/AbstractPriceListTool.cs b/src/PriceListTools/AbstractPriceListTool.cs index 3511a13..b1fd70b 100644 --- a/src/PriceListTools/AbstractPriceListTool.cs +++ b/src/PriceListTools/AbstractPriceListTool.cs @@ -17,6 +17,14 @@ namespace RehauSku.PriceListTools sourcePriceLists = new List(); } + protected private void FilterByAmount() + { + AutoFilter filter = NewPriceList.Sheet.AutoFilter; + + filter.Range.AutoFilter(NewPriceList.amountCell.Column, "<>"); + NewPriceList.Sheet.Range["A1"].Activate(); + } + public void OpenNewPrice(string path) { Workbook wb = ExcelApp.Workbooks.Open(path); diff --git a/src/PriceListTools/CombineTool.cs b/src/PriceListTools/CombineTool.cs index a9c02a9..2e5995c 100644 --- a/src/PriceListTools/CombineTool.cs +++ b/src/PriceListTools/CombineTool.cs @@ -7,9 +7,6 @@ namespace RehauSku.PriceListTools { public override void FillPriceList() { - PriceList offer = NewPriceList; - offer.Sheet.Activate(); - int exportedValues = 0; foreach (var sheet in sourcePriceLists) @@ -17,13 +14,13 @@ namespace RehauSku.PriceListTools if (sheet.SkuAmount.Count == 0) continue; - offer.Sheet.Columns[offer.amountCell.Column] + NewPriceList.Sheet.Columns[NewPriceList.amountCell.Column] .EntireColumn .Insert(XlInsertShiftDirection.xlShiftToRight, XlInsertFormatOrigin.xlFormatFromRightOrBelow); foreach (var kvp in sheet.SkuAmount) { - Range cell = offer.Sheet.Columns[offer.skuCell.Column].Find(kvp.Key); + Range cell = NewPriceList.Sheet.Columns[NewPriceList.skuCell.Column].Find(kvp.Key); if (cell == null) { @@ -36,8 +33,8 @@ namespace RehauSku.PriceListTools else { - offer.Sheet.Cells[cell.Row, offer.amountCell.Column - 1].Value2 = kvp.Value; - Range sumCell = offer.Sheet.Cells[cell.Row, offer.amountCell.Column]; + 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; @@ -47,17 +44,12 @@ namespace RehauSku.PriceListTools exportedValues++; } - offer.Sheet.Cells[offer.amountCell.Row, offer.amountCell.Column - 1].Value2 = $"{sheet.Name}"; + NewPriceList.Sheet.Cells[NewPriceList.amountCell.Row, NewPriceList.amountCell.Column - 1].Value2 = $"{sheet.Name}"; } } - AutoFilter filter = offer.Sheet.AutoFilter; - - filter.Range.AutoFilter(offer.amountCell.Column, "<>"); - offer.Sheet.Range["A1"].Activate(); - + FilterByAmount(); AddIn.Excel.StatusBar = $"Экспортировано {exportedValues} строк из {sourcePriceLists.Count} файлов"; - Forms.Dialog.SaveWorkbookAs(); } diff --git a/src/PriceListTools/ExportTool.cs b/src/PriceListTools/ExportTool.cs index 813df03..5ff52d2 100644 --- a/src/PriceListTools/ExportTool.cs +++ b/src/PriceListTools/ExportTool.cs @@ -107,14 +107,8 @@ namespace RehauSku.PriceListTools exportedValues++; } } - - AutoFilter filter = offer.Sheet.AutoFilter; - int firstFilterColumn = filter.Range.Column; - - filter.Range.AutoFilter(offer.amountCell.Column - firstFilterColumn + 1, "<>"); - offer.Sheet.Range["A1"].Activate(); - AddIn.Excel.StatusBar = $"Экспортировано {exportedValues} строк из {SkuAmount.Count}"; - + FilterByAmount(); + AddIn.Excel.StatusBar = $"Экспортировано {exportedValues} строк из {SkuAmount.Count}"; Forms.Dialog.SaveWorkbookAs(); } diff --git a/src/PriceListTools/MergeTool.cs b/src/PriceListTools/MergeTool.cs index 57f49a0..6440d4b 100644 --- a/src/PriceListTools/MergeTool.cs +++ b/src/PriceListTools/MergeTool.cs @@ -7,9 +7,6 @@ namespace RehauSku.PriceListTools { public override void FillPriceList() { - PriceList offer = NewPriceList; - offer.Sheet.Activate(); - int exportedValues = 0; foreach (var sheet in sourcePriceLists) @@ -19,7 +16,7 @@ namespace RehauSku.PriceListTools foreach (var kvp in sheet.SkuAmount) { - Range cell = offer.Sheet.Columns[offer.skuCell.Column].Find(kvp.Key); + Range cell = NewPriceList.Sheet.Columns[NewPriceList.skuCell.Column].Find(kvp.Key); if (cell == null) { @@ -32,7 +29,7 @@ namespace RehauSku.PriceListTools else { - Range sumCell = offer.Sheet.Cells[cell.Row, offer.amountCell.Column]; + Range sumCell = NewPriceList.Sheet.Cells[cell.Row, NewPriceList.amountCell.Column]; if (sumCell.Value2 == null) sumCell.Value2 = kvp.Value; @@ -44,12 +41,8 @@ namespace RehauSku.PriceListTools } } - AutoFilter filter = offer.Sheet.AutoFilter; - - filter.Range.AutoFilter(offer.amountCell.Column, "<>"); - offer.Sheet.Range["A1"].Activate(); + FilterByAmount(); AddIn.Excel.StatusBar = $"Экспортировано {exportedValues} строк из {sourcePriceLists.Count} файлов"; - Forms.Dialog.SaveWorkbookAs(); } diff --git a/src/PriceListTools/PriceList.cs b/src/PriceListTools/PriceList.cs index 588a0bc..1bf9663 100644 --- a/src/PriceListTools/PriceList.cs +++ b/src/PriceListTools/PriceList.cs @@ -18,24 +18,7 @@ namespace RehauSku.PriceListTools public readonly Range skuCell; public readonly Range groupCell; - public Dictionary Map { get; private set; } - - public PriceList(Worksheet sheet) - { - Sheet = sheet; - Name = sheet.Name; - - amountCell = Sheet.Cells.Find(amountHeader); - skuCell = Sheet.Cells.Find(skuHeader); - groupCell = Sheet.Cells.Find(groupHeader); - - if (amountCell == null || skuCell == null || groupCell == null) - { - throw new ArgumentException($"Лист { Name } не распознан"); - } - - FillSkuAmount(); - } + //public Dictionary Map { get; private set; } public PriceList(Workbook workbook) { From 233c91c71b5c68ed7c51f26731b491dac8423771 Mon Sep 17 00:00:00 2001 From: Sergey Chebotar Date: Wed, 26 Jan 2022 19:03:28 +0300 Subject: [PATCH 06/33] Exception message on wrong files --- src/PriceListTools/AbstractPriceListTool.cs | 24 +++++++++++++++++---- src/PriceListTools/ExportTool.cs | 15 +++++++------ src/PriceListTools/MergeTool.cs | 4 ++++ 3 files changed, 33 insertions(+), 10 deletions(-) diff --git a/src/PriceListTools/AbstractPriceListTool.cs b/src/PriceListTools/AbstractPriceListTool.cs index b1fd70b..c174f30 100644 --- a/src/PriceListTools/AbstractPriceListTool.cs +++ b/src/PriceListTools/AbstractPriceListTool.cs @@ -36,8 +36,12 @@ namespace RehauSku.PriceListTools catch (Exception ex) { + System.Windows.Forms.MessageBox.Show + (ex.Message, + "Ошибка открытия шаблонного прайс-листа", + System.Windows.Forms.MessageBoxButtons.OK, + System.Windows.Forms.MessageBoxIcon.Information); wb.Close(); - throw ex; } } @@ -52,9 +56,21 @@ namespace RehauSku.PriceListTools foreach (string file in files) { Workbook wb = ExcelApp.Workbooks.Open(file); - PriceList priceList = new PriceList(wb); - sourcePriceLists.Add(priceList); - wb.Close(); + try + { + PriceList priceList = new PriceList(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; } diff --git a/src/PriceListTools/ExportTool.cs b/src/PriceListTools/ExportTool.cs index 5ff52d2..2dc673b 100644 --- a/src/PriceListTools/ExportTool.cs +++ b/src/PriceListTools/ExportTool.cs @@ -75,16 +75,16 @@ namespace RehauSku.PriceListTools public override void FillPriceList() { - if (SkuAmount.Count < 1) return; - - PriceList offer = NewPriceList; - offer.Sheet.Activate(); + if (SkuAmount.Count < 1) + return; int exportedValues = 0; + ExcelApp.ScreenUpdating = false; + foreach (var kvp in SkuAmount) { - Range cell = offer.Sheet.Columns[offer.skuCell.Column].Find(kvp.Key); + Range cell = NewPriceList.Sheet.Columns[NewPriceList.skuCell.Column].Find(kvp.Key); if (cell == null) { @@ -97,7 +97,7 @@ namespace RehauSku.PriceListTools else { - Range sumCell = offer.Sheet.Cells[cell.Row, offer.amountCell.Column]; + Range sumCell = NewPriceList.Sheet.Cells[cell.Row, NewPriceList.amountCell.Column]; if (sumCell.Value2 == null) sumCell.Value2 = kvp.Value; @@ -107,7 +107,10 @@ namespace RehauSku.PriceListTools 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 6440d4b..0e98b95 100644 --- a/src/PriceListTools/MergeTool.cs +++ b/src/PriceListTools/MergeTool.cs @@ -9,6 +9,8 @@ namespace RehauSku.PriceListTools { int exportedValues = 0; + ExcelApp.ScreenUpdating = false; + foreach (var sheet in sourcePriceLists) { if (sheet.SkuAmount.Count == 0) @@ -42,6 +44,8 @@ namespace RehauSku.PriceListTools } FilterByAmount(); + ExcelApp.ScreenUpdating = true; + AddIn.Excel.StatusBar = $"Экспортировано {exportedValues} строк из {sourcePriceLists.Count} файлов"; Forms.Dialog.SaveWorkbookAs(); } From 8e3dff1788905c203509f866921957b027cb2643 Mon Sep 17 00:00:00 2001 From: Sergey Chebotar Date: Thu, 27 Jan 2022 09:59:33 +0300 Subject: [PATCH 07/33] Extract PriceList Base Class --- src/PriceListTools/AbstractPriceListTool.cs | 10 +-- src/PriceListTools/PriceList.cs | 83 +++------------------ src/PriceListTools/SourceFile.cs | 74 ++++++++++++++++++ src/RehauSku.Assist.csproj | 3 +- 4 files changed, 90 insertions(+), 80 deletions(-) create mode 100644 src/PriceListTools/SourceFile.cs diff --git a/src/PriceListTools/AbstractPriceListTool.cs b/src/PriceListTools/AbstractPriceListTool.cs index c174f30..437450c 100644 --- a/src/PriceListTools/AbstractPriceListTool.cs +++ b/src/PriceListTools/AbstractPriceListTool.cs @@ -8,13 +8,13 @@ namespace RehauSku.PriceListTools internal abstract class AbstractPriceListTool { protected private Application ExcelApp; - protected private PriceList NewPriceList; - protected private List sourcePriceLists; + protected private SourceFile NewPriceList; + protected private List sourcePriceLists; public AbstractPriceListTool() { ExcelApp = (Application)ExcelDnaUtil.Application; - sourcePriceLists = new List(); + sourcePriceLists = new List(); } protected private void FilterByAmount() @@ -31,7 +31,7 @@ namespace RehauSku.PriceListTools try { - NewPriceList = new PriceList(wb); + NewPriceList = new SourceFile(wb); } catch (Exception ex) @@ -58,7 +58,7 @@ namespace RehauSku.PriceListTools Workbook wb = ExcelApp.Workbooks.Open(file); try { - PriceList priceList = new PriceList(wb); + SourceFile priceList = new SourceFile(wb); sourcePriceLists.Add(priceList); wb.Close(); } diff --git a/src/PriceListTools/PriceList.cs b/src/PriceListTools/PriceList.cs index 1bf9663..5ec870e 100644 --- a/src/PriceListTools/PriceList.cs +++ b/src/PriceListTools/PriceList.cs @@ -1,83 +1,18 @@ using Microsoft.Office.Interop.Excel; -using System.Collections.Generic; -using System; namespace RehauSku.PriceListTools { internal class PriceList { - private const string amountHeader = "Кол-во"; - private const string skuHeader = "Актуальный материал"; - private const string groupHeader = "Программа"; + protected const string amountHeader = "Кол-во"; + protected const string skuHeader = "Актуальный материал"; + protected const string groupHeader = "Программа"; - public readonly Worksheet Sheet; - public readonly string Name; - public Dictionary SkuAmount { get; private set; } + public Range amountCell { get; protected set; } + public Range skuCell { get; protected set; } + public Range groupCell { get; protected set; } - public readonly Range amountCell; - public readonly Range skuCell; - public readonly Range groupCell; - - //public Dictionary Map { get; private set; } - - public PriceList(Workbook workbook) - { - Sheet = workbook.ActiveSheet; - Name = workbook.Name + '\n' + Sheet.Name; - - amountCell = Sheet.Cells.Find(amountHeader); - skuCell = Sheet.Cells.Find(skuHeader); - groupCell = Sheet.Cells.Find(groupHeader); - - if (amountCell == null || skuCell == null || groupCell == null) - { - throw new ArgumentException($"Лист { Name } не распознан"); - } - - FillSkuAmount(); - } - - private void FillSkuAmount() - { - SkuAmount = new Dictionary(); - - object[,] amountColumn = Sheet.Columns[amountCell.Column].Value2; - object[,] skuColumn = Sheet.Columns[skuCell.Column].Value2; - - for (int row = amountCell.Row + 1; row < amountColumn.GetLength(0); row++) - { - object amount = amountColumn[row, 1]; - object sku = skuColumn[row, 1]; - - if (amount != null && (double)amount != 0) - { - if (SkuAmount.ContainsKey(sku.ToString())) - SkuAmount[sku.ToString()] += (double)amount; - - else - SkuAmount.Add(sku.ToString(), (double)amount); - } - } - } - - //public 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 = - // } - - //} + public Worksheet Sheet { get; protected set; } + public string Name { get; protected set; } } - -} - +} \ No newline at end of file diff --git a/src/PriceListTools/SourceFile.cs b/src/PriceListTools/SourceFile.cs new file mode 100644 index 0000000..20d110d --- /dev/null +++ b/src/PriceListTools/SourceFile.cs @@ -0,0 +1,74 @@ +using Microsoft.Office.Interop.Excel; +using System.Collections.Generic; +using System; + +namespace RehauSku.PriceListTools +{ + internal class SourceFile : PriceList + { + public Dictionary SkuAmount { get; private set; } + + public SourceFile(Workbook workbook) + { + Sheet = workbook.ActiveSheet; + Name = workbook.Name + '\n' + Sheet.Name; + + amountCell = Sheet.Cells.Find(amountHeader); + skuCell = Sheet.Cells.Find(skuHeader); + groupCell = Sheet.Cells.Find(groupHeader); + + if (amountCell == null || skuCell == null || groupCell == null) + { + throw new ArgumentException($"Лист { Name } не распознан"); + } + + CreateAmountDict(); + } + + private void CreateAmountDict() + { + SkuAmount = new Dictionary(); + + object[,] amountColumn = Sheet.Columns[amountCell.Column].Value2; + object[,] skuColumn = Sheet.Columns[skuCell.Column].Value2; + + for (int row = amountCell.Row + 1; row < amountColumn.GetLength(0); row++) + { + object amount = amountColumn[row, 1]; + object sku = skuColumn[row, 1]; + + if (amount != null && (double)amount != 0) + { + if (SkuAmount.ContainsKey(sku.ToString())) + SkuAmount[sku.ToString()] += (double)amount; + + else + SkuAmount.Add(sku.ToString(), (double)amount); + } + } + } + } + + internal class NewFile : PriceList + { + public Dictionary Map { get; private set; } + + public 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 968e8c0..4d8c9d5 100644 --- a/src/RehauSku.Assist.csproj +++ b/src/RehauSku.Assist.csproj @@ -124,8 +124,9 @@ - + + From 72ac236b15603e84f18ec346749186b6cb2c2bdf Mon Sep 17 00:00:00 2001 From: Sergey Chebotar Date: Thu, 27 Jan 2022 10:22:30 +0300 Subject: [PATCH 08/33] Refactoring tolls classes --- src/PriceListTools/AbstractPriceListTool.cs | 83 ----------- src/PriceListTools/CombineTool.cs | 9 +- src/PriceListTools/ExportTool.cs | 11 +- src/PriceListTools/MergeTool.cs | 55 +------- src/PriceListTools/PriceListTool.cs | 129 ++++++++++++++++++ .../{SourceFile.cs => Source.cs} | 30 +--- src/PriceListTools/Target.cs | 46 +++++++ src/RehauSku.Assist.csproj | 5 +- src/Ribbon/RibbonController.cs | 10 +- 9 files changed, 194 insertions(+), 184 deletions(-) delete mode 100644 src/PriceListTools/AbstractPriceListTool.cs create mode 100644 src/PriceListTools/PriceListTool.cs rename src/PriceListTools/{SourceFile.cs => Source.cs} (63%) create mode 100644 src/PriceListTools/Target.cs diff --git a/src/PriceListTools/AbstractPriceListTool.cs b/src/PriceListTools/AbstractPriceListTool.cs deleted file mode 100644 index 437450c..0000000 --- a/src/PriceListTools/AbstractPriceListTool.cs +++ /dev/null @@ -1,83 +0,0 @@ -using ExcelDna.Integration; -using Microsoft.Office.Interop.Excel; -using System; -using System.Collections.Generic; - -namespace RehauSku.PriceListTools -{ - internal abstract class AbstractPriceListTool - { - protected private Application ExcelApp; - protected private SourceFile NewPriceList; - protected private List sourcePriceLists; - - public AbstractPriceListTool() - { - ExcelApp = (Application)ExcelDnaUtil.Application; - sourcePriceLists = new List(); - } - - protected private void FilterByAmount() - { - AutoFilter filter = NewPriceList.Sheet.AutoFilter; - - filter.Range.AutoFilter(NewPriceList.amountCell.Column, "<>"); - NewPriceList.Sheet.Range["A1"].Activate(); - } - - public void OpenNewPrice(string path) - { - Workbook wb = ExcelApp.Workbooks.Open(path); - - try - { - NewPriceList = new SourceFile(wb); - } - - catch (Exception ex) - { - System.Windows.Forms.MessageBox.Show - (ex.Message, - "Ошибка открытия шаблонного прайс-листа", - System.Windows.Forms.MessageBoxButtons.OK, - System.Windows.Forms.MessageBoxIcon.Information); - wb.Close(); - } - } - - public virtual void GetSource() - { - throw new NotImplementedException(); - } - - public virtual void GetSource(string[] files) - { - ExcelApp.ScreenUpdating = false; - foreach (string file in files) - { - Workbook wb = ExcelApp.Workbooks.Open(file); - try - { - SourceFile priceList = new SourceFile(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 FillPriceList() - { - throw new NotImplementedException(); - } - } -} \ No newline at end of file diff --git a/src/PriceListTools/CombineTool.cs b/src/PriceListTools/CombineTool.cs index 2e5995c..39d3b98 100644 --- a/src/PriceListTools/CombineTool.cs +++ b/src/PriceListTools/CombineTool.cs @@ -3,9 +3,9 @@ using System; namespace RehauSku.PriceListTools { - internal class CombineTool : AbstractPriceListTool, IDisposable + internal class CombineTool : PriceListTool { - public override void FillPriceList() + public override void FillTarget() { int exportedValues = 0; @@ -52,10 +52,5 @@ namespace RehauSku.PriceListTools AddIn.Excel.StatusBar = $"Экспортировано {exportedValues} строк из {sourcePriceLists.Count} файлов"; Forms.Dialog.SaveWorkbookAs(); } - - public void Dispose() - { - GC.SuppressFinalize(this); - } } } diff --git a/src/PriceListTools/ExportTool.cs b/src/PriceListTools/ExportTool.cs index 2dc673b..b36fd13 100644 --- a/src/PriceListTools/ExportTool.cs +++ b/src/PriceListTools/ExportTool.cs @@ -6,7 +6,7 @@ using System.Collections.Generic; namespace RehauSku.PriceListTools { - internal class ExportTool : AbstractPriceListTool, IDisposable + internal class ExportTool : PriceListTool { private Dictionary SkuAmount { get; set; } private Range Selection; @@ -25,7 +25,7 @@ namespace RehauSku.PriceListTools else throw new Exception("Неверный диапазон"); } - public override void GetSource(string[] files) + public override void GetSourceLists(string[] files) => GetSource(); private void FillSkuAmountDict() @@ -73,7 +73,7 @@ namespace RehauSku.PriceListTools } } - public override void FillPriceList() + public override void FillTarget() { if (SkuAmount.Count < 1) return; @@ -114,11 +114,6 @@ namespace RehauSku.PriceListTools AddIn.Excel.StatusBar = $"Экспортировано {exportedValues} строк из {SkuAmount.Count}"; Forms.Dialog.SaveWorkbookAs(); } - - public void Dispose() - { - GC.SuppressFinalize(this); - } } } diff --git a/src/PriceListTools/MergeTool.cs b/src/PriceListTools/MergeTool.cs index 0e98b95..51884dd 100644 --- a/src/PriceListTools/MergeTool.cs +++ b/src/PriceListTools/MergeTool.cs @@ -1,58 +1,7 @@ -using Microsoft.Office.Interop.Excel; -using System; - -namespace RehauSku.PriceListTools +namespace RehauSku.PriceListTools { - internal class MergeTool : AbstractPriceListTool, IDisposable + internal class MergeTool : PriceListTool { - public override void FillPriceList() - { - int exportedValues = 0; - ExcelApp.ScreenUpdating = false; - - foreach (var sheet in sourcePriceLists) - { - if (sheet.SkuAmount.Count == 0) - continue; - - foreach (var kvp in sheet.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} строк из {sourcePriceLists.Count} файлов"; - Forms.Dialog.SaveWorkbookAs(); - } - - public void Dispose() - { - GC.SuppressFinalize(this); - } } } diff --git a/src/PriceListTools/PriceListTool.cs b/src/PriceListTools/PriceListTool.cs new file mode 100644 index 0000000..5ccd31a --- /dev/null +++ b/src/PriceListTools/PriceListTool.cs @@ -0,0 +1,129 @@ +using ExcelDna.Integration; +using Microsoft.Office.Interop.Excel; +using System; +using System.Collections.Generic; + +namespace RehauSku.PriceListTools +{ + internal abstract class PriceListTool : IDisposable + { + protected private Application ExcelApp; + protected private Target NewPriceList; + protected private List sourcePriceLists; + + public PriceListTool() + { + ExcelApp = (Application)ExcelDnaUtil.Application; + sourcePriceLists = new List(); + } + + public void OpenNewPrice(string path) + { + Workbook wb = ExcelApp.Workbooks.Open(path); + + try + { + NewPriceList = new Target(wb); + } + + catch (Exception ex) + { + System.Windows.Forms.MessageBox.Show + (ex.Message, + "Ошибка открытия шаблонного прайс-листа", + System.Windows.Forms.MessageBoxButtons.OK, + System.Windows.Forms.MessageBoxIcon.Information); + wb.Close(); + } + } + + public virtual void GetSource() + { + throw new NotImplementedException(); + } + + public virtual void GetSourceLists(string[] files) + { + 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) + continue; + + foreach (var kvp in sheet.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++; + } + } + } + } + + protected private void FilterByAmount() + { + AutoFilter filter = NewPriceList.Sheet.AutoFilter; + + filter.Range.AutoFilter(NewPriceList.amountCell.Column, "<>"); + NewPriceList.Sheet.Range["A1"].Activate(); + } + + public void Dispose() + { + GC.SuppressFinalize(this); + } + } +} \ No newline at end of file diff --git a/src/PriceListTools/SourceFile.cs b/src/PriceListTools/Source.cs similarity index 63% rename from src/PriceListTools/SourceFile.cs rename to src/PriceListTools/Source.cs index 20d110d..40ac257 100644 --- a/src/PriceListTools/SourceFile.cs +++ b/src/PriceListTools/Source.cs @@ -1,17 +1,17 @@ using Microsoft.Office.Interop.Excel; -using System.Collections.Generic; using System; +using System.Collections.Generic; namespace RehauSku.PriceListTools { - internal class SourceFile : PriceList + internal class Source : PriceList { public Dictionary SkuAmount { get; private set; } - public SourceFile(Workbook workbook) + public Source(Workbook workbook) { Sheet = workbook.ActiveSheet; - Name = workbook.Name + '\n' + Sheet.Name; + Name = workbook.Name; amountCell = Sheet.Cells.Find(amountHeader); skuCell = Sheet.Cells.Find(skuHeader); @@ -48,27 +48,5 @@ namespace RehauSku.PriceListTools } } } - - internal class NewFile : PriceList - { - public Dictionary Map { get; private set; } - - public 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/PriceListTools/Target.cs b/src/PriceListTools/Target.cs new file mode 100644 index 0000000..b9eb856 --- /dev/null +++ b/src/PriceListTools/Target.cs @@ -0,0 +1,46 @@ +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; + + amountCell = Sheet.Cells.Find(amountHeader); + skuCell = Sheet.Cells.Find(skuHeader); + groupCell = Sheet.Cells.Find(groupHeader); + + if (amountCell == null || skuCell == null || groupCell == null) + { + 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 4d8c9d5..74d6df8 100644 --- a/src/RehauSku.Assist.csproj +++ b/src/RehauSku.Assist.csproj @@ -122,11 +122,12 @@ - + - + + diff --git a/src/Ribbon/RibbonController.cs b/src/Ribbon/RibbonController.cs index ed59541..d17426b 100644 --- a/src/Ribbon/RibbonController.cs +++ b/src/Ribbon/RibbonController.cs @@ -42,10 +42,10 @@ namespace RehauSku.Ribbon string[] files = Dialog.GetMultiplyFiles(); if (files.Length != 0) { - mergeTool.GetSource(files); + mergeTool.GetSourceLists(files); string exportFile = RegistryUtil.PriceListPath; mergeTool.OpenNewPrice(exportFile); - mergeTool.FillPriceList(); + mergeTool.FillTarget(); } } } @@ -57,10 +57,10 @@ namespace RehauSku.Ribbon string[] files = Dialog.GetMultiplyFiles(); if (files.Length != 0) { - combineTool.GetSource(files); + combineTool.GetSourceLists(files); string exportFile = RegistryUtil.PriceListPath; combineTool.OpenNewPrice(exportFile); - combineTool.FillPriceList(); + combineTool.FillTarget(); } } } @@ -74,7 +74,7 @@ namespace RehauSku.Ribbon exportTool.GetSource(); string exportFile = RegistryUtil.PriceListPath; exportTool.OpenNewPrice(exportFile); - exportTool.FillPriceList(); + exportTool.FillTarget(); } } catch (Exception ex) From 935d48fc5fe264218b39b335e1fc5232af5dae61 Mon Sep 17 00:00:00 2001 From: Sergey Chebotar Date: Thu, 27 Jan 2022 17:34:03 +0300 Subject: [PATCH 09/33] Complete tools refactoring --- src/PriceListTools/CombineTool.cs | 54 +++++++--------- src/PriceListTools/ExportTool.cs | 69 +++++--------------- src/PriceListTools/MergeTool.cs | 15 ++++- src/PriceListTools/PriceListPosition.cs | 23 ------- src/PriceListTools/PriceListTool.cs | 86 +++++-------------------- src/PriceListTools/Source.cs | 2 +- src/PriceListTools/SourceUtil.cs | 41 ++++++++++++ src/PriceListTools/Target.cs | 26 +------- src/RehauSku.Assist.csproj | 2 +- src/Ribbon/RibbonController.cs | 48 ++++++-------- 10 files changed, 135 insertions(+), 231 deletions(-) delete mode 100644 src/PriceListTools/PriceListPosition.cs create mode 100644 src/PriceListTools/SourceUtil.cs 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) From 7bb0a82ffbd5c5123bfdbff6ba29f463ab9d1b46 Mon Sep 17 00:00:00 2001 From: Sergey Chebotar Date: Thu, 27 Jan 2022 20:43:19 +0300 Subject: [PATCH 10/33] Add FillColumn method --- src/PriceListTools/CombineTool.cs | 35 +++++++--------------------- src/PriceListTools/ExportTool.cs | 8 ++++++- src/PriceListTools/MergeTool.cs | 7 +++++- src/PriceListTools/PriceListTool.cs | 36 ++++++++++++++--------------- 4 files changed, 39 insertions(+), 47 deletions(-) diff --git a/src/PriceListTools/CombineTool.cs b/src/PriceListTools/CombineTool.cs index f6eac8a..c634309 100644 --- a/src/PriceListTools/CombineTool.cs +++ b/src/PriceListTools/CombineTool.cs @@ -11,42 +11,23 @@ namespace RehauSku.PriceListTools 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) + foreach (Source source in SourceFiles) { - if (source.SkuAmount.Count == 0) - continue; - TargetFile.Sheet.Columns[TargetFile.amountCell.Column] .EntireColumn .Insert(XlInsertShiftDirection.xlShiftToRight, XlInsertFormatOrigin.xlFormatFromRightOrBelow); TargetFile.Sheet.Cells[TargetFile.amountCell.Row, TargetFile.amountCell.Column - 1].Value2 = $"{source.Name}"; - foreach (var kvp in source.SkuAmount) - { - Range cell = TargetFile.Sheet.Columns[TargetFile.skuCell.Column].Find(kvp.Key); - - if (cell == null) - { - continue; - } - - else - { - TargetFile.Sheet.Cells[cell.Row, TargetFile.amountCell.Column - 1].Value2 = kvp.Value; - } - } + FillColumn(source.SkuAmount, TargetFile.amountCell.Column - 1); + FillColumn(source.SkuAmount, TargetFile.amountCell.Column); } + + FilterByAmount(); + ExcelApp.ScreenUpdating = true; + + Forms.Dialog.SaveWorkbookAs(); } } } diff --git a/src/PriceListTools/ExportTool.cs b/src/PriceListTools/ExportTool.cs index 10d66b4..fc9e42d 100644 --- a/src/PriceListTools/ExportTool.cs +++ b/src/PriceListTools/ExportTool.cs @@ -24,7 +24,7 @@ namespace RehauSku.PriceListTools { ExcelApp.ScreenUpdating = false; GetSelected(); - FillAmountColumn(new [] {SkuAmount}); + FillColumn(SkuAmount, TargetFile.amountCell.Column); FilterByAmount(); ExcelApp.ScreenUpdating = true; @@ -67,12 +67,18 @@ namespace RehauSku.PriceListTools } if (sku == null || amount == null) + { continue; + } if (SkuAmount.ContainsKey(sku)) + { SkuAmount[sku] += amount.Value; + } else + { SkuAmount.Add(sku, amount.Value); + } } } } diff --git a/src/PriceListTools/MergeTool.cs b/src/PriceListTools/MergeTool.cs index 20ace85..75804a0 100644 --- a/src/PriceListTools/MergeTool.cs +++ b/src/PriceListTools/MergeTool.cs @@ -10,7 +10,12 @@ namespace RehauSku.PriceListTools public void FillTarget() { ExcelApp.ScreenUpdating = false; - FillAmountColumn(SourceFiles.Select(x => x.SkuAmount).ToArray()); + + foreach (Source source in SourceFiles) + { + FillColumn(source.SkuAmount, TargetFile.amountCell.Column); + } + FilterByAmount(); ExcelApp.ScreenUpdating = true; diff --git a/src/PriceListTools/PriceListTool.cs b/src/PriceListTools/PriceListTool.cs index 09315a2..99eef17 100644 --- a/src/PriceListTools/PriceListTool.cs +++ b/src/PriceListTools/PriceListTool.cs @@ -31,37 +31,37 @@ namespace RehauSku.PriceListTools } } - protected private void FillAmountColumn(Dictionary[] dictionaries) + protected private void FillColumn(Dictionary dictionary, int column) { - foreach (var dictionary in dictionaries) + foreach (var kvp in dictionary) { - if (dictionary.Count == 0) - continue; + Range cell = TargetFile.Sheet.Columns[TargetFile.skuCell.Column].Find(kvp.Key); - foreach (var kvp in dictionary) + if (cell == null) { - Range cell = TargetFile.Sheet.Columns[TargetFile.skuCell.Column].Find(kvp.Key); + System.Windows.Forms.MessageBox.Show + ($"Артикул {kvp.Key} отсутствует в таблице заказов {RegistryUtil.PriceListPath}", + "Отсутствует позиция в конечной таблице заказов", + System.Windows.Forms.MessageBoxButtons.OK, + System.Windows.Forms.MessageBoxIcon.Information); + } - if (cell == null) + else + { + Range sumCell = TargetFile.Sheet.Cells[cell.Row, column]; + + if (sumCell.Value2 == null) { - System.Windows.Forms.MessageBox.Show - ($"Артикул {kvp.Key} отсутствует в таблице заказов {RegistryUtil.PriceListPath}", - "Отсутствует позиция в конечной таблице заказов", - System.Windows.Forms.MessageBoxButtons.OK, - System.Windows.Forms.MessageBoxIcon.Information); + sumCell.Value2 = kvp.Value; } else { - Range sumCell = TargetFile.Sheet.Cells[cell.Row, TargetFile.amountCell.Column]; - - if (sumCell.Value2 == null) - sumCell.Value2 = kvp.Value; - else - sumCell.Value2 += kvp.Value; + sumCell.Value2 += kvp.Value; } } } + } protected private void FilterByAmount() From 71fcd9ee866cb3b1a221dbd691d78ff4b26f23e4 Mon Sep 17 00:00:00 2001 From: Sergey Chebotar Date: Fri, 28 Jan 2022 08:51:47 +0300 Subject: [PATCH 11/33] Add missing sku list, change missing messagebox --- src/PriceListTools/PriceListTool.cs | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/src/PriceListTools/PriceListTool.cs b/src/PriceListTools/PriceListTool.cs index 99eef17..cbae4d9 100644 --- a/src/PriceListTools/PriceListTool.cs +++ b/src/PriceListTools/PriceListTool.cs @@ -2,6 +2,7 @@ using Microsoft.Office.Interop.Excel; using System; using System.Collections.Generic; +using System.Linq; namespace RehauSku.PriceListTools { @@ -33,17 +34,15 @@ namespace RehauSku.PriceListTools protected private void FillColumn(Dictionary dictionary, int column) { + List> missing = new List>(); + foreach (var kvp in dictionary) { 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); + missing.Add(kvp); } else @@ -62,6 +61,12 @@ namespace RehauSku.PriceListTools } } + string values = string.Join("\n", missing.Select(kvp => kvp.Key).ToArray()); + System.Windows.Forms.MessageBox.Show + ($"{missing.Count} артикулов отсутствует в таблице заказов {RegistryUtil.PriceListPath}", + "Отсутствует позиция в конечной таблице заказов", + System.Windows.Forms.MessageBoxButtons.OK, + System.Windows.Forms.MessageBoxIcon.Information); } protected private void FilterByAmount() From 722de64dadd29744972ec5c5eca228f7a2e326e5 Mon Sep 17 00:00:00 2001 From: Sergey Chebotar Date: Fri, 28 Jan 2022 09:08:35 +0300 Subject: [PATCH 12/33] Add convert Tool --- src/PriceListTools/PriceListTool.cs | 21 +++++++++++++++++++++ src/Ribbon/RibbonController.cs | 12 +++++++++++- 2 files changed, 32 insertions(+), 1 deletion(-) diff --git a/src/PriceListTools/PriceListTool.cs b/src/PriceListTools/PriceListTool.cs index cbae4d9..918b6de 100644 --- a/src/PriceListTools/PriceListTool.cs +++ b/src/PriceListTools/PriceListTool.cs @@ -6,6 +6,27 @@ using System.Linq; namespace RehauSku.PriceListTools { + internal class ConvertTool : PriceListTool + { + private Source Current; + + public void GetCurrent() + { + Current = new Source(ExcelApp.ActiveWorkbook); + } + + public void FillTarget() + { + ExcelApp.ScreenUpdating = false; + FillColumn(Current.SkuAmount, TargetFile.amountCell.Column); + FilterByAmount(); + ExcelApp.ScreenUpdating = true; + + Forms.Dialog.SaveWorkbookAs(); + } + } + + internal abstract class PriceListTool { protected private Application ExcelApp = (Application)ExcelDnaUtil.Application; diff --git a/src/Ribbon/RibbonController.cs b/src/Ribbon/RibbonController.cs index b00e23a..99f44df 100644 --- a/src/Ribbon/RibbonController.cs +++ b/src/Ribbon/RibbonController.cs @@ -20,13 +20,14 @@ namespace RehauSku.Ribbon