diff options
Diffstat (limited to 'src/PriceListTools')
-rw-r--r-- | src/PriceListTools/AbstractPriceList.cs | 13 | ||||
-rw-r--r-- | src/PriceListTools/AbstractTool.cs | 120 | ||||
-rw-r--r-- | src/PriceListTools/CombineTool.cs | 29 | ||||
-rw-r--r-- | src/PriceListTools/ConvertTool.cs | 24 | ||||
-rw-r--r-- | src/PriceListTools/ExportTool.cs | 13 | ||||
-rw-r--r-- | src/PriceListTools/MergeTool.cs | 22 | ||||
-rw-r--r-- | src/PriceListTools/PriceListHeaders.cs | 11 | ||||
-rw-r--r-- | src/PriceListTools/SourcePriceList.cs | 19 | ||||
-rw-r--r-- | src/PriceListTools/TargetPriceList.cs | 13 |
9 files changed, 139 insertions, 125 deletions
diff --git a/src/PriceListTools/AbstractPriceList.cs b/src/PriceListTools/AbstractPriceList.cs index 06427a0..2f89a85 100644 --- a/src/PriceListTools/AbstractPriceList.cs +++ b/src/PriceListTools/AbstractPriceList.cs @@ -4,15 +4,10 @@ namespace RehauSku.PriceListTools { internal abstract class AbstractPriceList { - protected const string amountHeader = "Кол-во"; - protected const string skuHeader = "Актуальный материал"; - protected const string groupHeader = "Программа"; - protected const string nameHeader = "Наименование"; - - public Range amountCell { get; protected set; } - public Range skuCell { get; protected set; } - public Range groupCell { get; protected set; } - public Range nameCell { get; protected set; } + public Range AmountCell { get; protected set; } + public Range SkuCell { get; protected set; } + public Range GroupCell { get; protected set; } + public Range NameCell { get; protected set; } public Worksheet Sheet { get; protected set; } public string Name { get; protected set; } diff --git a/src/PriceListTools/AbstractTool.cs b/src/PriceListTools/AbstractTool.cs index d312315..256be06 100644 --- a/src/PriceListTools/AbstractTool.cs +++ b/src/PriceListTools/AbstractTool.cs @@ -1,21 +1,20 @@ -using ExcelDna.Integration; -using Microsoft.Office.Interop.Excel; +using Microsoft.Office.Interop.Excel; using RehauSku.Interface; using System; using System.Collections.Generic; using System.Linq; -using System.Windows.Forms; -using Application = Microsoft.Office.Interop.Excel.Application; using ProgressBar = RehauSku.Interface.ProgressBar; namespace RehauSku.PriceListTools { internal abstract class AbstractTool { - protected private Application ExcelApp = (Application)ExcelDnaUtil.Application; - protected private TargetPriceList TargetFile; - protected private ResultBar ResultBar { get; set; } - protected private ProgressBar ProgressBar { get; set; } + protected Application ExcelApp = AddIn.Excel; + protected TargetPriceList TargetFile { get; set; } + protected ResultBar ResultBar { get; set; } + protected ProgressBar ProgressBar { get; set; } + + public abstract void FillTarget(); public void OpenNewPrice() { @@ -23,12 +22,6 @@ namespace RehauSku.PriceListTools .Cast<Workbook>() .FirstOrDefault(w => w.FullName == RegistryUtil.PriceListPath) != null) { - MessageBox.Show - ("Шаблонный файл редактируется в другом месте", - "Ошибка открытия шаблонного прайс-листа", - MessageBoxButtons.OK, - MessageBoxIcon.Information); - throw new ArgumentException("Шаблонный файл редактируется в другом месте"); } @@ -41,12 +34,6 @@ namespace RehauSku.PriceListTools catch (Exception exception) { - MessageBox.Show - (exception.Message, - "Ошибка открытия шаблонного прайс-листа", - MessageBoxButtons.OK, - MessageBoxIcon.Information); - if (wb != null) { wb.Close(); @@ -56,9 +43,9 @@ namespace RehauSku.PriceListTools } } - protected private void FillPositionAmountToColumns(KeyValuePair<Position, double> positionAmount, params int[] columns) + protected void FillPositionAmountToColumns(KeyValuePair<Position, double> positionAmount, params int[] columns) { - int? row = GetPositionRow(positionAmount.Key.Sku, positionAmount.Key.Group, TargetFile.skuCell.Column); + int? row = GetPositionRow(TargetFile.SkuCell.EntireColumn, positionAmount.Key.Sku, positionAmount.Key.Group); if (row != null) { @@ -69,11 +56,12 @@ namespace RehauSku.PriceListTools } ResultBar.IncrementSuccess(); + return; } - else if (TargetFile.oldSkuCell != null) + if (TargetFile.OldSkuCell != null) { - row = GetPositionRow(positionAmount.Key.Sku, positionAmount.Key.Group, TargetFile.oldSkuCell.Column); + row = GetPositionRow(TargetFile.OldSkuCell.EntireColumn, positionAmount.Key.Sku, positionAmount.Key.Group); if (row != null) { @@ -84,36 +72,32 @@ namespace RehauSku.PriceListTools } ResultBar.IncrementReplaced(); + return; } } - else - { - string sku = positionAmount.Key.Sku.Substring(1, 6); - row = GetPositionRow(sku, positionAmount.Key.Group, TargetFile.skuCell.Column); + string sku = positionAmount.Key.Sku.Substring(1, 6); + row = GetPositionRow(TargetFile.SkuCell.EntireColumn, sku, positionAmount.Key.Group); - if (row != null) + if (row != null) + { + foreach (int column in columns) { - foreach (int column in columns) - { - Range cell = TargetFile.Sheet.Cells[row, column]; - cell.AddValue(positionAmount.Value); - } - - ResultBar.IncrementReplaced(); + Range cell = TargetFile.Sheet.Cells[row, column]; + cell.AddValue(positionAmount.Value); } - else - { - FillMissing(positionAmount, columns); - ResultBar.IncrementNotFound(); - } + ResultBar.IncrementReplaced(); + return; } + + FillMissing(positionAmount, columns); + ResultBar.IncrementNotFound(); } - protected private void FillMissing(KeyValuePair<Position, double> positionAmount, params int[] columns) + protected void FillMissing(KeyValuePair<Position, double> positionAmount, params int[] columns) { - int row = TargetFile.Sheet.Cells[TargetFile.Sheet.Rows.Count, TargetFile.skuCell.Column] + int row = TargetFile.Sheet.Cells[TargetFile.Sheet.Rows.Count, TargetFile.SkuCell.Column] .End[XlDirection.xlUp] .Row + 1; @@ -127,18 +111,18 @@ namespace RehauSku.PriceListTools previous.Copy(current); current.ClearContents(); - TargetFile.Sheet.Cells[row, TargetFile.groupCell.Column].Value2 = positionAmount.Key.Group; - TargetFile.Sheet.Cells[row, TargetFile.nameCell.Column].Value2 = positionAmount.Key.Name; + TargetFile.Sheet.Cells[row, TargetFile.GroupCell.Column].Value2 = positionAmount.Key.Group; + TargetFile.Sheet.Cells[row, TargetFile.NameCell.Column].Value2 = positionAmount.Key.Name; - if (TargetFile.oldSkuCell != null) + if (TargetFile.OldSkuCell != null) { - TargetFile.Sheet.Cells[row, TargetFile.skuCell.Column].Value2 = "Не найден"; - TargetFile.Sheet.Cells[row, TargetFile.oldSkuCell.Column].Value2 = positionAmount.Key.Sku; + TargetFile.Sheet.Cells[row, TargetFile.SkuCell.Column].Value2 = "Не найден"; + TargetFile.Sheet.Cells[row, TargetFile.OldSkuCell.Column].Value2 = positionAmount.Key.Sku; } else { - TargetFile.Sheet.Cells[row, TargetFile.skuCell.Column].Value2 = positionAmount.Key.Sku; + TargetFile.Sheet.Cells[row, TargetFile.SkuCell.Column].Value2 = positionAmount.Key.Sku; } foreach (int column in columns) @@ -148,40 +132,42 @@ namespace RehauSku.PriceListTools } } - protected private int? GetPositionRow(string sku, string group, int column) + protected int? GetPositionRow(Range range, string sku, string group) { - int? row = null; - Range foundCell = TargetFile.Sheet.Columns[column].Find(sku); + Range found = range.Find(sku); string foundGroupValue; - if (foundCell == null) return null; - - else + if (found == null) { - row = foundCell.Row; - foundGroupValue = TargetFile.Sheet.Cells[foundCell.Row, TargetFile.groupCell.Column].Value2.ToString(); + return null; } - if (string.IsNullOrEmpty(group) || group.Equals(foundGroupValue)) - return row; + int firstFoundRow = found.Row; - else - while (true) + while (true) + { + foundGroupValue = TargetFile.Sheet.Cells[found.Row, TargetFile.GroupCell.Column].Value2.ToString(); + + if (string.IsNullOrEmpty(group) || group.Equals(foundGroupValue)) { - foundCell = TargetFile.skuCell.EntireColumn.FindNext(foundCell); - if (foundCell == null) return row; + return found.Row; + } - foundGroupValue = TargetFile.Sheet.Cells[foundCell.Row, TargetFile.groupCell.Column].Value2.ToString(); - if (group.Equals(foundGroupValue)) return foundCell.Row; + found = range.FindNext(found); + + if (found.Row == firstFoundRow) + { + return null; } + } } - protected private void FilterByAmount() + protected void FilterByAmount() { AutoFilter filter = TargetFile.Sheet.AutoFilter; int startColumn = filter.Range.Column; - filter.Range.AutoFilter(TargetFile.amountCell.Column - startColumn + 1, "<>"); + filter.Range.AutoFilter(TargetFile.AmountCell.Column - startColumn + 1, "<>"); TargetFile.Sheet.Range["A1"].Activate(); } } diff --git a/src/PriceListTools/CombineTool.cs b/src/PriceListTools/CombineTool.cs index af9378c..eddf9e7 100644 --- a/src/PriceListTools/CombineTool.cs +++ b/src/PriceListTools/CombineTool.cs @@ -1,32 +1,49 @@ using Microsoft.Office.Interop.Excel; -using System.Collections.Generic; using RehauSku.Interface; +using System; +using System.Collections.Generic; using System.Linq; +using Dialog = RehauSku.Interface.Dialog; namespace RehauSku.PriceListTools { internal class CombineTool : AbstractTool { - public List<SourcePriceList> SourceFiles; + private List<SourcePriceList> SourceFiles { get; set; } + + public CombineTool() + { + string[] files = Dialog.GetMultiplyFiles(); + + if (files != null) + { + SourceFiles = SourcePriceList.GetSourceLists(files); + } + + else + { + throw new Exception("Не выбраны файлы"); + } + } - public void FillTarget() + public override void FillTarget() { ProgressBar = new ProgressBar("Заполняю строки...", SourceFiles.Sum(file => file.PositionAmount.Count)); ResultBar = new ResultBar(); foreach (SourcePriceList source in SourceFiles) { - TargetFile.Sheet.Columns[TargetFile.amountCell.Column] + TargetFile.Sheet.Columns[TargetFile.AmountCell.Column] .EntireColumn .Insert(XlInsertShiftDirection.xlShiftToRight, XlInsertFormatOrigin.xlFormatFromRightOrBelow); - Range newColumnHeader = TargetFile.Sheet.Cells[TargetFile.amountCell.Row, TargetFile.amountCell.Column - 1]; + 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); + FillPositionAmountToColumns(kvp, TargetFile.AmountCell.Column - 1, TargetFile.AmountCell.Column); ProgressBar.Update(); } } diff --git a/src/PriceListTools/ConvertTool.cs b/src/PriceListTools/ConvertTool.cs index d13c803..1bb02f4 100644 --- a/src/PriceListTools/ConvertTool.cs +++ b/src/PriceListTools/ConvertTool.cs @@ -1,38 +1,24 @@ using RehauSku.Interface; -using System; namespace RehauSku.PriceListTools { internal class ConvertTool : AbstractTool { - private SourcePriceList Current; + private SourcePriceList Current { get; set; } - public void GetCurrent() + public ConvertTool() { - try - { - Current = new SourcePriceList(ExcelApp.ActiveWorkbook); - } - - catch (Exception exception) - { - System.Windows.Forms.MessageBox.Show - (exception.Message, - "Ошибка распознавания", - System.Windows.Forms.MessageBoxButtons.OK, - System.Windows.Forms.MessageBoxIcon.Information); - throw exception; - } + Current = new SourcePriceList(ExcelApp.ActiveWorkbook); } - public void FillTarget() + public override void FillTarget() { ProgressBar = new ProgressBar("Заполняю строки...", Current.PositionAmount.Count); ResultBar = new ResultBar(); foreach (var kvp in Current.PositionAmount) { - FillPositionAmountToColumns(kvp, TargetFile.amountCell.Column); + FillPositionAmountToColumns(kvp, TargetFile.AmountCell.Column); ProgressBar.Update(); } diff --git a/src/PriceListTools/ExportTool.cs b/src/PriceListTools/ExportTool.cs index f341671..603de8b 100644 --- a/src/PriceListTools/ExportTool.cs +++ b/src/PriceListTools/ExportTool.cs @@ -8,22 +8,27 @@ namespace RehauSku.PriceListTools internal class ExportTool : AbstractTool { private Dictionary<Position, double> PositionAmount; - private Range Selection; + private readonly Range Selection; public ExportTool() { Selection = ExcelApp.Selection; + GetSelected(); + + if (PositionAmount.Count == 0) + { + throw new Exception("В выделенном диапазоне не найдены позиции для экспорта"); + } } - public void FillTarget() + public override void FillTarget() { - GetSelected(); ProgressBar = new ProgressBar("Заполняю строки...", PositionAmount.Count); ResultBar = new ResultBar(); foreach (var kvp in PositionAmount) { - FillPositionAmountToColumns(kvp, TargetFile.amountCell.Column); + FillPositionAmountToColumns(kvp, TargetFile.AmountCell.Column); ProgressBar.Update(); } diff --git a/src/PriceListTools/MergeTool.cs b/src/PriceListTools/MergeTool.cs index 0e3f1dc..179fb81 100644 --- a/src/PriceListTools/MergeTool.cs +++ b/src/PriceListTools/MergeTool.cs @@ -1,4 +1,5 @@ using RehauSku.Interface; +using System; using System.Collections.Generic; using System.Linq; @@ -6,9 +7,24 @@ namespace RehauSku.PriceListTools { internal class MergeTool : AbstractTool { - public List<SourcePriceList> SourceFiles; + private List<SourcePriceList> SourceFiles { get; set; } - public void FillTarget() + public MergeTool() + { + string[] files = Dialog.GetMultiplyFiles(); + + if (files != null) + { + SourceFiles = SourcePriceList.GetSourceLists(files); + } + + else + { + throw new Exception("Не выбраны файлы"); + } + } + + public override void FillTarget() { ProgressBar = new ProgressBar("Заполняю строки...", SourceFiles.Sum(x => x.PositionAmount.Count)); ResultBar = new ResultBar(); @@ -17,7 +33,7 @@ namespace RehauSku.PriceListTools { foreach (var kvp in source.PositionAmount) { - FillPositionAmountToColumns(kvp, TargetFile.amountCell.Column); + FillPositionAmountToColumns(kvp, TargetFile.AmountCell.Column); ProgressBar.Update(); } } diff --git a/src/PriceListTools/PriceListHeaders.cs b/src/PriceListTools/PriceListHeaders.cs new file mode 100644 index 0000000..74c7870 --- /dev/null +++ b/src/PriceListTools/PriceListHeaders.cs @@ -0,0 +1,11 @@ +namespace RehauSku.PriceListTools +{ + internal static class PriceListHeaders + { + public static readonly string Amount = "Кол-во"; + public static readonly string OldSku = "Прежний материал"; + public static readonly string Sku = "Актуальный материал"; + public static readonly string Group = "Программа"; + public static readonly string Name = "Наименование"; + } +}
\ No newline at end of file diff --git a/src/PriceListTools/SourcePriceList.cs b/src/PriceListTools/SourcePriceList.cs index 01637f8..d03d776 100644 --- a/src/PriceListTools/SourcePriceList.cs +++ b/src/PriceListTools/SourcePriceList.cs @@ -7,7 +7,6 @@ using RehauSku.Interface; namespace RehauSku.PriceListTools { - internal class SourcePriceList : AbstractPriceList { public Dictionary<Position, double> PositionAmount { get; private set; } @@ -24,10 +23,10 @@ namespace RehauSku.PriceListTools Range[] cells = new[] { - amountCell = Sheet.Cells.Find(amountHeader), - skuCell = Sheet.Cells.Find(skuHeader), - groupCell = Sheet.Cells.Find(groupHeader), - nameCell = Sheet.Cells.Find(nameHeader) + AmountCell = Sheet.Cells.Find(PriceListHeaders.Amount), + SkuCell = Sheet.Cells.Find(PriceListHeaders.Sku), + GroupCell = Sheet.Cells.Find(PriceListHeaders.Group), + NameCell = Sheet.Cells.Find(PriceListHeaders.Name) }; if (cells.Any(x => x == null)) @@ -76,15 +75,15 @@ namespace RehauSku.PriceListTools { PositionAmount = new Dictionary<Position, double>(); - for (int row = amountCell.Row + 1; row <= Sheet.Cells[Sheet.Rows.Count, amountCell.Column].End[XlDirection.xlUp].Row; row++) + for (int row = AmountCell.Row + 1; row <= Sheet.Cells[Sheet.Rows.Count, AmountCell.Column].End[XlDirection.xlUp].Row; row++) { - object amount = Sheet.Cells[row, amountCell.Column].Value2; + object amount = Sheet.Cells[row, AmountCell.Column].Value2; if (amount != null && (double)amount != 0) { - object group = Sheet.Cells[row, groupCell.Column].Value2; - object name = Sheet.Cells[row, nameCell.Column].Value2; - object sku = Sheet.Cells[row, skuCell.Column].Value2; + 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; diff --git a/src/PriceListTools/TargetPriceList.cs b/src/PriceListTools/TargetPriceList.cs index 5fb2bf9..2f23168 100644 --- a/src/PriceListTools/TargetPriceList.cs +++ b/src/PriceListTools/TargetPriceList.cs @@ -6,8 +6,7 @@ namespace RehauSku.PriceListTools { internal class TargetPriceList : AbstractPriceList { - private const string oldSkuHeader = "Прежний материал"; - public Range oldSkuCell { get; private set; } + public Range OldSkuCell { get; private set; } public TargetPriceList(Workbook workbook) { @@ -22,13 +21,13 @@ namespace RehauSku.PriceListTools Range[] cells = new[] { - amountCell = Sheet.Cells.Find(amountHeader), - skuCell = Sheet.Cells.Find(skuHeader), - groupCell = Sheet.Cells.Find(groupHeader), - nameCell = Sheet.Cells.Find(nameHeader) + 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(oldSkuHeader); + OldSkuCell = Sheet.Cells.Find(PriceListHeaders.OldSku); if (cells.Any(x => x == null)) { |