diff --git a/RhSolutions.AddIn/AddIn/ResetBarFunction.cs b/RhSolutions.AddIn/AddIn/ResetBarFunction.cs index a1a34fa..32bddfa 100644 --- a/RhSolutions.AddIn/AddIn/ResetBarFunction.cs +++ b/RhSolutions.AddIn/AddIn/ResetBarFunction.cs @@ -1,9 +1,15 @@ -namespace RhSolutions.AddIn; +#if !NET472 +using System.Runtime.Versioning; +#endif +namespace RhSolutions.AddIn; +#if !NET472 +[SupportedOSPlatform("windows")] +#endif public static class ResetBarFunction { [ExcelFunction] - public static void _ResetStatusBar() + public static void StatusBarReset() { RhSolutionsAddIn.Excel.StatusBar = false; } diff --git a/RhSolutions.AddIn/AddIn/RhSolutionsAddIn.cs b/RhSolutions.AddIn/AddIn/RhSolutionsAddIn.cs index 796e540..0ef2d69 100644 --- a/RhSolutions.AddIn/AddIn/RhSolutionsAddIn.cs +++ b/RhSolutions.AddIn/AddIn/RhSolutionsAddIn.cs @@ -1,8 +1,15 @@ using ExcelDna.IntelliSense; +using RhSolutions.Tools; using System.Net; +#if !NET472 +using System.Runtime.Versioning; +#endif namespace RhSolutions.AddIn; +#if !NET472 +[SupportedOSPlatform("windows")] +#endif public sealed class RhSolutionsAddIn : IExcelAddIn { public static Application Excel { get; private set; } @@ -14,9 +21,12 @@ public sealed class RhSolutionsAddIn : IExcelAddIn IServiceCollection Services = new ServiceCollection(); Services.AddHttpClient() + .AddSingleton((Application)ExcelDnaUtil.Application) .AddSingleton() - .AddSingleton() - .AddSingleton((Application)ExcelDnaUtil.Application); + .AddSingleton() + .AddTransient() + .AddTransient() + .AddTransient(); ServiceProvider = Services.BuildServiceProvider(); Configuration = ServiceProvider.GetService(); diff --git a/RhSolutions.AddIn/AddIn/RhSolutionsFunction.cs b/RhSolutions.AddIn/AddIn/RhSolutionsFunction.cs index bb7bba5..d45ed10 100644 --- a/RhSolutions.AddIn/AddIn/RhSolutionsFunction.cs +++ b/RhSolutions.AddIn/AddIn/RhSolutionsFunction.cs @@ -1,23 +1,29 @@ -namespace RhSolutions.AddIn; +#if !NET472 +using System.Runtime.Versioning; +#endif +namespace RhSolutions.AddIn; + +#if !NET472 +[SupportedOSPlatform("windows")] +#endif public class RhSolutionsFunction { [ExcelFunction(Description = "Распознать артикул и попробовать найти его в прайс-листе")] public static object RHSOLUTIONS([ExcelArgument(Name = "\"Строка с названием материала\"")] string line) { IDatabaseClient databaseClient = RhSolutionsAddIn.ServiceProvider.GetService(); - IEnumerable requestResult = ExcelAsyncUtil.Run("Database request", line, delegate + + Sku.TryParse(line, out var skus); + + if (ExcelAsyncUtil.Run("Database request", line, delegate { return databaseClient.GetProducts(line) .GetAwaiter() .GetResult(); - }) as IEnumerable; - - Sku.TryParse(line, out var skus); - - if (requestResult == null) + }) is not IEnumerable requestResult) { - if (skus.Count() > 0) + if (skus.Any()) { return $"{skus.First()} ..."; } @@ -29,12 +35,12 @@ public class RhSolutionsFunction else { - if (requestResult.Count() == 0 && skus.Count() == 0) + if (!requestResult.Any() && !skus.Any()) { return ExcelError.ExcelErrorNA; } - else if (requestResult.Count() == 0) + else if (!requestResult.Any()) { return $"{skus.First()}"; } diff --git a/RhSolutions.AddIn/Controllers/CombineTool.cs b/RhSolutions.AddIn/Controllers/CombineTool.cs deleted file mode 100644 index 355539f..0000000 --- a/RhSolutions.AddIn/Controllers/CombineTool.cs +++ /dev/null @@ -1,59 +0,0 @@ -using RhSolutions.AddIn; - -namespace RhSolutions.Controllers; - -internal class CombineTool : ToolBase -{ - private List SourceFiles { get; set; } - - public CombineTool() - { - var dialog = RhSolutionsAddIn.Excel.FileDialog[Microsoft.Office.Core.MsoFileDialogType.msoFileDialogFilePicker]; - dialog.AllowMultiSelect = true; - dialog.Filters.Add("Файлы Excel", "*.xls; *.xlsx; *.xlsm"); - - if (dialog.Show() < 0) - { - List files = new(); - - foreach (string file in dialog.SelectedItems) - { - files.Add(file); - } - - SourceFiles = SourcePriceList.GetSourceLists(files.ToArray()); - } - - else - { - throw new Exception("Не выбраны файлы"); - } - } - - public override void FillTarget() - { - using (ProgressBar = new ProgressBar("Заполняю строки...", SourceFiles.Sum(file => file.PositionAmount.Count))) - using (ResultBar = new ResultBar()) - { - foreach (SourcePriceList source in SourceFiles) - { - TargetFile.Sheet.Columns[TargetFile.AmountCell.Column] - .EntireColumn - .Insert(XlInsertShiftDirection.xlShiftToRight, XlInsertFormatOrigin.xlFormatFromRightOrBelow); - - Range newColumnHeader = TargetFile.Sheet.Cells[TargetFile.AmountCell.Row, TargetFile.AmountCell.Column - 1]; - newColumnHeader.Value2 = $"{source.Name}"; - newColumnHeader.WrapText = true; - - foreach (var kvp in source.PositionAmount) - { - FillPositionAmountToColumns(kvp, TargetFile.AmountCell.Column - 1, TargetFile.AmountCell.Column); - ProgressBar.Update(); - } - } - - FilterByAmount(); - ResultBar.Update(); - } - } -} diff --git a/RhSolutions.AddIn/Controllers/ConvertTool.cs b/RhSolutions.AddIn/Controllers/ConvertTool.cs deleted file mode 100644 index 754fde1..0000000 --- a/RhSolutions.AddIn/Controllers/ConvertTool.cs +++ /dev/null @@ -1,27 +0,0 @@ -namespace RhSolutions.Controllers; - -internal class ConvertTool : ToolBase -{ - private SourcePriceList Current { get; set; } - - public ConvertTool() - { - Current = new SourcePriceList(ExcelApp.ActiveWorkbook); - } - - public override void FillTarget() - { - using (ProgressBar = new ProgressBar("Заполняю строки...", Current.PositionAmount.Count)) - using (ResultBar = new ResultBar()) - { - foreach (var kvp in Current.PositionAmount) - { - FillPositionAmountToColumns(kvp, TargetFile.AmountCell.Column); - ProgressBar.Update(); - } - - FilterByAmount(); - ResultBar.Update(); - } - } -} \ No newline at end of file diff --git a/RhSolutions.AddIn/Controllers/ExportTool.cs b/RhSolutions.AddIn/Controllers/ExportTool.cs deleted file mode 100644 index 16b51aa..0000000 --- a/RhSolutions.AddIn/Controllers/ExportTool.cs +++ /dev/null @@ -1,97 +0,0 @@ -using RhSolutions.Models; -using System.Collections.Generic; -using System.Linq; - -namespace RhSolutions.Controllers; - -internal class ExportTool : ToolBase -{ - private Dictionary PositionAmount; - private readonly Range Selection; - - public ExportTool() - { - Selection = ExcelApp.Selection; - GetSelected(); - - if (PositionAmount.Count == 0) - { - throw new Exception("В выделенном диапазоне не найдены позиции для экспорта"); - } - } - - public override void FillTarget() - { - using (ProgressBar = new ProgressBar("Заполняю строки...", PositionAmount.Count)) - using (ResultBar = new ResultBar()) - { - foreach (var kvp in PositionAmount) - { - FillPositionAmountToColumns(kvp, TargetFile.AmountCell.Column); - ProgressBar.Update(); - } - - FilterByAmount(); - ResultBar.Update(); - } - } - - private void GetSelected() - { - object[,] cells = Selection.Value2; - PositionAmount = new Dictionary(); - - int rowsCount = Selection.Rows.Count; - - for (int row = 1; row <= rowsCount; row++) - { - if (cells[row, 1] == null || cells[row, 2] == null) - continue; - - string sku = null; - double? amount = null; - - for (int column = 1; column <= 2; column++) - { - object current = cells[row, column]; - - if (Sku.TryParse(current.ToString(), out var rauSku)) - { - sku = rauSku.FirstOrDefault().ToString() ?? null; - } - - else if (current.GetType() == typeof(string) - && double.TryParse(current.ToString(), out _)) - { - amount = double.Parse((string)current); - } - - else if (current.GetType() == typeof(double)) - { - amount = (double)current; - } - } - - if (sku == null || amount == null) - { - continue; - } - - Product position = new Product - { - ProductSku = sku - }; - - if (PositionAmount.ContainsKey(position)) - { - PositionAmount[position] += amount.Value; - } - - else - { - PositionAmount.Add(position, amount.Value); - } - } - } -} - diff --git a/RhSolutions.AddIn/Controllers/MergeTool.cs b/RhSolutions.AddIn/Controllers/MergeTool.cs deleted file mode 100644 index 8566ee7..0000000 --- a/RhSolutions.AddIn/Controllers/MergeTool.cs +++ /dev/null @@ -1,56 +0,0 @@ -using RhSolutions.AddIn; -using RhSolutions.Models; -using System; -using System.Collections.Generic; -using System.Linq; - -namespace RhSolutions.Controllers -{ - internal class MergeTool : ToolBase - { - private List SourceFiles { get; set; } - - public MergeTool() - { - var dialog = RhSolutionsAddIn.Excel.FileDialog[Microsoft.Office.Core.MsoFileDialogType.msoFileDialogFilePicker]; - dialog.AllowMultiSelect = true; - dialog.Filters.Add("Файлы Excel", "*.xls; *.xlsx; *.xlsm"); - - if ( dialog.Show() < 0) - { - List files = new(); - - foreach (string file in dialog.SelectedItems) - { - files.Add(file); - } - - SourceFiles = SourcePriceList.GetSourceLists(files.ToArray()); - } - - else - { - throw new Exception("Не выбраны файлы"); - } - } - - public override void FillTarget() - { - using (ProgressBar = new ProgressBar("Заполняю строки...", SourceFiles.Sum(x => x.PositionAmount.Count))) - using (ResultBar = new ResultBar()) - { - foreach (SourcePriceList source in SourceFiles) - { - foreach (var kvp in source.PositionAmount) - { - FillPositionAmountToColumns(kvp, TargetFile.AmountCell.Column); - ProgressBar.Update(); - } - } - - FilterByAmount(); - ResultBar.Update(); - } - } - } -} diff --git a/RhSolutions.AddIn/Controllers/RibbonController.cs b/RhSolutions.AddIn/Controllers/RibbonController.cs index 978732d..880d49f 100644 --- a/RhSolutions.AddIn/Controllers/RibbonController.cs +++ b/RhSolutions.AddIn/Controllers/RibbonController.cs @@ -1,11 +1,17 @@ using ExcelDna.Integration.CustomUI; -using RhSolutions.AddIn; +using RhSolutions.Tools; using System.Reflection; using System.Runtime.InteropServices; +#if! NET472 +using System.Runtime.Versioning; +#endif using System.Windows.Forms; namespace RhSolutions.Controllers; +#if !NET472 +[SupportedOSPlatform("windows")] +#endif [ComVisible(true)] public class RibbonController : ExcelRibbon { @@ -21,10 +27,7 @@ public class RibbonController : ExcelRibbon