diff --git a/README.md b/README.md index 4e1bb53..fac9977 100644 --- a/README.md +++ b/README.md @@ -11,12 +11,12 @@ - Отображение артикула с помощью `=RAUSKU()` - Отображение цены с помощью формулы `=RAUPRICE()` - Экспорт массива ячеек вида "Артикул - Количество" в прайс-лист -- Актуализация прайс-листа +- Актуализация прайс-листа до последней версии - Объединение нескольких прайс-листов в один файл - Сложением всех позиций по артикулам - С разнесением данных по колонкам в конечном файле -*Для работы функций "Экспорт" и "Объединение" требуется указать путь к файлу пустого прайс-листа REHAU* +*Для работы функций "Экспорт", "Актуализация" и "Объединение" требуется указать путь к файлу пустого прайс-листа REHAU* ## Работа без установки 1. Запустить файл `RehauSku.Assist-AddIn-packed.xll` или `RehauSku.Assist-AddIn64-packed.xll` в зависимости от архитектуры приложения diff --git a/src/AddIn/AddIn.cs b/src/AddIn/AddIn.cs index 93d8aec..b532bfb 100644 --- a/src/AddIn/AddIn.cs +++ b/src/AddIn/AddIn.cs @@ -5,10 +5,9 @@ using Microsoft.Office.Interop.Excel; using System.Net.Http; using System.Runtime.Caching; - namespace RehauSku { - public enum ResponseOrder + enum ResponseOrder { Default, Relevance, @@ -17,7 +16,7 @@ namespace RehauSku Series } - public class AddIn : IExcelAddIn + class AddIn : IExcelAddIn { public static HttpClient httpClient; public static MemoryCache memoryCache; @@ -27,16 +26,18 @@ namespace RehauSku { httpClient = new HttpClient(); memoryCache = new MemoryCache("RehauSku"); + Excel = (Application)ExcelDnaUtil.Application; RegisterFunctions(); IntelliSenseServer.Install(); RegistryUtil.Initialize(); - Excel = (Application)ExcelDnaUtil.Application; + EventsUtil.Initialize(); } public void AutoClose() { IntelliSenseServer.Uninstall(); RegistryUtil.Uninitialize(); + EventsUtil.Uninitialize(); memoryCache.Dispose(); } diff --git a/src/AddIn/EventsUtil.cs b/src/AddIn/EventsUtil.cs new file mode 100644 index 0000000..102e12e --- /dev/null +++ b/src/AddIn/EventsUtil.cs @@ -0,0 +1,33 @@ +using Microsoft.Office.Interop.Excel; + +namespace RehauSku +{ + internal static class EventsUtil + { + private static Application Excel = AddIn.Excel; + + public static void Initialize() + { + Excel.SheetSelectionChange += RefreshExportButton; + Excel.SheetActivate += RefreshConvertButton; + Excel.WorkbookActivate += RefreshConvertButton; + } + + public static void Uninitialize() + { + Excel.SheetSelectionChange -= RefreshExportButton; + Excel.SheetActivate -= RefreshConvertButton; + Excel.WorkbookActivate -= RefreshConvertButton; + } + + private static void RefreshConvertButton(object sh) + { + Interface.RibbonController.RefreshControl("convertPrice"); + } + + private static void RefreshExportButton(object sh, Range target) + { + Interface.RibbonController.RefreshControl("exportToPrice"); + } + } +} diff --git a/src/AddIn/RegistryUtil.cs b/src/AddIn/RegistryUtil.cs index 3ec6f6a..5fe2eea 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; @@ -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/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/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; } 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/Forms/Dialog.cs b/src/Interface/Dialog.cs similarity index 53% rename from src/Forms/Dialog.cs rename to src/Interface/Dialog.cs index cc1c29a..23f65d7 100644 --- a/src/Forms/Dialog.cs +++ b/src/Interface/Dialog.cs @@ -2,31 +2,27 @@ using System.Collections.Generic; using System.Windows.Forms; -namespace RehauSku.Forms +namespace RehauSku.Interface { static class Dialog { 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; + else 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,29 +30,33 @@ namespace RehauSku.Forms 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() { - 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); + else + { + string fileName = dialog.FileName; + workbook.SaveAs(fileName); + } + } } } } diff --git a/src/Interface/ProgressBar.cs b/src/Interface/ProgressBar.cs new file mode 100644 index 0000000..2e68e8b --- /dev/null +++ b/src/Interface/ProgressBar.cs @@ -0,0 +1,31 @@ +namespace RehauSku.Interface +{ + internal class ProgressBar : AbstractBar + { + private double CurrentProgress { get; set; } + private readonly double TaskWeight; + private readonly string Message; + + public ProgressBar(string message, int weight) + { + Message = message; + TaskWeight = weight; + CurrentProgress = 0; + } + + public override void Update() + { + double percent = (++CurrentProgress / TaskWeight) * 100; + + if (percent < 100) + { + Excel.StatusBar = $"{Message} Выполнено {percent.ToString("#.#")} %"; + } + + else + { + Excel.StatusBar = false; + } + } + } +} 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/Ribbon/RibbonController.cs b/src/Interface/RibbonController.cs similarity index 58% rename from src/Ribbon/RibbonController.cs rename to src/Interface/RibbonController.cs index 7a514bd..bc038d1 100644 --- a/src/Ribbon/RibbonController.cs +++ b/src/Interface/RibbonController.cs @@ -1,25 +1,27 @@ using ExcelDna.Integration.CustomUI; -using RehauSku.Forms; +using Microsoft.Office.Interop.Excel; 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 { + private static IRibbonUI ribbonUi; + public override string GetCustomUI(string RibbonID) { return @" - + -