aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSergey Chebotar <s.chebotar@gmail.com>2023-03-28 10:36:36 +0300
committerSergey Chebotar <s.chebotar@gmail.com>2023-03-28 10:36:36 +0300
commitcdb153c988f0eaa1355bf53d27280e3d6eed92a4 (patch)
tree29d1aef7d94ceb2267ee01f386ed471d827caf03
parent2280b49ae14579166ff226b39bb19d659b27c691 (diff)
Usings simplify
-rw-r--r--RhSolutions.AddIn/AddIn/RhSolutionsFunction.cs74
-rw-r--r--RhSolutions.AddIn/Controllers/CombineTool.cs85
-rw-r--r--RhSolutions.AddIn/Controllers/ConvertTool.cs37
-rw-r--r--RhSolutions.AddIn/Controllers/ExportTool.cs132
-rw-r--r--RhSolutions.AddIn/Controllers/RibbonController.cs159
-rw-r--r--RhSolutions.AddIn/Controllers/ToolBase.cs9
-rw-r--r--RhSolutions.AddIn/Models/SourcePriceList.cs175
-rw-r--r--RhSolutions.AddIn/Models/TargetPriceList.cs59
-rw-r--r--RhSolutions.AddIn/Models/WorksheetExtensions.cs51
-rw-r--r--RhSolutions.AddIn/Usings.cs6
10 files changed, 370 insertions, 417 deletions
diff --git a/RhSolutions.AddIn/AddIn/RhSolutionsFunction.cs b/RhSolutions.AddIn/AddIn/RhSolutionsFunction.cs
index faa700f..bb7bba5 100644
--- a/RhSolutions.AddIn/AddIn/RhSolutionsFunction.cs
+++ b/RhSolutions.AddIn/AddIn/RhSolutionsFunction.cs
@@ -1,56 +1,48 @@
-using ExcelDna.Integration;
-using Microsoft.Extensions.DependencyInjection;
-using RhSolutions.Models;
-using RhSolutions.Services;
-using System.Collections.Generic;
-using System.Linq;
+namespace RhSolutions.AddIn;
-namespace RhSolutions.AddIn
+public class RhSolutionsFunction
{
- public class RhSolutionsFunction
+ [ExcelFunction(Description = "Распознать артикул и попробовать найти его в прайс-листе")]
+ public static object RHSOLUTIONS([ExcelArgument(Name = "\"Строка с названием материала\"")] string line)
{
- [ExcelFunction(Description = "Распознать артикул и попробовать найти его в прайс-листе")]
- public static object RHSOLUTIONS([ExcelArgument(Name = "\"Строка с названием материала\"")] string line)
+ IDatabaseClient databaseClient = RhSolutionsAddIn.ServiceProvider.GetService<IDatabaseClient>();
+ IEnumerable<Product> requestResult = ExcelAsyncUtil.Run("Database request", line, delegate
{
- IDatabaseClient databaseClient = RhSolutionsAddIn.ServiceProvider.GetService<IDatabaseClient>();
- IEnumerable<Product> requestResult = ExcelAsyncUtil.Run("Database request", line, delegate
- {
- return databaseClient.GetProducts(line)
- .GetAwaiter()
- .GetResult();
- }) as IEnumerable<Product>;
+ return databaseClient.GetProducts(line)
+ .GetAwaiter()
+ .GetResult();
+ }) as IEnumerable<Product>;
- Sku.TryParse(line, out var skus);
+ Sku.TryParse(line, out var skus);
- if (requestResult == null)
+ if (requestResult == null)
+ {
+ if (skus.Count() > 0)
{
- if (skus.Count() > 0)
- {
- return $"{skus.First()} ...";
- }
- else
- {
- return "Загрузка...";
- }
+ return $"{skus.First()} ...";
}
-
else
{
- if (requestResult.Count() == 0 && skus.Count() == 0)
- {
- return ExcelError.ExcelErrorNA;
- }
+ return "Загрузка...";
+ }
+ }
- else if (requestResult.Count() == 0)
- {
- return $"{skus.First()}";
- }
+ else
+ {
+ if (requestResult.Count() == 0 && skus.Count() == 0)
+ {
+ return ExcelError.ExcelErrorNA;
+ }
+
+ else if (requestResult.Count() == 0)
+ {
+ return $"{skus.First()}";
+ }
- else
- {
- var firstProduct = requestResult.First();
- return $"{firstProduct.ProductSku} {firstProduct.Name}";
- }
+ else
+ {
+ var firstProduct = requestResult.First();
+ return $"{firstProduct.ProductSku} {firstProduct.Name}";
}
}
}
diff --git a/RhSolutions.AddIn/Controllers/CombineTool.cs b/RhSolutions.AddIn/Controllers/CombineTool.cs
index ea4e782..355539f 100644
--- a/RhSolutions.AddIn/Controllers/CombineTool.cs
+++ b/RhSolutions.AddIn/Controllers/CombineTool.cs
@@ -1,66 +1,59 @@
-using Microsoft.Office.Interop.Excel;
-using RhSolutions.AddIn;
-using RhSolutions.Models;
-using System;
-using System.Collections.Generic;
-using System.Linq;
-using Range = Microsoft.Office.Interop.Excel.Range;
+using RhSolutions.AddIn;
-namespace RhSolutions.Controllers
+namespace RhSolutions.Controllers;
+
+internal class CombineTool : ToolBase
{
- internal class CombineTool : ToolBase
+ private List<SourcePriceList> SourceFiles { get; set; }
+
+ public CombineTool()
{
- private List<SourcePriceList> SourceFiles { get; set; }
+ var dialog = RhSolutionsAddIn.Excel.FileDialog[Microsoft.Office.Core.MsoFileDialogType.msoFileDialogFilePicker];
+ dialog.AllowMultiSelect = true;
+ dialog.Filters.Add("Файлы Excel", "*.xls; *.xlsx; *.xlsm");
- public CombineTool()
+ if (dialog.Show() < 0)
{
- var dialog = RhSolutionsAddIn.Excel.FileDialog[Microsoft.Office.Core.MsoFileDialogType.msoFileDialogFilePicker];
- dialog.AllowMultiSelect = true;
- dialog.Filters.Add("Файлы Excel", "*.xls; *.xlsx; *.xlsm");
+ List<string> files = new();
- if (dialog.Show() < 0)
+ foreach (string file in dialog.SelectedItems)
{
- List<string> files = new();
-
- foreach (string file in dialog.SelectedItems)
- {
- files.Add(file);
- }
-
- SourceFiles = SourcePriceList.GetSourceLists(files.ToArray());
+ files.Add(file);
}
- else
- {
- throw new Exception("Не выбраны файлы");
- }
+ SourceFiles = SourcePriceList.GetSourceLists(files.ToArray());
}
- public override void FillTarget()
+ else
{
- using (ProgressBar = new ProgressBar("Заполняю строки...", SourceFiles.Sum(file => file.PositionAmount.Count)))
- using (ResultBar = new ResultBar())
+ 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)
{
- foreach (SourcePriceList source in SourceFiles)
- {
- TargetFile.Sheet.Columns[TargetFile.AmountCell.Column]
- .EntireColumn
- .Insert(XlInsertShiftDirection.xlShiftToRight, XlInsertFormatOrigin.xlFormatFromRightOrBelow);
+ 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;
+ 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();
- }
+ foreach (var kvp in source.PositionAmount)
+ {
+ FillPositionAmountToColumns(kvp, TargetFile.AmountCell.Column - 1, TargetFile.AmountCell.Column);
+ ProgressBar.Update();
}
-
- FilterByAmount();
- ResultBar.Update();
}
+
+ FilterByAmount();
+ ResultBar.Update();
}
}
}
diff --git a/RhSolutions.AddIn/Controllers/ConvertTool.cs b/RhSolutions.AddIn/Controllers/ConvertTool.cs
index 5b2cd4d..754fde1 100644
--- a/RhSolutions.AddIn/Controllers/ConvertTool.cs
+++ b/RhSolutions.AddIn/Controllers/ConvertTool.cs
@@ -1,30 +1,27 @@
-using RhSolutions.Models;
+namespace RhSolutions.Controllers;
-namespace RhSolutions.Controllers
+internal class ConvertTool : ToolBase
{
- internal class ConvertTool : ToolBase
- {
- private SourcePriceList Current { get; set; }
+ private SourcePriceList Current { get; set; }
- public ConvertTool()
- {
- Current = new SourcePriceList(ExcelApp.ActiveWorkbook);
- }
+ public ConvertTool()
+ {
+ Current = new SourcePriceList(ExcelApp.ActiveWorkbook);
+ }
- public override void FillTarget()
+ public override void FillTarget()
+ {
+ using (ProgressBar = new ProgressBar("Заполняю строки...", Current.PositionAmount.Count))
+ using (ResultBar = new ResultBar())
{
- using (ProgressBar = new ProgressBar("Заполняю строки...", Current.PositionAmount.Count))
- using (ResultBar = new ResultBar())
+ foreach (var kvp in Current.PositionAmount)
{
- foreach (var kvp in Current.PositionAmount)
- {
- FillPositionAmountToColumns(kvp, TargetFile.AmountCell.Column);
- ProgressBar.Update();
- }
-
- FilterByAmount();
- ResultBar.Update();
+ 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
index e2bbde4..16b51aa 100644
--- a/RhSolutions.AddIn/Controllers/ExportTool.cs
+++ b/RhSolutions.AddIn/Controllers/ExportTool.cs
@@ -1,99 +1,95 @@
-using Microsoft.Office.Interop.Excel;
-using System;
+using RhSolutions.Models;
using System.Collections.Generic;
-using RhSolutions.Models;
using System.Linq;
-using Range = Microsoft.Office.Interop.Excel.Range;
-namespace RhSolutions.Controllers
+namespace RhSolutions.Controllers;
+
+internal class ExportTool : ToolBase
{
- internal class ExportTool : ToolBase
+ private Dictionary<Product, double> PositionAmount;
+ private readonly Range Selection;
+
+ public ExportTool()
{
- private Dictionary<Product, double> PositionAmount;
- private readonly Range Selection;
+ Selection = ExcelApp.Selection;
+ GetSelected();
- public ExportTool()
+ if (PositionAmount.Count == 0)
{
- Selection = ExcelApp.Selection;
- GetSelected();
-
- if (PositionAmount.Count == 0)
- {
- throw new Exception("В выделенном диапазоне не найдены позиции для экспорта");
- }
+ throw new Exception("В выделенном диапазоне не найдены позиции для экспорта");
}
+ }
- public override void FillTarget()
+ public override void FillTarget()
+ {
+ using (ProgressBar = new ProgressBar("Заполняю строки...", PositionAmount.Count))
+ using (ResultBar = new ResultBar())
{
- using (ProgressBar = new ProgressBar("Заполняю строки...", PositionAmount.Count))
- using (ResultBar = new ResultBar())
+ foreach (var kvp in PositionAmount)
{
- foreach (var kvp in PositionAmount)
- {
- FillPositionAmountToColumns(kvp, TargetFile.AmountCell.Column);
- ProgressBar.Update();
- }
-
- FilterByAmount();
- ResultBar.Update();
+ FillPositionAmountToColumns(kvp, TargetFile.AmountCell.Column);
+ ProgressBar.Update();
}
+
+ FilterByAmount();
+ ResultBar.Update();
}
+ }
+
+ private void GetSelected()
+ {
+ object[,] cells = Selection.Value2;
+ PositionAmount = new Dictionary<Product, double>();
+
+ int rowsCount = Selection.Rows.Count;
- private void GetSelected()
+ for (int row = 1; row <= rowsCount; row++)
{
- object[,] cells = Selection.Value2;
- PositionAmount = new Dictionary<Product, double>();
+ if (cells[row, 1] == null || cells[row, 2] == null)
+ continue;
- int rowsCount = Selection.Rows.Count;
+ string sku = null;
+ double? amount = null;
- for (int row = 1; row <= rowsCount; row++)
+ for (int column = 1; column <= 2; column++)
{
- if (cells[row, 1] == null || cells[row, 2] == null)
- continue;
+ object current = cells[row, column];
- string sku = null;
- double? amount = null;
-
- for (int column = 1; column <= 2; column++)
+ if (Sku.TryParse(current.ToString(), out var rauSku))
{
- 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;
- }
+ sku = rauSku.FirstOrDefault().ToString() ?? null;
}
- if (sku == null || amount == null)
+ else if (current.GetType() == typeof(string)
+ && double.TryParse(current.ToString(), out _))
{
- continue;
+ amount = double.Parse((string)current);
}
- Product position = new Product
- {
- ProductSku = sku
- };
-
- if (PositionAmount.ContainsKey(position))
+ else if (current.GetType() == typeof(double))
{
- PositionAmount[position] += amount.Value;
+ amount = (double)current;
}
+ }
- else
- {
- PositionAmount.Add(position, amount.Value);
- }
+ 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/RibbonController.cs b/RhSolutions.AddIn/Controllers/RibbonController.cs
index d819b4b..978732d 100644
--- a/RhSolutions.AddIn/Controllers/RibbonController.cs
+++ b/RhSolutions.AddIn/Controllers/RibbonController.cs
@@ -1,23 +1,19 @@
using ExcelDna.Integration.CustomUI;
-using Microsoft.Office.Interop.Excel;
using RhSolutions.AddIn;
-using RhSolutions.Services;
-using System;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Windows.Forms;
-using Range = Microsoft.Office.Interop.Excel.Range;
-namespace RhSolutions.Controllers
+namespace RhSolutions.Controllers;
+
+[ComVisible(true)]
+public class RibbonController : ExcelRibbon
{
- [ComVisible(true)]
- public class RibbonController : ExcelRibbon
- {
- private static IRibbonUI ribbonUi;
+ private static IRibbonUI ribbonUi;
- public override string GetCustomUI(string RibbonID)
- {
- return @"
+ public override string GetCustomUI(string RibbonID)
+ {
+ return @"
<customUI onLoad='RibbonLoad' xmlns='http://schemas.microsoft.com/office/2006/01/customui'>
<ribbon>
<tabs>
@@ -37,96 +33,95 @@ namespace RhSolutions.Controllers
</tabs>
</ribbon>
</customUI>";
- }
+ }
- public void RibbonLoad(IRibbonUI sender)
- {
- ribbonUi = sender;
- }
+ public void RibbonLoad(IRibbonUI sender)
+ {
+ ribbonUi = sender;
+ }
- public static void RefreshControl(string id)
+ public static void RefreshControl(string id)
+ {
+ if (ribbonUi != null)
{
- if (ribbonUi != null)
- {
- ribbonUi.InvalidateControl(id);
- }
+ ribbonUi.InvalidateControl(id);
}
+ }
- public void OnSetPricePressed(IRibbonControl control)
- {
- var dialog = RhSolutionsAddIn.Excel
- .FileDialog[Microsoft.Office.Core.MsoFileDialogType.msoFileDialogFilePicker];
- dialog.AllowMultiSelect = false;
- dialog.Filters.Add("Файлы Excel", "*.xls; *.xlsx; *.xlsm");
-
- if (dialog.Show() < 0)
- {
- RhSolutionsAddIn.Configuration.SetPriceListPath(dialog.SelectedItems.Item(1));
- RhSolutionsAddIn.Configuration.SaveSettings();
- }
- }
+ public void OnSetPricePressed(IRibbonControl control)
+ {
+ var dialog = RhSolutionsAddIn.Excel
+ .FileDialog[Microsoft.Office.Core.MsoFileDialogType.msoFileDialogFilePicker];
+ dialog.AllowMultiSelect = false;
+ dialog.Filters.Add("Файлы Excel", "*.xls; *.xlsx; *.xlsm");
- public void OnToolPressed(IRibbonControl control)
+ if (dialog.Show() < 0)
{
- try
- {
- ToolBase tool = control.Id switch
- {
- "export" => new ExportTool(),
- "convert" => new ConvertTool(),
- "merge" => new MergeTool(),
- "combine" => new CombineTool(),
- _ => throw new Exception("Неизвестный инструмент"),
- };
- tool.OpenNewPrice();
- tool.FillTarget();
- }
-
- catch (Exception exception)
- {
- MessageBox.Show(exception.Message,
- "Ошибка",
- MessageBoxButtons.OK,
- MessageBoxIcon.Information);
- RhSolutionsAddIn.Excel.StatusBar = false;
- return;
- }
+ RhSolutionsAddIn.Configuration.SetPriceListPath(dialog.SelectedItems.Item(1));
+ RhSolutionsAddIn.Configuration.SaveSettings();
}
+ }
- public bool GetConvertEnabled(IRibbonControl control)
+ public void OnToolPressed(IRibbonControl control)
+ {
+ try
{
- if (RhSolutionsAddIn.Excel.ActiveWorkbook == null)
- return false;
-
- else
+ ToolBase tool = control.Id switch
{
- Worksheet worksheet = RhSolutionsAddIn.Excel.ActiveWorkbook.ActiveSheet;
- return worksheet.IsRehauSource();
- }
+ "export" => new ExportTool(),
+ "convert" => new ConvertTool(),
+ "merge" => new MergeTool(),
+ "combine" => new CombineTool(),
+ _ => throw new Exception("Неизвестный инструмент"),
+ };
+ tool.OpenNewPrice();
+ tool.FillTarget();
}
- public bool GetExportEnabled(IRibbonControl control)
+ catch (Exception exception)
{
- if (RhSolutionsAddIn.Excel.ActiveWorkbook == null)
- return false;
-
- else
- {
- Range selection = RhSolutionsAddIn.Excel.Selection;
- return selection.Columns.Count == 2;
- }
+ MessageBox.Show(exception.Message,
+ "Ошибка",
+ MessageBoxButtons.OK,
+ MessageBoxIcon.Information);
+ RhSolutionsAddIn.Excel.StatusBar = false;
+ return;
}
+ }
+
+ public bool GetConvertEnabled(IRibbonControl control)
+ {
+ if (RhSolutionsAddIn.Excel.ActiveWorkbook == null)
+ return false;
- public string GetVersionLabel(IRibbonControl control)
+ else
{
- string version = Assembly.GetExecutingAssembly().GetName().Version.ToString();
- return $"v{version}";
+ Worksheet worksheet = RhSolutionsAddIn.Excel.ActiveWorkbook.ActiveSheet;
+ return worksheet.IsRehauSource();
}
+ }
+
+ public bool GetExportEnabled(IRibbonControl control)
+ {
+ if (RhSolutionsAddIn.Excel.ActiveWorkbook == null)
+ return false;
- public string GetPriceListPathLabel(IRibbonControl control)
+ else
{
- string name = RhSolutionsAddIn.Configuration.GetPriceListFileName();
- return string.IsNullOrEmpty(name) ? "Нет файла шаблона!" : name;
+ Range selection = RhSolutionsAddIn.Excel.Selection;
+ return selection.Columns.Count == 2;
}
}
+
+ public string GetVersionLabel(IRibbonControl control)
+ {
+ string version = Assembly.GetExecutingAssembly().GetName().Version.ToString();
+ return $"v{version}";
+ }
+
+ public string GetPriceListPathLabel(IRibbonControl control)
+ {
+ string name = RhSolutionsAddIn.Configuration.GetPriceListFileName();
+ return string.IsNullOrEmpty(name) ? "Нет файла шаблона!" : name;
+ }
}
diff --git a/RhSolutions.AddIn/Controllers/ToolBase.cs b/RhSolutions.AddIn/Controllers/ToolBase.cs
index b54b45a..3f9d82d 100644
--- a/RhSolutions.AddIn/Controllers/ToolBase.cs
+++ b/RhSolutions.AddIn/Controllers/ToolBase.cs
@@ -1,11 +1,4 @@
-using Microsoft.Office.Interop.Excel;
-using RhSolutions.AddIn;
-using RhSolutions.Models;
-using RhSolutions.Services;
-using System;
-using System.Collections.Generic;
-using System.Linq;
-using Range = Microsoft.Office.Interop.Excel.Range;
+using RhSolutions.AddIn;
namespace RhSolutions.Controllers
{
diff --git a/RhSolutions.AddIn/Models/SourcePriceList.cs b/RhSolutions.AddIn/Models/SourcePriceList.cs
index dc950eb..d6c2cfe 100644
--- a/RhSolutions.AddIn/Models/SourcePriceList.cs
+++ b/RhSolutions.AddIn/Models/SourcePriceList.cs
@@ -1,113 +1,106 @@
-using ExcelDna.Integration;
-using Microsoft.Office.Interop.Excel;
-using System;
-using System.Collections.Generic;
-using System.IO;
-using System.Linq;
-using Range = Microsoft.Office.Interop.Excel.Range;
-
-namespace RhSolutions.Models
+using System.IO;
+
+namespace RhSolutions.Models;
+
+internal class SourcePriceList : PriceListBase
{
- internal class SourcePriceList : PriceListBase
- {
- public Dictionary<Product, double> PositionAmount { get; private set; }
+ public Dictionary<Product, double> PositionAmount { get; private set; }
- public SourcePriceList(Workbook workbook)
+ public SourcePriceList(Workbook workbook)
+ {
+ if (workbook == null)
{
- if (workbook == null)
- {
- throw new ArgumentException($"Нет рабочего файла");
- }
-
- Sheet = workbook.ActiveSheet;
- Name = Path.GetFileNameWithoutExtension(workbook.FullName);
+ throw new ArgumentException($"Нет рабочего файла");
+ }
- Range[] cells = new[]
- {
- AmountCell = Sheet.Cells.Find(PriceListHeaders.Amount),
- SkuCell = Sheet.Cells.Find(PriceListHeaders.Sku),
- GroupCell = Sheet.Cells.Find(PriceListHeaders.Group),
- NameCell = Sheet.Cells.Find(PriceListHeaders.Name)
- };
+ Sheet = workbook.ActiveSheet;
+ Name = Path.GetFileNameWithoutExtension(workbook.FullName);
- if (cells.Any(x => x == null))
- {
- throw new ArgumentException($"Файл {Name} не распознан");
- }
+ Range[] cells = new[]
+ {
+ AmountCell = Sheet.Cells.Find(PriceListHeaders.Amount),
+ SkuCell = Sheet.Cells.Find(PriceListHeaders.Sku),
+ GroupCell = Sheet.Cells.Find(PriceListHeaders.Group),
+ NameCell = Sheet.Cells.Find(PriceListHeaders.Name)
+ };
- CreatePositionsDict();
+ if (cells.Any(x => x == null))
+ {
+ throw new ArgumentException($"Файл {Name} не распознан");
}
- public static List<SourcePriceList> GetSourceLists(string[] files)
- {
- var ExcelApp = (Application)ExcelDnaUtil.Application;
- ProgressBar bar = new ProgressBar("Открываю исходные файлы...", files.Length);
+ CreatePositionsDict();
+ }
+
+ public static List<SourcePriceList> GetSourceLists(string[] files)
+ {
+ var ExcelApp = (Application)ExcelDnaUtil.Application;
+ ProgressBar bar = new ProgressBar("Открываю исходные файлы...", files.Length);
- List<SourcePriceList> sourceFiles = new List<SourcePriceList>();
+ List<SourcePriceList> sourceFiles = new List<SourcePriceList>();
- foreach (string file in files)
+ foreach (string file in files)
+ {
+ ExcelApp.ScreenUpdating = false;
+ Workbook wb = ExcelApp.Workbooks.Open(file);
+ try
{
- ExcelApp.ScreenUpdating = false;
- Workbook wb = ExcelApp.Workbooks.Open(file);
- try
- {
- SourcePriceList priceList = new SourcePriceList(wb);
- sourceFiles.Add(priceList);
- wb.Close();
- bar.Update();
- }
- catch (Exception ex)
- {
- System.Windows.Forms.MessageBox.Show
- (ex.Message,
- "Ошибка открытия исходного прайс-листа",
- System.Windows.Forms.MessageBoxButtons.OK,
- System.Windows.Forms.MessageBoxIcon.Information);
- wb.Close();
- bar.Update();
- }
- ExcelApp.ScreenUpdating = true;
+ SourcePriceList priceList = new SourcePriceList(wb);
+ sourceFiles.Add(priceList);
+ wb.Close();
+ bar.Update();
}
-
- return sourceFiles;
+ catch (Exception ex)
+ {
+ System.Windows.Forms.MessageBox.Show
+ (ex.Message,
+ "Ошибка открытия исходного прайс-листа",
+ System.Windows.Forms.MessageBoxButtons.OK,
+ System.Windows.Forms.MessageBoxIcon.Information);
+ wb.Close();
+ bar.Update();
+ }
+ ExcelApp.ScreenUpdating = true;
}
- private void CreatePositionsDict()
+ return sourceFiles;
+ }
+
+ private void CreatePositionsDict()
+ {
+ PositionAmount = new Dictionary<Product, double>();
+
+ for (int row = AmountCell.Row + 1; row <= Sheet.Cells[Sheet.Rows.Count, AmountCell.Column].End[XlDirection.xlUp].Row; row++)
{
- PositionAmount = new Dictionary<Product, double>();
+ double? amount = Sheet.Cells[row, AmountCell.Column].Value2 as double?;
- for (int row = AmountCell.Row + 1; row <= Sheet.Cells[Sheet.Rows.Count, AmountCell.Column].End[XlDirection.xlUp].Row; row++)
+ if (amount != null && amount.Value != 0)
{
- double? amount = Sheet.Cells[row, AmountCell.Column].Value2 as double?;
+ object group = Sheet.Cells[row, GroupCell.Column].Value2;
+ 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.TryParse(sku.ToString(), out _))
+ continue;
+
+ Product p = new Product
+ {
+ ProductSku = sku.ToString(),
+ ProductLine = group.ToString(),
+ Name = name.ToString()
+ };
+
+ if (PositionAmount.ContainsKey(p))
+ {
+ PositionAmount[p] += amount.Value;
+ }
- if (amount != null && amount.Value != 0)
+ else
{
- object group = Sheet.Cells[row, GroupCell.Column].Value2;
- 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.TryParse(sku.ToString(), out _))
- continue;
-
- Product p = new Product
- {
- ProductSku = sku.ToString(),
- ProductLine = group.ToString(),
- Name = name.ToString()
- };
-
- if (PositionAmount.ContainsKey(p))
- {
- PositionAmount[p] += amount.Value;
- }
-
- else
- {
- PositionAmount.Add(p, amount.Value);
- }
+ PositionAmount.Add(p, amount.Value);
}
}
}
diff --git a/RhSolutions.AddIn/Models/TargetPriceList.cs b/RhSolutions.AddIn/Models/TargetPriceList.cs
index 2dcd48e..254b5b0 100644
--- a/RhSolutions.AddIn/Models/TargetPriceList.cs
+++ b/RhSolutions.AddIn/Models/TargetPriceList.cs
@@ -1,40 +1,35 @@
-using Microsoft.Office.Interop.Excel;
-using System;
-using System.IO;
-using System.Linq;
-using Range = Microsoft.Office.Interop.Excel.Range;
+using System.IO;
-namespace RhSolutions.Models
+namespace RhSolutions.Models;
+
+internal class TargetPriceList : PriceListBase
{
- internal class TargetPriceList : PriceListBase
+ public Range OldSkuCell { get; private set; }
+
+ public TargetPriceList(Workbook workbook)
{
- public Range OldSkuCell { get; private set; }
+ if (workbook == null)
+ {
+ throw new ArgumentException("Невозможно открыть книгу шаблонного файла. " +
+ "Возможно открыт файл с именем, совпадающим с именем шаблонного файла.");
+ }
+
+ Sheet = workbook.ActiveSheet;
+ Name = Path.GetFileNameWithoutExtension(workbook.FullName);
+
+ Range[] cells = new[]
+ {
+ AmountCell = Sheet.Cells.Find(PriceListHeaders.Amount),
+ SkuCell = Sheet.Cells.Find(PriceListHeaders.Sku),
+ GroupCell = Sheet.Cells.Find(PriceListHeaders.Group),
+ NameCell = Sheet.Cells.Find(PriceListHeaders.Name)
+ };
+
+ OldSkuCell = Sheet.Cells.Find(PriceListHeaders.OldSku);
- public TargetPriceList(Workbook workbook)
+ if (cells.Any(x => x == null))
{
- if (workbook == null)
- {
- throw new ArgumentException("Невозможно открыть книгу шаблонного файла. " +
- "Возможно открыт файл с именем, совпадающим с именем шаблонного файла.");
- }
-
- Sheet = workbook.ActiveSheet;
- Name = Path.GetFileNameWithoutExtension(workbook.FullName);
-
- Range[] cells = new[]
- {
- AmountCell = Sheet.Cells.Find(PriceListHeaders.Amount),
- SkuCell = Sheet.Cells.Find(PriceListHeaders.Sku),
- GroupCell = Sheet.Cells.Find(PriceListHeaders.Group),
- NameCell = Sheet.Cells.Find(PriceListHeaders.Name)
- };
-
- OldSkuCell = Sheet.Cells.Find(PriceListHeaders.OldSku);
-
- if (cells.Any(x => x == null))
- {
- throw new ArgumentException($"Шаблон {Name} не является прайс-листом");
- }
+ throw new ArgumentException($"Шаблон {Name} не является прайс-листом");
}
}
}
diff --git a/RhSolutions.AddIn/Models/WorksheetExtensions.cs b/RhSolutions.AddIn/Models/WorksheetExtensions.cs
index 6b5fc3e..24968e2 100644
--- a/RhSolutions.AddIn/Models/WorksheetExtensions.cs
+++ b/RhSolutions.AddIn/Models/WorksheetExtensions.cs
@@ -1,40 +1,35 @@
-using Microsoft.Office.Interop.Excel;
-using RhSolutions.Models;
-using System.Linq;
+namespace RhSolutions.Services;
-namespace RhSolutions.Services
+public static class WorksheetExtensions
{
- public static class WorksheetExtensions
+ public static bool IsRehauSource(this Worksheet worksheet)
{
- public static bool IsRehauSource(this Worksheet worksheet)
+ Range amountCell;
+ Range skuCell;
+ Range groupCell;
+ Range nameCell;
+
+ Range[] cells = new[]
{
- Range amountCell;
- Range skuCell;
- Range groupCell;
- Range nameCell;
+ amountCell = worksheet.Cells.Find(PriceListHeaders.Amount),
+ skuCell = worksheet.Cells.Find(PriceListHeaders.Sku),
+ groupCell = worksheet.Cells.Find(PriceListHeaders.Group),
+ nameCell = worksheet.Cells.Find(PriceListHeaders.Name)
+ };
- Range[] cells = new[]
- {
- amountCell = worksheet.Cells.Find(PriceListHeaders.Amount),
- skuCell = worksheet.Cells.Find(PriceListHeaders.Sku),
- groupCell = worksheet.Cells.Find(PriceListHeaders.Group),
- nameCell = worksheet.Cells.Find(PriceListHeaders.Name)
- };
+ return cells.All(x => x != null);
+ }
- return cells.All(x => x != null);
+ public static void AddValue(this Range range, double value)
+ {
+ if (range.Value2 == null)
+ {
+ range.Value2 = value;
}
- public static void AddValue(this Range range, double value)
+ else
{
- if (range.Value2 == null)
- {
- range.Value2 = value;
- }
-
- else
- {
- range.Value2 += value;
- }
+ range.Value2 += value;
}
}
}
diff --git a/RhSolutions.AddIn/Usings.cs b/RhSolutions.AddIn/Usings.cs
index 1047bda..929f09f 100644
--- a/RhSolutions.AddIn/Usings.cs
+++ b/RhSolutions.AddIn/Usings.cs
@@ -1,5 +1,9 @@
global using ExcelDna.Integration;
global using Microsoft.Extensions.DependencyInjection;
global using Microsoft.Office.Interop.Excel;
+global using RhSolutions.Models;
global using RhSolutions.Services;
-global using System; \ No newline at end of file
+global using System;
+global using System.Collections.Generic;
+global using System.Linq;
+global using Range = Microsoft.Office.Interop.Excel.Range; \ No newline at end of file