From 6e889419e2658a3a80fa00582314f1428f6052e5 Mon Sep 17 00:00:00 2001 From: Sergey Chebotar Date: Fri, 4 Feb 2022 09:17:12 +0300 Subject: [PATCH] Add Result Statusbar message --- src/Interface/AbstractBar.cs | 11 +++ src/Interface/ProgressBar.cs | 9 +-- src/Interface/ResultBar.cs | 44 +++++++++++ src/PriceListTools/AbstractTool.cs | 102 ++++++++++++++++---------- src/PriceListTools/CombineTool.cs | 7 +- src/PriceListTools/ConvertTool.cs | 7 +- src/PriceListTools/ExportTool.cs | 7 +- src/PriceListTools/MergeTool.cs | 7 +- src/PriceListTools/SourcePriceList.cs | 11 ++- src/PriceListTools/TargetPriceList.cs | 5 +- src/RehauSku.Assist.csproj | 2 + 11 files changed, 157 insertions(+), 55 deletions(-) create mode 100644 src/Interface/AbstractBar.cs create mode 100644 src/Interface/ResultBar.cs diff --git a/src/Interface/AbstractBar.cs b/src/Interface/AbstractBar.cs new file mode 100644 index 0000000..c5918a8 --- /dev/null +++ b/src/Interface/AbstractBar.cs @@ -0,0 +1,11 @@ +using Microsoft.Office.Interop.Excel; + +namespace RehauSku.Interface +{ + internal abstract class AbstractBar + { + protected Application Excel = AddIn.Excel; + + public abstract void Update(); + } +} diff --git a/src/Interface/ProgressBar.cs b/src/Interface/ProgressBar.cs index 9a0490b..ed889f6 100644 --- a/src/Interface/ProgressBar.cs +++ b/src/Interface/ProgressBar.cs @@ -1,10 +1,7 @@ -using Microsoft.Office.Interop.Excel; - -namespace RehauSku.Interface +namespace RehauSku.Interface { - internal class ProgressBar + internal class ProgressBar : AbstractBar { - private Application Excel = AddIn.Excel; private double CurrentProgress { get; set; } private readonly double TaskWeight; private readonly string Message; @@ -16,7 +13,7 @@ namespace RehauSku.Interface CurrentProgress = 0; } - public void DoProgress() + public override void Update() { double percent = (++CurrentProgress / TaskWeight) * 100; diff --git a/src/Interface/ResultBar.cs b/src/Interface/ResultBar.cs new file mode 100644 index 0000000..1b4d7f4 --- /dev/null +++ b/src/Interface/ResultBar.cs @@ -0,0 +1,44 @@ +using System.Text; + +namespace RehauSku.Interface +{ + internal class ResultBar : AbstractBar + { + private int Success { get; set; } + private int Replaced { get; set; } + private int NotFound { get; set; } + + public ResultBar() + { + Success = 0; + Replaced = 0; + NotFound = 0; + } + + public void IncrementSuccess() => Success++; + public void IncrementReplaced() => Replaced++; + public void IncrementNotFound() => NotFound++; + + public override void Update() + { + StringBuilder sb = new StringBuilder(); + + if (Success > 0) + { + sb.Append($"Успешно экспортировано {Success} артикулов. "); + } + + if (Replaced > 0) + { + sb.Append($"Заменено {Replaced} артикулов. "); + } + + if (NotFound > 0) + { + sb.Append($"Не найдено {NotFound} артикулов."); + } + + Excel.StatusBar = sb.ToString(); + } + } +} diff --git a/src/PriceListTools/AbstractTool.cs b/src/PriceListTools/AbstractTool.cs index 0d185f9..b4e34cc 100644 --- a/src/PriceListTools/AbstractTool.cs +++ b/src/PriceListTools/AbstractTool.cs @@ -1,9 +1,11 @@ using ExcelDna.Integration; using Microsoft.Office.Interop.Excel; +using RehauSku.Interface; using System; using System.Collections.Generic; using System.Windows.Forms; using Application = Microsoft.Office.Interop.Excel.Application; +using ProgressBar = RehauSku.Interface.ProgressBar; namespace RehauSku.PriceListTools { @@ -11,6 +13,8 @@ namespace RehauSku.PriceListTools { protected private Application ExcelApp = (Application)ExcelDnaUtil.Application; protected private TargetPriceList TargetFile; + protected private ResultBar ResultBar { get; set; } + protected private ProgressBar ProgressBar { get; set; } public void OpenNewPrice() { @@ -53,72 +57,96 @@ namespace RehauSku.PriceListTools sumCell.Value2 += positionAmount.Value; } } + + ResultBar.IncrementSuccess(); + return; } - else + if (TargetFile.oldSkuCell != null) { - string sku = positionAmount.Key.Sku.Substring(1, 6); + Range foundCell = TargetFile.oldSkuCell.EntireColumn.Find(positionAmount.Key.Sku); - row = GetPositionRow(sku, positionAmount.Key.Group, TargetFile.skuCell.Column); - - if (row != null) + if (foundCell != null) { + row = foundCell.Row; + foreach (int column in columns) { - Range amountCell = TargetFile.Sheet.Cells[row, column]; - - if (amountCell.Value2 == null) + if (TargetFile.Sheet.Cells[row, column].Value2 == null) { - amountCell.Value2 = positionAmount.Value; + TargetFile.Sheet.Cells[row, column].Value2 = positionAmount.Value; } else { - amountCell.Value2 += positionAmount.Value; + TargetFile.Sheet.Cells[row, column].Value2 += positionAmount.Value; } + } - Range oldSkuCell = TargetFile.Sheet.Cells[row, TargetFile.oldSkuCell.Column]; - oldSkuCell.Value2 = positionAmount.Key.Sku; + ResultBar.IncrementReplaced(); + return; + } + } + + string sku = positionAmount.Key.Sku.Substring(1, 6); + row = GetPositionRow(sku, positionAmount.Key.Group, TargetFile.skuCell.Column); + + if (row != null) + { + foreach (int column in columns) + { + Range amountCell = TargetFile.Sheet.Cells[row, column]; + + if (amountCell.Value2 == null) + { + amountCell.Value2 = positionAmount.Value; + } + + else + { + amountCell.Value2 += positionAmount.Value; } } - else - { - FillMissing(positionAmount, columns); - } + ResultBar.IncrementReplaced(); + return; + } + + else + { + FillMissing(positionAmount, columns); + ResultBar.IncrementNotFound(); } } protected private void FillMissing(KeyValuePair positionAmount, params int[] columns) { - Range foundCell = TargetFile.oldSkuCell.EntireColumn.Find(positionAmount.Key.Sku); - int row; + int row = TargetFile.Sheet.Cells[TargetFile.Sheet.Rows.Count, TargetFile.skuCell.Column] + .End[XlDirection.xlUp] + .Row + 1; - if (foundCell == null) + TargetFile.Sheet.Rows[row] + .EntireRow + .Insert(XlInsertShiftDirection.xlShiftDown, XlInsertFormatOrigin.xlFormatFromLeftOrAbove); + + Range previous = TargetFile.Sheet.Rows[row - 1]; + Range current = TargetFile.Sheet.Rows[row]; + + previous.Copy(current); + current.ClearContents(); + + TargetFile.Sheet.Cells[row, TargetFile.groupCell.Column].Value2 = positionAmount.Key.Group; + TargetFile.Sheet.Cells[row, TargetFile.nameCell.Column].Value2 = positionAmount.Key.Name; + + if (TargetFile.oldSkuCell != null) { - row = TargetFile.Sheet.Cells[TargetFile.Sheet.Rows.Count, TargetFile.skuCell.Column] - .End[XlDirection.xlUp] - .Row + 1; - - TargetFile.Sheet.Rows[row] - .EntireRow - .Insert(XlInsertShiftDirection.xlShiftDown, XlInsertFormatOrigin.xlFormatFromLeftOrAbove); - - Range previous = TargetFile.Sheet.Rows[row - 1]; - Range current = TargetFile.Sheet.Rows[row]; - - previous.Copy(current); - current.ClearContents(); - - TargetFile.Sheet.Cells[row, TargetFile.groupCell.Column].Value2 = positionAmount.Key.Group; - TargetFile.Sheet.Cells[row, TargetFile.oldSkuCell.Column].Value2 = positionAmount.Key.Sku; - TargetFile.Sheet.Cells[row, TargetFile.nameCell.Column].Value2 = positionAmount.Key.Name; TargetFile.Sheet.Cells[row, TargetFile.skuCell.Column].Value2 = "Не найден"; + TargetFile.Sheet.Cells[row, TargetFile.oldSkuCell.Column].Value2 = positionAmount.Key.Sku; } else { - row = foundCell.Row; + TargetFile.Sheet.Cells[row, TargetFile.skuCell.Column].Value2 = positionAmount.Key.Sku; } foreach (int column in columns) diff --git a/src/PriceListTools/CombineTool.cs b/src/PriceListTools/CombineTool.cs index 6b3dcb2..af9378c 100644 --- a/src/PriceListTools/CombineTool.cs +++ b/src/PriceListTools/CombineTool.cs @@ -11,7 +11,8 @@ namespace RehauSku.PriceListTools public void FillTarget() { - ProgressBar bar = new ProgressBar("Заполняю строки...", SourceFiles.Sum(file => file.PositionAmount.Count)); + ProgressBar = new ProgressBar("Заполняю строки...", SourceFiles.Sum(file => file.PositionAmount.Count)); + ResultBar = new ResultBar(); foreach (SourcePriceList source in SourceFiles) { @@ -26,13 +27,15 @@ namespace RehauSku.PriceListTools foreach (var kvp in source.PositionAmount) { FillPositionAmountToColumns(kvp, TargetFile.amountCell.Column - 1, TargetFile.amountCell.Column); - bar.DoProgress(); + ProgressBar.Update(); } } FilterByAmount(); + ResultBar.Update(); Interface.Dialog.SaveWorkbookAs(); + ExcelApp.StatusBar = false; } } } diff --git a/src/PriceListTools/ConvertTool.cs b/src/PriceListTools/ConvertTool.cs index 3b634eb..d13c803 100644 --- a/src/PriceListTools/ConvertTool.cs +++ b/src/PriceListTools/ConvertTool.cs @@ -27,17 +27,20 @@ namespace RehauSku.PriceListTools public void FillTarget() { - ProgressBar bar = new ProgressBar("Заполняю строки...", Current.PositionAmount.Count); + ProgressBar = new ProgressBar("Заполняю строки...", Current.PositionAmount.Count); + ResultBar = new ResultBar(); foreach (var kvp in Current.PositionAmount) { FillPositionAmountToColumns(kvp, TargetFile.amountCell.Column); - bar.DoProgress(); + ProgressBar.Update(); } FilterByAmount(); + ResultBar.Update(); Dialog.SaveWorkbookAs(); + ExcelApp.StatusBar = false; } } } \ No newline at end of file diff --git a/src/PriceListTools/ExportTool.cs b/src/PriceListTools/ExportTool.cs index 024cb3c..b36bf09 100644 --- a/src/PriceListTools/ExportTool.cs +++ b/src/PriceListTools/ExportTool.cs @@ -24,17 +24,20 @@ namespace RehauSku.PriceListTools public void FillTarget() { GetSelected(); - ProgressBar bar = new ProgressBar("Заполняю строки...", PositionAmount.Count); + ProgressBar = new ProgressBar("Заполняю строки...", PositionAmount.Count); + ResultBar = new ResultBar(); foreach (var kvp in PositionAmount) { FillPositionAmountToColumns(kvp, TargetFile.amountCell.Column); - bar.DoProgress(); + ProgressBar.Update(); } FilterByAmount(); + ResultBar.Update(); Interface.Dialog.SaveWorkbookAs(); + ExcelApp.StatusBar = false; } private void GetSelected() diff --git a/src/PriceListTools/MergeTool.cs b/src/PriceListTools/MergeTool.cs index 8f3f6fc..0e3f1dc 100644 --- a/src/PriceListTools/MergeTool.cs +++ b/src/PriceListTools/MergeTool.cs @@ -10,20 +10,23 @@ namespace RehauSku.PriceListTools public void FillTarget() { - ProgressBar bar = new ProgressBar("Заполняю строки...", SourceFiles.Sum(x => x.PositionAmount.Count)); + ProgressBar = new ProgressBar("Заполняю строки...", SourceFiles.Sum(x => x.PositionAmount.Count)); + ResultBar = new ResultBar(); foreach (SourcePriceList source in SourceFiles) { foreach (var kvp in source.PositionAmount) { FillPositionAmountToColumns(kvp, TargetFile.amountCell.Column); - bar.DoProgress(); + ProgressBar.Update(); } } FilterByAmount(); + ResultBar.Update(); Dialog.SaveWorkbookAs(); + ExcelApp.StatusBar = false; } } } diff --git a/src/PriceListTools/SourcePriceList.cs b/src/PriceListTools/SourcePriceList.cs index ebd4d6a..ec9025d 100644 --- a/src/PriceListTools/SourcePriceList.cs +++ b/src/PriceListTools/SourcePriceList.cs @@ -4,6 +4,7 @@ using System; using System.Collections.Generic; using System.Linq; using RehauSku.Interface; +using RehauSku.Assistant; namespace RehauSku.PriceListTools { @@ -53,7 +54,7 @@ namespace RehauSku.PriceListTools SourcePriceList priceList = new SourcePriceList(wb); sourceFiles.Add(priceList); wb.Close(); - bar.DoProgress(); + bar.Update(); } catch (Exception ex) { @@ -63,7 +64,7 @@ namespace RehauSku.PriceListTools System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Information); wb.Close(); - bar.DoProgress(); + bar.Update(); } ExcelApp.ScreenUpdating = true; } @@ -85,6 +86,12 @@ namespace RehauSku.PriceListTools object name = Sheet.Cells[row, nameCell.Column].Value2; object sku = Sheet.Cells[row, skuCell.Column].Value2; + if (group == null || name == null || sku == null) + continue; + + if (!sku.ToString().IsRehauSku()) + continue; + Position p = new Position(group.ToString(), sku.ToString(), name.ToString()); if (PositionAmount.ContainsKey(p)) diff --git a/src/PriceListTools/TargetPriceList.cs b/src/PriceListTools/TargetPriceList.cs index 2c2f029..32b071c 100644 --- a/src/PriceListTools/TargetPriceList.cs +++ b/src/PriceListTools/TargetPriceList.cs @@ -19,10 +19,11 @@ namespace RehauSku.PriceListTools amountCell = Sheet.Cells.Find(amountHeader), skuCell = Sheet.Cells.Find(skuHeader), groupCell = Sheet.Cells.Find(groupHeader), - nameCell = Sheet.Cells.Find(nameHeader), - oldSkuCell = Sheet.Cells.Find(oldSkuHeader) + nameCell = Sheet.Cells.Find(nameHeader) }; + oldSkuCell = Sheet.Cells.Find(oldSkuHeader); + if (cells.Any(x => x == null)) { throw new ArgumentException($"Шаблон { Name } не является прайс-листом"); diff --git a/src/RehauSku.Assist.csproj b/src/RehauSku.Assist.csproj index 25fa5a5..f4b4695 100644 --- a/src/RehauSku.Assist.csproj +++ b/src/RehauSku.Assist.csproj @@ -115,6 +115,7 @@ + @@ -122,6 +123,7 @@ +