From 24024b5729c1c44bb01cb29813868743d1753e31 Mon Sep 17 00:00:00 2001 From: Sergey Chebotar Date: Fri, 24 Dec 2021 16:22:03 +0300 Subject: MergeTool, MemoryUtil anf stuff --- Source/PriceListTools/ExportTool.cs | 97 +++++++++++++++++++++++++++++++++++++ 1 file changed, 97 insertions(+) create mode 100644 Source/PriceListTools/ExportTool.cs (limited to 'Source/PriceListTools/ExportTool.cs') diff --git a/Source/PriceListTools/ExportTool.cs b/Source/PriceListTools/ExportTool.cs new file mode 100644 index 0000000..02def5b --- /dev/null +++ b/Source/PriceListTools/ExportTool.cs @@ -0,0 +1,97 @@ +using ExcelDna.Integration; +using Microsoft.Office.Interop.Excel; +using RehauSku.Assistant; +using System; +using System.Collections.Generic; + +namespace RehauSku.PriceListTools +{ + class ExportTool : IDisposable + { + private Application ExcelApp; + private Dictionary SkuAmount { get; set; } + private Range Selection { get; set; } + + public ExportTool() + { + this.ExcelApp = (Application)ExcelDnaUtil.Application; + Selection = ExcelApp.Selection; + + if (IsRangeValid()) + _FillSkuAmountDict(); + } + + public bool IsRangeValid() + { + return Selection != null && + Selection.Columns.Count == 2; + } + + private void _FillSkuAmountDict() + { + object[,] cells = Selection.Value2; + SkuAmount = 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 (current.ToString().IsRehauSku()) + { + sku = current.ToString(); + } + + 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; + + if (SkuAmount.ContainsKey(sku)) + SkuAmount[sku] += amount.Value; + else + SkuAmount.Add(sku, amount.Value); + } + } + + public void ExportToNewFile() + { + string exportFile = PriceListUtil.CreateNewExportFile(); + Workbook wb = ExcelApp.Workbooks.Open(exportFile); + PriceList priceList = new PriceList(wb); + + if (priceList.IsValid()) + priceList.Fill(SkuAmount); + } + + public void Dispose() + { + Dispose(true); + GC.SuppressFinalize(this); + } + + protected virtual void Dispose(bool disposing) + { + + } + } +} + -- cgit v1.2.3 From 20cfbfcca3a779c04aecdca5e4b465651e2be42a Mon Sep 17 00:00:00 2001 From: Sergey Chebotar Date: Fri, 24 Dec 2021 17:42:20 +0300 Subject: New release --- Source/PriceListTools/ExportTool.cs | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) (limited to 'Source/PriceListTools/ExportTool.cs') diff --git a/Source/PriceListTools/ExportTool.cs b/Source/PriceListTools/ExportTool.cs index 02def5b..0a28bf3 100644 --- a/Source/PriceListTools/ExportTool.cs +++ b/Source/PriceListTools/ExportTool.cs @@ -74,12 +74,30 @@ namespace RehauSku.PriceListTools public void ExportToNewFile() { + if (SkuAmount.Count < 1) + { + return; + } + string exportFile = PriceListUtil.CreateNewExportFile(); Workbook wb = ExcelApp.Workbooks.Open(exportFile); - PriceList priceList = new PriceList(wb); - if (priceList.IsValid()) + try + { + PriceList priceList = new PriceList(wb); priceList.Fill(SkuAmount); + } + + catch(Exception ex) + { + System.Windows.Forms.MessageBox.Show + ($"{RegistryUtil.PriceListPath} не является файлом прайслиста \n\n {ex.Message}", + "Неверный файл прайс-листа!", + System.Windows.Forms.MessageBoxButtons.OK, + System.Windows.Forms.MessageBoxIcon.Error); + + wb.Close(); + } } public void Dispose() -- cgit v1.2.3