diff options
author | Serghei Cebotari <51533848+schebotar@users.noreply.github.com> | 2022-01-28 18:20:30 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-01-28 18:20:30 +0300 |
commit | ec1d38f2d4926ddd89dc8f17d29617ea4ddefa82 (patch) | |
tree | 9fd3a44e58693dc9bbc8d0e406ba4de21b39ec86 /src/PriceListTools/ExportTool.cs | |
parent | d688578a46e3a3383371c1df952fa2898c828a9a (diff) | |
parent | 2ad016bb4c332ecad6d12d824a84f15616ecea38 (diff) |
Merge pull request #12 from schebotar/dev
Dev
Diffstat (limited to 'src/PriceListTools/ExportTool.cs')
-rw-r--r-- | src/PriceListTools/ExportTool.cs | 119 |
1 files changed, 59 insertions, 60 deletions
diff --git a/src/PriceListTools/ExportTool.cs b/src/PriceListTools/ExportTool.cs index a93097d..8bf274a 100644 --- a/src/PriceListTools/ExportTool.cs +++ b/src/PriceListTools/ExportTool.cs @@ -1,34 +1,77 @@ -using ExcelDna.Integration; -using Microsoft.Office.Interop.Excel; +using Microsoft.Office.Interop.Excel; using RehauSku.Assistant; using System; using System.Collections.Generic; namespace RehauSku.PriceListTools { - internal class ExportTool : AbstractPriceListTool, IDisposable + internal class ExportTool : PriceListTool { private Dictionary<string, double> SkuAmount { get; set; } private Range Selection; - public ExportTool() + public void TryGetSelection() { - ExcelApp = (Application)ExcelDnaUtil.Application; Selection = ExcelApp.Selection; + + if (Selection == null || Selection.Columns.Count != 2) + { + throw new Exception("Неверный диапазон"); + } + } + + public void FillTarget() + { + ExcelApp.ScreenUpdating = false; + GetSelected(); + FillColumn(SkuAmount, TargetFile.amountCell.Column); + FilterByAmount(); + ExcelApp.ScreenUpdating = true; + + Forms.Dialog.SaveWorkbookAs(); } - public override void GetSource() + private void FillColumn(IEnumerable<KeyValuePair<string, double>> dictionary, int column) { - if (Selection != null && Selection.Columns.Count == 2) - FillSkuAmountDict(); + List<KeyValuePair<string, double>> missing = new List<KeyValuePair<string, double>>(); - else throw new Exception("Неверный диапазон"); + foreach (var kvp in dictionary) + { + Range cell = TargetFile.skuCell.EntireColumn.Find(kvp.Key); + + if (cell == null) + { + missing.Add(kvp); + } + + else + { + Range sumCell = TargetFile.Sheet.Cells[cell.Row, column]; + + if (sumCell.Value2 == null) + { + sumCell.Value2 = kvp.Value; + } + + else + { + sumCell.Value2 += kvp.Value; + } + } + } + + if (missing.Count > 0) + { + System.Windows.Forms.MessageBox.Show + ($"{missing.Count} артикулов отсутствует в таблице заказов {RegistryUtil.PriceListPath} Попробовать найти новый вариант?", + "Отсутствует позиция в конечной таблице заказов", + System.Windows.Forms.MessageBoxButtons.YesNo, + System.Windows.Forms.MessageBoxIcon.Information); + } } - public override void GetSource(string[] files) - => GetSource(); - private void FillSkuAmountDict() + private void GetSelected() { object[,] cells = Selection.Value2; SkuAmount = new Dictionary<string, double>(); @@ -64,63 +107,19 @@ namespace RehauSku.PriceListTools } if (sku == null || amount == null) + { continue; + } if (SkuAmount.ContainsKey(sku)) - SkuAmount[sku] += amount.Value; - else - SkuAmount.Add(sku, amount.Value); - } - } - - public override void FillPriceList() - { - if (SkuAmount.Count < 1) return; - - PriceListSheet offer = NewPriceList.OfferSheet; - offer.Sheet.Activate(); - - int exportedValues = 0; - - foreach (var kvp in SkuAmount) - { - Range cell = offer.Sheet.Columns[offer.skuColumnNumber].Find(kvp.Key); - - if (cell == null) { - System.Windows.Forms.MessageBox.Show - ($"Артикул {kvp.Key} отсутствует в таблице заказов {RegistryUtil.PriceListPath}", - "Отсутствует позиция в конечной таблице заказов", - System.Windows.Forms.MessageBoxButtons.OK, - System.Windows.Forms.MessageBoxIcon.Information); + SkuAmount[sku] += amount.Value; } - else { - Range sumCell = offer.Sheet.Cells[cell.Row, offer.amountColumnNumber]; - - if (sumCell.Value2 == null) - sumCell.Value2 = kvp.Value; - else - sumCell.Value2 += kvp.Value; - - exportedValues++; + SkuAmount.Add(sku, amount.Value); } } - - AutoFilter filter = offer.Sheet.AutoFilter; - int firstFilterColumn = filter.Range.Column; - - filter.Range.AutoFilter(offer.amountColumnNumber - firstFilterColumn + 1, "<>"); - offer.Sheet.Range["A1"].Activate(); - AddIn.Excel.StatusBar = $"Экспортировано {exportedValues} строк из {SkuAmount.Count}"; - - Forms.Dialog.SaveWorkbookAs(); - } - - public void Dispose() - { - GC.SuppressFinalize(this); } } } |