From 06799119fb83cb6b75721c5cf60f4051e50976a7 Mon Sep 17 00:00:00 2001 From: Sergey Chebotar Date: Wed, 2 Feb 2022 09:46:47 +0300 Subject: [PATCH 01/19] Add Interface namespace --- src/AddIn/RegistryUtil.cs | 2 +- src/{Forms => Interface}/Dialog.cs | 2 +- src/{Ribbon => Interface}/RibbonController.cs | 3 +-- src/PriceListTools/CombineTool.cs | 2 +- src/PriceListTools/ConvertTool.cs | 5 +++-- src/PriceListTools/ExportTool.cs | 2 +- src/PriceListTools/MergeTool.cs | 5 +++-- src/RehauSku.Assist.csproj | 4 ++-- 8 files changed, 13 insertions(+), 12 deletions(-) rename src/{Forms => Interface}/Dialog.cs (98%) rename src/{Ribbon => Interface}/RibbonController.cs (98%) diff --git a/src/AddIn/RegistryUtil.cs b/src/AddIn/RegistryUtil.cs index 3ec6f6a..dabd74d 100644 --- a/src/AddIn/RegistryUtil.cs +++ b/src/AddIn/RegistryUtil.cs @@ -1,5 +1,5 @@ using Microsoft.Win32; -using RehauSku.Forms; +using RehauSku.Interface; using System; using System.IO; using System.Windows.Forms; diff --git a/src/Forms/Dialog.cs b/src/Interface/Dialog.cs similarity index 98% rename from src/Forms/Dialog.cs rename to src/Interface/Dialog.cs index cc1c29a..c888703 100644 --- a/src/Forms/Dialog.cs +++ b/src/Interface/Dialog.cs @@ -2,7 +2,7 @@ using System.Collections.Generic; using System.Windows.Forms; -namespace RehauSku.Forms +namespace RehauSku.Interface { static class Dialog { diff --git a/src/Ribbon/RibbonController.cs b/src/Interface/RibbonController.cs similarity index 98% rename from src/Ribbon/RibbonController.cs rename to src/Interface/RibbonController.cs index 7a514bd..cd7058d 100644 --- a/src/Ribbon/RibbonController.cs +++ b/src/Interface/RibbonController.cs @@ -1,11 +1,10 @@ using ExcelDna.Integration.CustomUI; -using RehauSku.Forms; using RehauSku.PriceListTools; using System; using System.Runtime.InteropServices; using System.Windows.Forms; -namespace RehauSku.Ribbon +namespace RehauSku.Interface { [ComVisible(true)] public class RibbonController : ExcelRibbon diff --git a/src/PriceListTools/CombineTool.cs b/src/PriceListTools/CombineTool.cs index da29e4d..e637fef 100644 --- a/src/PriceListTools/CombineTool.cs +++ b/src/PriceListTools/CombineTool.cs @@ -25,7 +25,7 @@ namespace RehauSku.PriceListTools FilterByAmount(); - Forms.Dialog.SaveWorkbookAs(); + Interface.Dialog.SaveWorkbookAs(); } } } diff --git a/src/PriceListTools/ConvertTool.cs b/src/PriceListTools/ConvertTool.cs index f37bfe2..d43b30d 100644 --- a/src/PriceListTools/ConvertTool.cs +++ b/src/PriceListTools/ConvertTool.cs @@ -1,4 +1,5 @@ -using System; +using RehauSku.Interface; +using System; namespace RehauSku.PriceListTools { @@ -31,7 +32,7 @@ namespace RehauSku.PriceListTools FilterByAmount(); - Forms.Dialog.SaveWorkbookAs(); + Dialog.SaveWorkbookAs(); } } } \ No newline at end of file diff --git a/src/PriceListTools/ExportTool.cs b/src/PriceListTools/ExportTool.cs index bfb3d8a..119a289 100644 --- a/src/PriceListTools/ExportTool.cs +++ b/src/PriceListTools/ExportTool.cs @@ -31,7 +31,7 @@ namespace RehauSku.PriceListTools FilterByAmount(); - Forms.Dialog.SaveWorkbookAs(); + Interface.Dialog.SaveWorkbookAs(); } private void GetSelected() diff --git a/src/PriceListTools/MergeTool.cs b/src/PriceListTools/MergeTool.cs index e196b3a..895740c 100644 --- a/src/PriceListTools/MergeTool.cs +++ b/src/PriceListTools/MergeTool.cs @@ -1,4 +1,5 @@ -using System.Collections.Generic; +using RehauSku.Interface; +using System.Collections.Generic; namespace RehauSku.PriceListTools { @@ -16,7 +17,7 @@ namespace RehauSku.PriceListTools FilterByAmount(); - Forms.Dialog.SaveWorkbookAs(); + Dialog.SaveWorkbookAs(); } } } diff --git a/src/RehauSku.Assist.csproj b/src/RehauSku.Assist.csproj index b2e14b7..f1a7725 100644 --- a/src/RehauSku.Assist.csproj +++ b/src/RehauSku.Assist.csproj @@ -115,7 +115,7 @@ - + @@ -129,7 +129,7 @@ - + From 120eee0231d02e90d9d195ebc38327a58d4564a8 Mon Sep 17 00:00:00 2001 From: Sergey Chebotar Date: Wed, 2 Feb 2022 10:23:50 +0300 Subject: [PATCH 02/19] 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 @@ + From 7f3487d9132bf9b9e83a202c0ce04230926d56fd Mon Sep 17 00:00:00 2001 From: Sergey Chebotar Date: Wed, 2 Feb 2022 17:58:54 +0300 Subject: [PATCH 03/19] Change Excel SaveAs to Windows Forms SaveFileDialog --- src/Interface/Dialog.cs | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/src/Interface/Dialog.cs b/src/Interface/Dialog.cs index c888703..6b05dc8 100644 --- a/src/Interface/Dialog.cs +++ b/src/Interface/Dialog.cs @@ -46,17 +46,22 @@ namespace RehauSku.Interface public static void SaveWorkbookAs() { - Workbook wb = AddIn.Excel.ActiveWorkbook; - string currentFilename = wb.FullName; - string fileFilter = "Файлы Excel (*.xls;*.xlsx;*.xlsm),*.xls;*.xlsx;*.xlsm"; + Workbook workbook = AddIn.Excel.ActiveWorkbook; - object fileName = AddIn.Excel.GetSaveAsFilename(currentFilename, fileFilter); + using (SaveFileDialog dialog = new SaveFileDialog()) + { + dialog.FileName = workbook.Name; + dialog.Filter = "Файлы Excel (*.xls;*.xlsx;*.xlsm)|*.xls;*.xlsx;*.xlsm"; - if (fileName.GetType() == typeof(string)) - wb.SaveAs(fileName); + if (dialog.ShowDialog() == DialogResult.Cancel) + { + workbook.Close(false); + } - else - wb.Close(false); + string fileName = dialog.FileName; + + workbook.SaveAs(fileName); + } } } } From cb5fee18918b5a29ccff23a0fb74bb13151f2e42 Mon Sep 17 00:00:00 2001 From: Sergey Chebotar Date: Wed, 2 Feb 2022 18:02:17 +0300 Subject: [PATCH 04/19] Rename Abstract tool --- src/PriceListTools/{PriceListTool.cs => AbstractTool.cs} | 2 +- src/PriceListTools/CombineTool.cs | 2 +- src/PriceListTools/ConvertTool.cs | 2 +- src/PriceListTools/ExportTool.cs | 2 +- src/PriceListTools/MergeTool.cs | 2 +- 5 files changed, 5 insertions(+), 5 deletions(-) rename src/PriceListTools/{PriceListTool.cs => AbstractTool.cs} (99%) diff --git a/src/PriceListTools/PriceListTool.cs b/src/PriceListTools/AbstractTool.cs similarity index 99% rename from src/PriceListTools/PriceListTool.cs rename to src/PriceListTools/AbstractTool.cs index 0a82a41..77dc63c 100644 --- a/src/PriceListTools/PriceListTool.cs +++ b/src/PriceListTools/AbstractTool.cs @@ -7,7 +7,7 @@ using Application = Microsoft.Office.Interop.Excel.Application; namespace RehauSku.PriceListTools { - internal abstract class PriceListTool + internal abstract class AbstractTool { protected private Application ExcelApp = (Application)ExcelDnaUtil.Application; protected private Target TargetFile; diff --git a/src/PriceListTools/CombineTool.cs b/src/PriceListTools/CombineTool.cs index cb47379..3222dae 100644 --- a/src/PriceListTools/CombineTool.cs +++ b/src/PriceListTools/CombineTool.cs @@ -5,7 +5,7 @@ using System.Linq; namespace RehauSku.PriceListTools { - internal class CombineTool : PriceListTool + internal class CombineTool : AbstractTool { public List SourceFiles; diff --git a/src/PriceListTools/ConvertTool.cs b/src/PriceListTools/ConvertTool.cs index d10d65e..7308be9 100644 --- a/src/PriceListTools/ConvertTool.cs +++ b/src/PriceListTools/ConvertTool.cs @@ -3,7 +3,7 @@ using System; namespace RehauSku.PriceListTools { - internal class ConvertTool : PriceListTool + internal class ConvertTool : AbstractTool { private Source Current; diff --git a/src/PriceListTools/ExportTool.cs b/src/PriceListTools/ExportTool.cs index 568145d..562c1b2 100644 --- a/src/PriceListTools/ExportTool.cs +++ b/src/PriceListTools/ExportTool.cs @@ -6,7 +6,7 @@ using RehauSku.Interface; namespace RehauSku.PriceListTools { - internal class ExportTool : PriceListTool + internal class ExportTool : AbstractTool { private Dictionary PositionAmount; private Range Selection; diff --git a/src/PriceListTools/MergeTool.cs b/src/PriceListTools/MergeTool.cs index a51b9b7..d31cc8a 100644 --- a/src/PriceListTools/MergeTool.cs +++ b/src/PriceListTools/MergeTool.cs @@ -4,7 +4,7 @@ using System.Linq; namespace RehauSku.PriceListTools { - internal class MergeTool : PriceListTool + internal class MergeTool : AbstractTool { public List SourceFiles; From 8f9cc1a46247fb3304340618068a7e3498d2630e Mon Sep 17 00:00:00 2001 From: Sergey Chebotar Date: Wed, 2 Feb 2022 18:02:35 +0300 Subject: [PATCH 05/19] Rename Abstract tool --- src/RehauSku.Assist.csproj | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/RehauSku.Assist.csproj b/src/RehauSku.Assist.csproj index e2d9794..94ac5c0 100644 --- a/src/RehauSku.Assist.csproj +++ b/src/RehauSku.Assist.csproj @@ -125,7 +125,7 @@ - + From bf2e187e6625ca4d19a6d48000843c9813a3efa5 Mon Sep 17 00:00:00 2001 From: Sergey Chebotar Date: Wed, 2 Feb 2022 18:03:49 +0300 Subject: [PATCH 06/19] Make base PriceList class abstract --- src/PriceListTools/{PriceList.cs => AbstractPriceList.cs} | 2 +- src/PriceListTools/Source.cs | 2 +- src/PriceListTools/Target.cs | 2 +- src/RehauSku.Assist.csproj | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) rename src/PriceListTools/{PriceList.cs => AbstractPriceList.cs} (93%) diff --git a/src/PriceListTools/PriceList.cs b/src/PriceListTools/AbstractPriceList.cs similarity index 93% rename from src/PriceListTools/PriceList.cs rename to src/PriceListTools/AbstractPriceList.cs index 65ff3df..06427a0 100644 --- a/src/PriceListTools/PriceList.cs +++ b/src/PriceListTools/AbstractPriceList.cs @@ -2,7 +2,7 @@ namespace RehauSku.PriceListTools { - internal class PriceList + internal abstract class AbstractPriceList { protected const string amountHeader = "Кол-во"; protected const string skuHeader = "Актуальный материал"; diff --git a/src/PriceListTools/Source.cs b/src/PriceListTools/Source.cs index fe5ee9b..a20ad81 100644 --- a/src/PriceListTools/Source.cs +++ b/src/PriceListTools/Source.cs @@ -7,7 +7,7 @@ using RehauSku.Interface; namespace RehauSku.PriceListTools { - internal class Source : PriceList + internal class Source : AbstractPriceList { public Dictionary PositionAmount { get; private set; } diff --git a/src/PriceListTools/Target.cs b/src/PriceListTools/Target.cs index 996a092..ac91544 100644 --- a/src/PriceListTools/Target.cs +++ b/src/PriceListTools/Target.cs @@ -4,7 +4,7 @@ using System.Linq; namespace RehauSku.PriceListTools { - internal class Target : PriceList + internal class Target : AbstractPriceList { private const string oldSkuHeader = "Прежний материал"; public Range oldSkuCell { get; private set; } diff --git a/src/RehauSku.Assist.csproj b/src/RehauSku.Assist.csproj index 94ac5c0..6edbc2c 100644 --- a/src/RehauSku.Assist.csproj +++ b/src/RehauSku.Assist.csproj @@ -127,7 +127,7 @@ - + From 1dfbfaa461479a14cd75c6fc5f6a5082e61c6371 Mon Sep 17 00:00:00 2001 From: Sergey Chebotar Date: Wed, 2 Feb 2022 18:05:49 +0300 Subject: [PATCH 07/19] Rename Abstract PriceList inheritors --- src/Interface/RibbonController.cs | 4 ++-- src/PriceListTools/AbstractTool.cs | 4 ++-- src/PriceListTools/CombineTool.cs | 4 ++-- src/PriceListTools/ConvertTool.cs | 4 ++-- src/PriceListTools/MergeTool.cs | 4 ++-- src/PriceListTools/{Source.cs => SourcePriceList.cs} | 10 +++++----- src/PriceListTools/{Target.cs => TargetPriceList.cs} | 4 ++-- src/RehauSku.Assist.csproj | 4 ++-- 8 files changed, 19 insertions(+), 19 deletions(-) rename src/PriceListTools/{Source.cs => SourcePriceList.cs} (90%) rename src/PriceListTools/{Target.cs => TargetPriceList.cs} (89%) diff --git a/src/Interface/RibbonController.cs b/src/Interface/RibbonController.cs index cd7058d..2b1fd6b 100644 --- a/src/Interface/RibbonController.cs +++ b/src/Interface/RibbonController.cs @@ -40,7 +40,7 @@ namespace RehauSku.Interface if (files.Length != 0) { - mergeTool.SourceFiles = Source.GetSourceLists(files); + mergeTool.SourceFiles = SourcePriceList.GetSourceLists(files); mergeTool.OpenNewPrice(); mergeTool.FillTarget(); } @@ -53,7 +53,7 @@ namespace RehauSku.Interface if (files.Length != 0) { - combineTool.SourceFiles = Source.GetSourceLists(files); + combineTool.SourceFiles = SourcePriceList.GetSourceLists(files); combineTool.OpenNewPrice(); combineTool.FillTarget(); } diff --git a/src/PriceListTools/AbstractTool.cs b/src/PriceListTools/AbstractTool.cs index 77dc63c..a86fd95 100644 --- a/src/PriceListTools/AbstractTool.cs +++ b/src/PriceListTools/AbstractTool.cs @@ -10,7 +10,7 @@ namespace RehauSku.PriceListTools internal abstract class AbstractTool { protected private Application ExcelApp = (Application)ExcelDnaUtil.Application; - protected private Target TargetFile; + protected private TargetPriceList TargetFile; public void OpenNewPrice() { @@ -18,7 +18,7 @@ namespace RehauSku.PriceListTools try { - TargetFile = new Target(wb); + TargetFile = new TargetPriceList(wb); } catch (Exception ex) diff --git a/src/PriceListTools/CombineTool.cs b/src/PriceListTools/CombineTool.cs index 3222dae..80c7a67 100644 --- a/src/PriceListTools/CombineTool.cs +++ b/src/PriceListTools/CombineTool.cs @@ -7,13 +7,13 @@ namespace RehauSku.PriceListTools { internal class CombineTool : AbstractTool { - public List SourceFiles; + public List SourceFiles; public void FillTarget() { ProgressBar bar = new ProgressBar(SourceFiles.Sum(file => file.PositionAmount.Count)); - foreach (Source source in SourceFiles) + foreach (SourcePriceList source in SourceFiles) { TargetFile.Sheet.Columns[TargetFile.amountCell.Column] .EntireColumn diff --git a/src/PriceListTools/ConvertTool.cs b/src/PriceListTools/ConvertTool.cs index 7308be9..58044ed 100644 --- a/src/PriceListTools/ConvertTool.cs +++ b/src/PriceListTools/ConvertTool.cs @@ -5,13 +5,13 @@ namespace RehauSku.PriceListTools { internal class ConvertTool : AbstractTool { - private Source Current; + private SourcePriceList Current; public void GetCurrent() { try { - Current = new Source(ExcelApp.ActiveWorkbook); + Current = new SourcePriceList(ExcelApp.ActiveWorkbook); } catch (Exception exception) diff --git a/src/PriceListTools/MergeTool.cs b/src/PriceListTools/MergeTool.cs index d31cc8a..22e443f 100644 --- a/src/PriceListTools/MergeTool.cs +++ b/src/PriceListTools/MergeTool.cs @@ -6,13 +6,13 @@ namespace RehauSku.PriceListTools { internal class MergeTool : AbstractTool { - public List SourceFiles; + public List SourceFiles; public void FillTarget() { ProgressBar bar = new ProgressBar(SourceFiles.Sum(x => x.PositionAmount.Count)); - foreach (Source source in SourceFiles) + foreach (SourcePriceList source in SourceFiles) { foreach (var kvp in source.PositionAmount) { diff --git a/src/PriceListTools/Source.cs b/src/PriceListTools/SourcePriceList.cs similarity index 90% rename from src/PriceListTools/Source.cs rename to src/PriceListTools/SourcePriceList.cs index a20ad81..a5ee73a 100644 --- a/src/PriceListTools/Source.cs +++ b/src/PriceListTools/SourcePriceList.cs @@ -7,11 +7,11 @@ using RehauSku.Interface; namespace RehauSku.PriceListTools { - internal class Source : AbstractPriceList + internal class SourcePriceList : AbstractPriceList { public Dictionary PositionAmount { get; private set; } - public Source(Workbook workbook) + public SourcePriceList(Workbook workbook) { if (workbook == null) { @@ -37,12 +37,12 @@ namespace RehauSku.PriceListTools CreatePositionsDict(); } - public static List GetSourceLists(string[] files) + public static List GetSourceLists(string[] files) { var ExcelApp = (Application)ExcelDnaUtil.Application; ProgressBar bar = new ProgressBar(files.Length); - List sourceFiles = new List(); + List sourceFiles = new List(); foreach (string file in files) { @@ -50,7 +50,7 @@ namespace RehauSku.PriceListTools Workbook wb = ExcelApp.Workbooks.Open(file); try { - Source priceList = new Source(wb); + SourcePriceList priceList = new SourcePriceList(wb); sourceFiles.Add(priceList); wb.Close(); bar.DoProgress(); diff --git a/src/PriceListTools/Target.cs b/src/PriceListTools/TargetPriceList.cs similarity index 89% rename from src/PriceListTools/Target.cs rename to src/PriceListTools/TargetPriceList.cs index ac91544..2c2f029 100644 --- a/src/PriceListTools/Target.cs +++ b/src/PriceListTools/TargetPriceList.cs @@ -4,12 +4,12 @@ using System.Linq; namespace RehauSku.PriceListTools { - internal class Target : AbstractPriceList + internal class TargetPriceList : AbstractPriceList { private const string oldSkuHeader = "Прежний материал"; public Range oldSkuCell { get; private set; } - public Target(Workbook workbook) + public TargetPriceList(Workbook workbook) { Sheet = workbook.ActiveSheet; Name = workbook.FullName; diff --git a/src/RehauSku.Assist.csproj b/src/RehauSku.Assist.csproj index 6edbc2c..25fa5a5 100644 --- a/src/RehauSku.Assist.csproj +++ b/src/RehauSku.Assist.csproj @@ -128,8 +128,8 @@ - - + + From 0258eb4d2bf82c03be2b0eb2deed5643bb78843e Mon Sep 17 00:00:00 2001 From: Sergey Chebotar Date: Wed, 2 Feb 2022 18:10:29 +0300 Subject: [PATCH 08/19] Incapsulate some classes --- src/AddIn/AddIn.cs | 4 ++-- src/Assistant/RequestModifier.cs | 2 +- src/Assistant/SkuAssist.cs | 2 +- src/Assistant/StoreResponse.cs | 6 +++--- 4 files changed, 7 insertions(+), 7 deletions(-) diff --git a/src/AddIn/AddIn.cs b/src/AddIn/AddIn.cs index 93d8aec..b0fcc3b 100644 --- a/src/AddIn/AddIn.cs +++ b/src/AddIn/AddIn.cs @@ -8,7 +8,7 @@ using System.Runtime.Caching; namespace RehauSku { - public enum ResponseOrder + enum ResponseOrder { Default, Relevance, @@ -17,7 +17,7 @@ namespace RehauSku Series } - public class AddIn : IExcelAddIn + class AddIn : IExcelAddIn { public static HttpClient httpClient; public static MemoryCache memoryCache; diff --git a/src/Assistant/RequestModifier.cs b/src/Assistant/RequestModifier.cs index 9f42e71..c2c3436 100644 --- a/src/Assistant/RequestModifier.cs +++ b/src/Assistant/RequestModifier.cs @@ -5,7 +5,7 @@ using System.Text.RegularExpressions; namespace RehauSku.Assistant { - public static class RequestModifier + static class RequestModifier { public static string CleanRequest(this string input) { diff --git a/src/Assistant/SkuAssist.cs b/src/Assistant/SkuAssist.cs index 6c68288..85a084c 100644 --- a/src/Assistant/SkuAssist.cs +++ b/src/Assistant/SkuAssist.cs @@ -2,7 +2,7 @@ namespace RehauSku.Assistant { - public enum ProductField + enum ProductField { Name, Id, diff --git a/src/Assistant/StoreResponse.cs b/src/Assistant/StoreResponse.cs index 8e1759d..1a9b1c5 100644 --- a/src/Assistant/StoreResponse.cs +++ b/src/Assistant/StoreResponse.cs @@ -2,17 +2,17 @@ namespace RehauSku.Assistant { - public class StoreResponce + class StoreResponce { public Ecommerce Ecommerce { get; set; } } - public class Ecommerce + class Ecommerce { public List Impressions { get; set; } } - public class Product : IProduct + class Product : IProduct { public string Id { get; set; } public string Name { get; set; } From 94b7c4a52bc97026a9b24b7d703174f21f315912 Mon Sep 17 00:00:00 2001 From: Sergey Chebotar Date: Wed, 2 Feb 2022 18:13:03 +0300 Subject: [PATCH 09/19] SaveWorkbookAs edit --- src/Interface/Dialog.cs | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/Interface/Dialog.cs b/src/Interface/Dialog.cs index 6b05dc8..f8ab645 100644 --- a/src/Interface/Dialog.cs +++ b/src/Interface/Dialog.cs @@ -58,9 +58,11 @@ namespace RehauSku.Interface workbook.Close(false); } - string fileName = dialog.FileName; - - workbook.SaveAs(fileName); + else + { + string fileName = dialog.FileName; + workbook.SaveAs(fileName); + } } } } From 9018d7d504dfebbec9a3c75851314d25d1e8197c Mon Sep 17 00:00:00 2001 From: Sergey Chebotar Date: Wed, 2 Feb 2022 18:26:04 +0300 Subject: [PATCH 10/19] Edit Open File Dialogs --- src/Interface/Dialog.cs | 17 +++++------------ src/Interface/RibbonController.cs | 4 ++-- 2 files changed, 7 insertions(+), 14 deletions(-) diff --git a/src/Interface/Dialog.cs b/src/Interface/Dialog.cs index f8ab645..806e92d 100644 --- a/src/Interface/Dialog.cs +++ b/src/Interface/Dialog.cs @@ -8,25 +8,21 @@ namespace RehauSku.Interface { public static string GetFilePath() { - string filePath = string.Empty; - using (OpenFileDialog dialog = new OpenFileDialog()) { dialog.Filter = "Файлы Excel (*.xls;*.xlsx;*.xlsm)|*.xls;*.xlsx;*.xlsm"; if (dialog.ShowDialog() == DialogResult.OK) { - filePath = dialog.FileName; + return dialog.FileName; } } - return filePath; + return string.Empty; } public static string[] GetMultiplyFiles() { - List fileNames = new List(); - using (OpenFileDialog dialog = new OpenFileDialog()) { dialog.Filter = "Файлы Excel (*.xls;*.xlsx;*.xlsm)|*.xls;*.xlsx;*.xlsm"; @@ -34,14 +30,11 @@ namespace RehauSku.Interface if (dialog.ShowDialog() == DialogResult.OK) { - foreach (string file in dialog.FileNames) - { - fileNames.Add(file); - } + return dialog.FileNames; } - } - return fileNames.ToArray(); + else return null; + } } public static void SaveWorkbookAs() diff --git a/src/Interface/RibbonController.cs b/src/Interface/RibbonController.cs index 2b1fd6b..b2e7b28 100644 --- a/src/Interface/RibbonController.cs +++ b/src/Interface/RibbonController.cs @@ -38,7 +38,7 @@ namespace RehauSku.Interface MergeTool mergeTool = new MergeTool(); string[] files = Dialog.GetMultiplyFiles(); - if (files.Length != 0) + if (files != null) { mergeTool.SourceFiles = SourcePriceList.GetSourceLists(files); mergeTool.OpenNewPrice(); @@ -51,7 +51,7 @@ namespace RehauSku.Interface CombineTool combineTool = new CombineTool(); string[] files = Dialog.GetMultiplyFiles(); - if (files.Length != 0) + if (files != null) { combineTool.SourceFiles = SourcePriceList.GetSourceLists(files); combineTool.OpenNewPrice(); From 5001219695090d4a6ead73b2d17434b0805516a3 Mon Sep 17 00:00:00 2001 From: Sergey Chebotar Date: Wed, 2 Feb 2022 18:34:32 +0300 Subject: [PATCH 11/19] Do not delete template path on registry key if cancel pick template dialog --- src/AddIn/RegistryUtil.cs | 6 ++++++ src/Interface/Dialog.cs | 4 ++-- src/Interface/RibbonController.cs | 6 +++++- 3 files changed, 13 insertions(+), 3 deletions(-) diff --git a/src/AddIn/RegistryUtil.cs b/src/AddIn/RegistryUtil.cs index dabd74d..5fe2eea 100644 --- a/src/AddIn/RegistryUtil.cs +++ b/src/AddIn/RegistryUtil.cs @@ -38,6 +38,12 @@ namespace RehauSku if (result == DialogResult.OK) { string fileName = Dialog.GetFilePath(); + + if (string.IsNullOrEmpty(fileName)) + { + throw new Exception("Нет файла шаблона"); + } + priceListPath = fileName; RootKey.SetValue("PriceListPath", fileName); return priceListPath; diff --git a/src/Interface/Dialog.cs b/src/Interface/Dialog.cs index 806e92d..23f65d7 100644 --- a/src/Interface/Dialog.cs +++ b/src/Interface/Dialog.cs @@ -16,9 +16,9 @@ namespace RehauSku.Interface { return dialog.FileName; } - } - return string.Empty; + else return string.Empty; + } } public static string[] GetMultiplyFiles() diff --git a/src/Interface/RibbonController.cs b/src/Interface/RibbonController.cs index b2e7b28..822fe98 100644 --- a/src/Interface/RibbonController.cs +++ b/src/Interface/RibbonController.cs @@ -91,7 +91,11 @@ namespace RehauSku.Interface public void OnSetPricePressed(IRibbonControl control) { string path = Dialog.GetFilePath(); - RegistryUtil.PriceListPath = path; + + if (!string.IsNullOrEmpty(path)) + { + RegistryUtil.PriceListPath = path; + } } } } From 99d1fb6740e93d7d81f0171885ac8f788b56dcfc Mon Sep 17 00:00:00 2001 From: Sergey Chebotar Date: Thu, 3 Feb 2022 21:44:24 +0300 Subject: [PATCH 12/19] Rename method --- src/PriceListTools/AbstractTool.cs | 2 +- src/PriceListTools/CombineTool.cs | 2 +- src/PriceListTools/ConvertTool.cs | 2 +- src/PriceListTools/ExportTool.cs | 2 +- src/PriceListTools/MergeTool.cs | 2 +- 5 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/PriceListTools/AbstractTool.cs b/src/PriceListTools/AbstractTool.cs index a86fd95..0d185f9 100644 --- a/src/PriceListTools/AbstractTool.cs +++ b/src/PriceListTools/AbstractTool.cs @@ -33,7 +33,7 @@ namespace RehauSku.PriceListTools } } - protected private void FillColumnsWithDictionary(KeyValuePair positionAmount, params int[] columns) + protected private void FillPositionAmountToColumns(KeyValuePair positionAmount, params int[] columns) { int? row = GetPositionRow(positionAmount.Key.Sku, positionAmount.Key.Group, TargetFile.skuCell.Column); diff --git a/src/PriceListTools/CombineTool.cs b/src/PriceListTools/CombineTool.cs index 80c7a67..28bcd7c 100644 --- a/src/PriceListTools/CombineTool.cs +++ b/src/PriceListTools/CombineTool.cs @@ -25,7 +25,7 @@ namespace RehauSku.PriceListTools foreach (var kvp in source.PositionAmount) { - FillColumnsWithDictionary(kvp, TargetFile.amountCell.Column - 1, TargetFile.amountCell.Column); + FillPositionAmountToColumns(kvp, TargetFile.amountCell.Column - 1, TargetFile.amountCell.Column); bar.DoProgress(); } } diff --git a/src/PriceListTools/ConvertTool.cs b/src/PriceListTools/ConvertTool.cs index 58044ed..f84f445 100644 --- a/src/PriceListTools/ConvertTool.cs +++ b/src/PriceListTools/ConvertTool.cs @@ -31,7 +31,7 @@ namespace RehauSku.PriceListTools foreach (var kvp in Current.PositionAmount) { - FillColumnsWithDictionary(kvp, TargetFile.amountCell.Column); + FillPositionAmountToColumns(kvp, TargetFile.amountCell.Column); bar.DoProgress(); } diff --git a/src/PriceListTools/ExportTool.cs b/src/PriceListTools/ExportTool.cs index 562c1b2..f2454f5 100644 --- a/src/PriceListTools/ExportTool.cs +++ b/src/PriceListTools/ExportTool.cs @@ -28,7 +28,7 @@ namespace RehauSku.PriceListTools foreach (var kvp in PositionAmount) { - FillColumnsWithDictionary(kvp, TargetFile.amountCell.Column); + FillPositionAmountToColumns(kvp, TargetFile.amountCell.Column); bar.DoProgress(); } diff --git a/src/PriceListTools/MergeTool.cs b/src/PriceListTools/MergeTool.cs index 22e443f..8a99294 100644 --- a/src/PriceListTools/MergeTool.cs +++ b/src/PriceListTools/MergeTool.cs @@ -16,7 +16,7 @@ namespace RehauSku.PriceListTools { foreach (var kvp in source.PositionAmount) { - FillColumnsWithDictionary(kvp, TargetFile.amountCell.Column); + FillPositionAmountToColumns(kvp, TargetFile.amountCell.Column); bar.DoProgress(); } } From ef04747df50da1fbb5124fc9c02f1869d93864f5 Mon Sep 17 00:00:00 2001 From: Sergey Chebotar Date: Thu, 3 Feb 2022 21:56:14 +0300 Subject: [PATCH 13/19] Additional message to statusbar --- src/Interface/ProgressBar.cs | 6 ++++-- src/PriceListTools/CombineTool.cs | 2 +- src/PriceListTools/ConvertTool.cs | 2 +- src/PriceListTools/ExportTool.cs | 2 +- src/PriceListTools/MergeTool.cs | 2 +- src/PriceListTools/SourcePriceList.cs | 2 +- 6 files changed, 9 insertions(+), 7 deletions(-) diff --git a/src/Interface/ProgressBar.cs b/src/Interface/ProgressBar.cs index 474bed7..9a0490b 100644 --- a/src/Interface/ProgressBar.cs +++ b/src/Interface/ProgressBar.cs @@ -7,9 +7,11 @@ namespace RehauSku.Interface private Application Excel = AddIn.Excel; private double CurrentProgress { get; set; } private readonly double TaskWeight; + private readonly string Message; - public ProgressBar(int weight) + public ProgressBar(string message, int weight) { + Message = message; TaskWeight = weight; CurrentProgress = 0; } @@ -20,7 +22,7 @@ namespace RehauSku.Interface if (percent < 100) { - Excel.StatusBar = $"Выполнено {percent.ToString("#.##")} %"; + Excel.StatusBar = $"{Message} Выполнено {percent.ToString("#.##")} %"; } else diff --git a/src/PriceListTools/CombineTool.cs b/src/PriceListTools/CombineTool.cs index 28bcd7c..6b3dcb2 100644 --- a/src/PriceListTools/CombineTool.cs +++ b/src/PriceListTools/CombineTool.cs @@ -11,7 +11,7 @@ namespace RehauSku.PriceListTools public void FillTarget() { - ProgressBar bar = new ProgressBar(SourceFiles.Sum(file => file.PositionAmount.Count)); + ProgressBar bar = new ProgressBar("Заполняю строки...", SourceFiles.Sum(file => file.PositionAmount.Count)); foreach (SourcePriceList source in SourceFiles) { diff --git a/src/PriceListTools/ConvertTool.cs b/src/PriceListTools/ConvertTool.cs index f84f445..3b634eb 100644 --- a/src/PriceListTools/ConvertTool.cs +++ b/src/PriceListTools/ConvertTool.cs @@ -27,7 +27,7 @@ namespace RehauSku.PriceListTools public void FillTarget() { - ProgressBar bar = new ProgressBar(Current.PositionAmount.Count); + ProgressBar bar = new ProgressBar("Заполняю строки...", Current.PositionAmount.Count); foreach (var kvp in Current.PositionAmount) { diff --git a/src/PriceListTools/ExportTool.cs b/src/PriceListTools/ExportTool.cs index f2454f5..024cb3c 100644 --- a/src/PriceListTools/ExportTool.cs +++ b/src/PriceListTools/ExportTool.cs @@ -24,7 +24,7 @@ namespace RehauSku.PriceListTools public void FillTarget() { GetSelected(); - ProgressBar bar = new ProgressBar(PositionAmount.Count); + ProgressBar bar = new ProgressBar("Заполняю строки...", PositionAmount.Count); foreach (var kvp in PositionAmount) { diff --git a/src/PriceListTools/MergeTool.cs b/src/PriceListTools/MergeTool.cs index 8a99294..8f3f6fc 100644 --- a/src/PriceListTools/MergeTool.cs +++ b/src/PriceListTools/MergeTool.cs @@ -10,7 +10,7 @@ namespace RehauSku.PriceListTools public void FillTarget() { - ProgressBar bar = new ProgressBar(SourceFiles.Sum(x => x.PositionAmount.Count)); + ProgressBar bar = new ProgressBar("Заполняю строки...", SourceFiles.Sum(x => x.PositionAmount.Count)); foreach (SourcePriceList source in SourceFiles) { diff --git a/src/PriceListTools/SourcePriceList.cs b/src/PriceListTools/SourcePriceList.cs index a5ee73a..ebd4d6a 100644 --- a/src/PriceListTools/SourcePriceList.cs +++ b/src/PriceListTools/SourcePriceList.cs @@ -40,7 +40,7 @@ namespace RehauSku.PriceListTools public static List GetSourceLists(string[] files) { var ExcelApp = (Application)ExcelDnaUtil.Application; - ProgressBar bar = new ProgressBar(files.Length); + ProgressBar bar = new ProgressBar("Открываю исходные файлы...", files.Length); List sourceFiles = new List(); From 6e889419e2658a3a80fa00582314f1428f6052e5 Mon Sep 17 00:00:00 2001 From: Sergey Chebotar Date: Fri, 4 Feb 2022 09:17:12 +0300 Subject: [PATCH 14/19] 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 @@ + From cc96e1ebe7255c7278c70cef0f951103e9844487 Mon Sep 17 00:00:00 2001 From: Sergey Chebotar Date: Fri, 4 Feb 2022 17:13:47 +0300 Subject: [PATCH 15/19] Implement Enable/Disable tools buttons events --- src/AddIn/AddIn.cs | 18 +++++++++ src/{Assistant => AddIn}/SkuExtensions.cs | 2 +- src/AddIn/WorksheetExtensions.cs | 32 +++++++++++++++ src/Interface/RibbonController.cs | 47 +++++++++++++++++++++-- src/PriceListTools/ExportTool.cs | 1 - src/PriceListTools/SourcePriceList.cs | 6 +-- src/RehauSku.Assist.csproj | 3 +- 7 files changed, 100 insertions(+), 9 deletions(-) rename src/{Assistant => AddIn}/SkuExtensions.cs (88%) create mode 100644 src/AddIn/WorksheetExtensions.cs diff --git a/src/AddIn/AddIn.cs b/src/AddIn/AddIn.cs index b0fcc3b..ad77247 100644 --- a/src/AddIn/AddIn.cs +++ b/src/AddIn/AddIn.cs @@ -31,6 +31,24 @@ namespace RehauSku IntelliSenseServer.Install(); RegistryUtil.Initialize(); Excel = (Application)ExcelDnaUtil.Application; + AddEvents(); + } + + private void AddEvents() + { + Excel.SheetSelectionChange += RefreshExportButton; + Excel.SheetActivate += RefreshConvertButton; + Excel.WorkbookActivate += RefreshConvertButton; + } + + private void RefreshConvertButton(object sh) + { + Interface.RibbonController.RefreshControl("convertPrice"); + } + + private void RefreshExportButton(object sh, Range target) + { + Interface.RibbonController.RefreshControl("exportToPrice"); } public void AutoClose() diff --git a/src/Assistant/SkuExtensions.cs b/src/AddIn/SkuExtensions.cs similarity index 88% rename from src/Assistant/SkuExtensions.cs rename to src/AddIn/SkuExtensions.cs index e39807b..c7fe2bc 100644 --- a/src/Assistant/SkuExtensions.cs +++ b/src/AddIn/SkuExtensions.cs @@ -1,6 +1,6 @@ using System.Text.RegularExpressions; -namespace RehauSku.Assistant +namespace RehauSku { static class SkuExtensions { diff --git a/src/AddIn/WorksheetExtensions.cs b/src/AddIn/WorksheetExtensions.cs new file mode 100644 index 0000000..51ce13a --- /dev/null +++ b/src/AddIn/WorksheetExtensions.cs @@ -0,0 +1,32 @@ +using Microsoft.Office.Interop.Excel; +using System.Linq; + +namespace RehauSku +{ + public static class WorksheetExtensions + { + private static string amountHeader = "Кол-во"; + private static string skuHeader = "Актуальный материал"; + private static string groupHeader = "Программа"; + private static string nameHeader = "Наименование"; + + public static bool IsRehauSource(this Worksheet worksheet) + { + Range amountCell; + Range skuCell; + Range groupCell; + Range nameCell; + + Range[] cells = new[] + { + amountCell = worksheet.Cells.Find(amountHeader), + skuCell = worksheet.Cells.Find(skuHeader), + groupCell = worksheet.Cells.Find(groupHeader), + nameCell = worksheet.Cells.Find(nameHeader) + }; + + return cells.All(x => x != null); + } + } +} + diff --git a/src/Interface/RibbonController.cs b/src/Interface/RibbonController.cs index 822fe98..7d70391 100644 --- a/src/Interface/RibbonController.cs +++ b/src/Interface/RibbonController.cs @@ -1,4 +1,5 @@ using ExcelDna.Integration.CustomUI; +using Microsoft.Office.Interop.Excel; using RehauSku.PriceListTools; using System; using System.Runtime.InteropServices; @@ -9,16 +10,18 @@ namespace RehauSku.Interface [ComVisible(true)] public class RibbonController : ExcelRibbon { + private static IRibbonUI ribbonUi; + public override string GetCustomUI(string RibbonID) { return @" - + -