From 120eee0231d02e90d9d195ebc38327a58d4564a8 Mon Sep 17 00:00:00 2001 From: Sergey Chebotar Date: Wed, 2 Feb 2022 10:23:50 +0300 Subject: [PATCH] Add Excel statusbar progress message --- src/Interface/ProgressBar.cs | 32 +++++++++++++++++++++++++++++++ src/PriceListTools/CombineTool.cs | 9 ++++++++- src/PriceListTools/ConvertTool.cs | 5 +++++ src/PriceListTools/ExportTool.cs | 5 ++++- src/PriceListTools/MergeTool.cs | 6 ++++++ src/PriceListTools/Source.cs | 4 ++++ src/RehauSku.Assist.csproj | 1 + 7 files changed, 60 insertions(+), 2 deletions(-) create mode 100644 src/Interface/ProgressBar.cs diff --git a/src/Interface/ProgressBar.cs b/src/Interface/ProgressBar.cs new file mode 100644 index 0000000..474bed7 --- /dev/null +++ b/src/Interface/ProgressBar.cs @@ -0,0 +1,32 @@ +using Microsoft.Office.Interop.Excel; + +namespace RehauSku.Interface +{ + internal class ProgressBar + { + private Application Excel = AddIn.Excel; + private double CurrentProgress { get; set; } + private readonly double TaskWeight; + + public ProgressBar(int weight) + { + TaskWeight = weight; + CurrentProgress = 0; + } + + public void DoProgress() + { + double percent = (++CurrentProgress / TaskWeight) * 100; + + if (percent < 100) + { + Excel.StatusBar = $"Выполнено {percent.ToString("#.##")} %"; + } + + else + { + Excel.StatusBar = false; + } + } + } +} diff --git a/src/PriceListTools/CombineTool.cs b/src/PriceListTools/CombineTool.cs index e637fef..cb47379 100644 --- a/src/PriceListTools/CombineTool.cs +++ b/src/PriceListTools/CombineTool.cs @@ -1,5 +1,7 @@ using Microsoft.Office.Interop.Excel; using System.Collections.Generic; +using RehauSku.Interface; +using System.Linq; namespace RehauSku.PriceListTools { @@ -9,6 +11,8 @@ namespace RehauSku.PriceListTools public void FillTarget() { + ProgressBar bar = new ProgressBar(SourceFiles.Sum(file => file.PositionAmount.Count)); + foreach (Source source in SourceFiles) { TargetFile.Sheet.Columns[TargetFile.amountCell.Column] @@ -19,8 +23,11 @@ namespace RehauSku.PriceListTools newColumnHeader.Value2 = $"{source.Name}"; newColumnHeader.WrapText = true; - foreach(var kvp in source.PositionAmount) + foreach (var kvp in source.PositionAmount) + { FillColumnsWithDictionary(kvp, TargetFile.amountCell.Column - 1, TargetFile.amountCell.Column); + bar.DoProgress(); + } } FilterByAmount(); diff --git a/src/PriceListTools/ConvertTool.cs b/src/PriceListTools/ConvertTool.cs index d43b30d..d10d65e 100644 --- a/src/PriceListTools/ConvertTool.cs +++ b/src/PriceListTools/ConvertTool.cs @@ -27,8 +27,13 @@ namespace RehauSku.PriceListTools public void FillTarget() { + ProgressBar bar = new ProgressBar(Current.PositionAmount.Count); + foreach (var kvp in Current.PositionAmount) + { FillColumnsWithDictionary(kvp, TargetFile.amountCell.Column); + bar.DoProgress(); + } FilterByAmount(); diff --git a/src/PriceListTools/ExportTool.cs b/src/PriceListTools/ExportTool.cs index 119a289..568145d 100644 --- a/src/PriceListTools/ExportTool.cs +++ b/src/PriceListTools/ExportTool.cs @@ -2,6 +2,7 @@ using RehauSku.Assistant; using System; using System.Collections.Generic; +using RehauSku.Interface; namespace RehauSku.PriceListTools { @@ -23,10 +24,12 @@ namespace RehauSku.PriceListTools public void FillTarget() { GetSelected(); - + ProgressBar bar = new ProgressBar(PositionAmount.Count); + foreach (var kvp in PositionAmount) { FillColumnsWithDictionary(kvp, TargetFile.amountCell.Column); + bar.DoProgress(); } FilterByAmount(); diff --git a/src/PriceListTools/MergeTool.cs b/src/PriceListTools/MergeTool.cs index 895740c..a51b9b7 100644 --- a/src/PriceListTools/MergeTool.cs +++ b/src/PriceListTools/MergeTool.cs @@ -1,5 +1,6 @@ using RehauSku.Interface; using System.Collections.Generic; +using System.Linq; namespace RehauSku.PriceListTools { @@ -9,10 +10,15 @@ namespace RehauSku.PriceListTools public void FillTarget() { + ProgressBar bar = new ProgressBar(SourceFiles.Sum(x => x.PositionAmount.Count)); + foreach (Source source in SourceFiles) { foreach (var kvp in source.PositionAmount) + { FillColumnsWithDictionary(kvp, TargetFile.amountCell.Column); + bar.DoProgress(); + } } FilterByAmount(); diff --git a/src/PriceListTools/Source.cs b/src/PriceListTools/Source.cs index 7cf56be..fe5ee9b 100644 --- a/src/PriceListTools/Source.cs +++ b/src/PriceListTools/Source.cs @@ -3,6 +3,7 @@ using Microsoft.Office.Interop.Excel; using System; using System.Collections.Generic; using System.Linq; +using RehauSku.Interface; namespace RehauSku.PriceListTools { @@ -39,6 +40,7 @@ namespace RehauSku.PriceListTools public static List GetSourceLists(string[] files) { var ExcelApp = (Application)ExcelDnaUtil.Application; + ProgressBar bar = new ProgressBar(files.Length); List sourceFiles = new List(); @@ -51,6 +53,7 @@ namespace RehauSku.PriceListTools Source priceList = new Source(wb); sourceFiles.Add(priceList); wb.Close(); + bar.DoProgress(); } catch (Exception ex) { @@ -60,6 +63,7 @@ namespace RehauSku.PriceListTools System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Information); wb.Close(); + bar.DoProgress(); } ExcelApp.ScreenUpdating = true; } diff --git a/src/RehauSku.Assist.csproj b/src/RehauSku.Assist.csproj index f1a7725..e2d9794 100644 --- a/src/RehauSku.Assist.csproj +++ b/src/RehauSku.Assist.csproj @@ -121,6 +121,7 @@ +