diff options
author | Sergey Chebotar <s.chebotar@gmail.com> | 2021-12-24 16:22:03 +0300 |
---|---|---|
committer | Sergey Chebotar <s.chebotar@gmail.com> | 2021-12-24 16:22:03 +0300 |
commit | 24024b5729c1c44bb01cb29813868743d1753e31 (patch) | |
tree | 673a1c1102e04adee36be8ded4934d2e40794fef /Source/PriceListTools | |
parent | 2a4076d6eef1b4556f6af8cb074638d440927c5e (diff) |
MergeTool, MemoryUtil anf stuff
Diffstat (limited to 'Source/PriceListTools')
-rw-r--r-- | Source/PriceListTools/ExportTool.cs | 97 | ||||
-rw-r--r-- | Source/PriceListTools/MergeTool.cs | 55 | ||||
-rw-r--r-- | Source/PriceListTools/PriceList.cs | 87 | ||||
-rw-r--r-- | Source/PriceListTools/PriceListUtil.cs | 41 |
4 files changed, 280 insertions, 0 deletions
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<string, double> 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<string, double>(); + 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) + { + + } + } +} + diff --git a/Source/PriceListTools/MergeTool.cs b/Source/PriceListTools/MergeTool.cs new file mode 100644 index 0000000..21da41d --- /dev/null +++ b/Source/PriceListTools/MergeTool.cs @@ -0,0 +1,55 @@ +using ExcelDna.Integration; +using Microsoft.Office.Interop.Excel; +using System; +using System.Collections.Generic; + +namespace RehauSku.PriceListTools +{ + class MergeTool : IDisposable + { + private Application ExcelApp; + private Dictionary<string, double> SkuAmount { get; set; } + + public MergeTool() + { + this.ExcelApp = (Application)ExcelDnaUtil.Application; + this.SkuAmount = new Dictionary<string, double>(); + } + + public void AddSkuAmountToDict(string[] files) + { + ExcelApp.ScreenUpdating = false; + foreach (string file in files) + { + Workbook wb = ExcelApp.Workbooks.Open(file); + PriceList priceList = new PriceList(wb); + + if (priceList.IsValid()) + SkuAmount.AddValues(priceList); + + wb.Close(); + } + ExcelApp.ScreenUpdating = true; + } + + public void ExportToNewFile(string exportFile) + { + 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) + { + + } + } +} diff --git a/Source/PriceListTools/PriceList.cs b/Source/PriceListTools/PriceList.cs new file mode 100644 index 0000000..1460c07 --- /dev/null +++ b/Source/PriceListTools/PriceList.cs @@ -0,0 +1,87 @@ +using Microsoft.Office.Interop.Excel; +using System.Collections.Generic; + +namespace RehauSku.PriceListTools +{ + class PriceList + { + public readonly Workbook Workbook; + public readonly PriceListSheet OfferSheet; + public readonly PriceListSheet ActiveSheet; + + private const string _amountHeader = "Кол-во"; + private const string _skuHeader = "Актуальный материал"; + private const string _offerSheetHeader = "КП"; + + public PriceList(Workbook workbook) + { + Workbook = workbook; + OfferSheet = new PriceListSheet(workbook.Sheets[_offerSheetHeader]); + + Worksheet active = workbook.ActiveSheet; + + if (active.Name == _offerSheetHeader) + ActiveSheet = OfferSheet; + + else + ActiveSheet = new PriceListSheet(active); + } + + public bool IsValid() + { + return OfferSheet.IsValid() && + ActiveSheet.IsValid(); + } + + public void Fill(Dictionary<string, double> values) + { + Worksheet ws = OfferSheet.sheet; + ws.Activate(); + + int amountColumn = OfferSheet.amountColumn.Value; + int skuColumn = OfferSheet.skuColumn.Value; + + foreach (KeyValuePair<string, double> kvp in values) + { + Range cell = ws.Columns[skuColumn].Find(kvp.Key); + ws.Cells[cell.Row, amountColumn].Value = kvp.Value; + } + + AutoFilter filter = ws.AutoFilter; + int firstFilterColumn = filter.Range.Column; + + filter.Range.AutoFilter(amountColumn - firstFilterColumn + 1, "<>"); + ws.Range["A1"].Activate(); + } + + public class PriceListSheet + { + public readonly Worksheet sheet; + public readonly int? headerRow; + public readonly int? amountColumn; + public readonly int? skuColumn; + public object[,] amountCells; + public object[,] skuCells; + + public PriceListSheet(Worksheet sheet) + { + this.sheet = sheet; + headerRow = sheet.Cells.Find(_amountHeader).Row; + amountColumn = sheet.Cells.Find(_amountHeader).Column; + skuColumn = sheet.Cells.Find(_skuHeader).Column; + + amountCells = sheet.Columns[amountColumn].Value2; + skuCells = sheet.Columns[skuColumn].Value2; + } + + public bool IsValid() + { + return sheet != null && + headerRow != null && + amountColumn != null && + skuColumn != null; + } + } + } +} + diff --git a/Source/PriceListTools/PriceListUtil.cs b/Source/PriceListTools/PriceListUtil.cs new file mode 100644 index 0000000..14797d9 --- /dev/null +++ b/Source/PriceListTools/PriceListUtil.cs @@ -0,0 +1,41 @@ +using System.Collections.Generic; +using System.IO; + +namespace RehauSku.PriceListTools +{ + static class PriceListUtil + { + public static string CreateNewExportFile() + { + string fileExtension = Path.GetExtension(RegistryUtil.PriceListPath); + string path = Path.GetTempFileName() + fileExtension; + + File.Copy(RegistryUtil.PriceListPath, path); + return path; + } + + public static void AddValues(this Dictionary<string, double> SkuAmount, PriceList priceList) + { + object[,] amountCells = priceList.ActiveSheet.amountCells; + object[,] skuCells = priceList.ActiveSheet.skuCells; + + for (int row = priceList.ActiveSheet.headerRow.Value + 1; row < amountCells.GetLength(0); row++) + { + object amount = amountCells[row, 1]; + object sku = skuCells[row, 1]; + + if (amount != null && (double)amount != 0) + { + if (SkuAmount.ContainsKey(sku.ToString())) + { + SkuAmount[sku.ToString()] += (double)amount; + } + + else + SkuAmount.Add(sku.ToString(), (double)amount); + } + } + } + } +} + |