From fa195001bc1f90c28a08ac05aac0d47344e517a6 Mon Sep 17 00:00:00 2001 From: Sergey Chebotar Date: Mon, 6 Dec 2021 19:49:10 +0300 Subject: Add DataWriter class --- Source/DataExport/DataWriter.cs | 91 +++++++++++++++++++++++++++++++++++ Source/DataExport/RibbonController.cs | 26 ++++++++++ 2 files changed, 117 insertions(+) create mode 100644 Source/DataExport/DataWriter.cs create mode 100644 Source/DataExport/RibbonController.cs (limited to 'Source/DataExport') diff --git a/Source/DataExport/DataWriter.cs b/Source/DataExport/DataWriter.cs new file mode 100644 index 0000000..c024a22 --- /dev/null +++ b/Source/DataExport/DataWriter.cs @@ -0,0 +1,91 @@ +using ExcelDna.Integration; +using Microsoft.Office.Interop.Excel; +using System; +using System.Collections.Generic; + +namespace Rehau.Sku.Assist +{ + public class DataWriter : IDisposable + { + private Application xlApp; + private Dictionary SkuAmount { get; set; } + private object[,] SelectedCells { get; set; } + private string FileName { get; set; } + + public DataWriter() + { + this.xlApp = (Application)ExcelDnaUtil.Application; + this.FileName = AddIn.priceListPath; + + GetSelectedCells(); + } + + private void GetSelectedCells() + { + Range selection = xlApp.Selection; + this.SelectedCells = (object[,])selection.Value2; + } + + public bool IsRangeValid() + { + return SelectedCells != null && + SelectedCells.GetLength(1) == 2; + } + + public void FillSkuAmountDict() + { + SkuAmount = new Dictionary(); + int rowsCount = SelectedCells.GetLength(0); + + for (int row = 1; row <= rowsCount; row++) + { + if (SelectedCells[row, 1] == null || SelectedCells[row, 2] == null) + continue; + + string sku = null; + double? amount = null; + + for (int column = 1; column <= 2; column++) + { + object current = SelectedCells[row, column]; + + if (current.GetType() == typeof(string) + && SkuAssist.IsRehauSku((string)current)) + sku = (string)current; + + else if (current.GetType() == typeof(string) + && double.TryParse((string)current, 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 GetPriceListWB() + { + Workbook wb = xlApp.Workbooks.Open(FileName); + } + + public void Dispose() + { + Dispose(true); + GC.SuppressFinalize(this); + } + + protected virtual void Dispose(bool disposing) + { + + } + } +} + diff --git a/Source/DataExport/RibbonController.cs b/Source/DataExport/RibbonController.cs new file mode 100644 index 0000000..a9eedb5 --- /dev/null +++ b/Source/DataExport/RibbonController.cs @@ -0,0 +1,26 @@ +using System.Runtime.InteropServices; +using System.Windows.Forms; +using ExcelDna.Integration.CustomUI; + +namespace Ribbon +{ + [ComVisible(true)] + public class RibbonController : ExcelRibbon + { + public override string GetCustomUI(string RibbonID) + { + return @" + + + + + +