diff --git a/src/AddIn/WorksheetExtensions.cs b/src/AddIn/WorksheetExtensions.cs index 51ce13a..cffa55c 100644 --- a/src/AddIn/WorksheetExtensions.cs +++ b/src/AddIn/WorksheetExtensions.cs @@ -27,6 +27,19 @@ namespace RehauSku return cells.All(x => x != null); } + + public static void AddValue(this Range range, double value) + { + if (range.Value2 == null) + { + range.Value2 = value; + } + + else + { + range.Value2 += value; + } + } } } diff --git a/src/Interface/Dialog.cs b/src/Interface/Dialog.cs index 23f65d7..e6c7955 100644 --- a/src/Interface/Dialog.cs +++ b/src/Interface/Dialog.cs @@ -46,12 +46,7 @@ namespace RehauSku.Interface dialog.FileName = workbook.Name; dialog.Filter = "Файлы Excel (*.xls;*.xlsx;*.xlsm)|*.xls;*.xlsx;*.xlsm"; - if (dialog.ShowDialog() == DialogResult.Cancel) - { - workbook.Close(false); - } - - else + if (dialog.ShowDialog() == DialogResult.OK) { string fileName = dialog.FileName; workbook.SaveAs(fileName); diff --git a/src/PriceListTools/AbstractTool.cs b/src/PriceListTools/AbstractTool.cs index b4e34cc..d312315 100644 --- a/src/PriceListTools/AbstractTool.cs +++ b/src/PriceListTools/AbstractTool.cs @@ -3,6 +3,7 @@ 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; @@ -18,22 +19,40 @@ namespace RehauSku.PriceListTools public void OpenNewPrice() { - Workbook wb = ExcelApp.Workbooks.Open(RegistryUtil.PriceListPath); + if (ExcelApp.Workbooks + .Cast() + .FirstOrDefault(w => w.FullName == RegistryUtil.PriceListPath) != null) + { + MessageBox.Show + ("Шаблонный файл редактируется в другом месте", + "Ошибка открытия шаблонного прайс-листа", + MessageBoxButtons.OK, + MessageBoxIcon.Information); + + throw new ArgumentException("Шаблонный файл редактируется в другом месте"); + } + + Workbook wb = ExcelApp.Workbooks.Open(RegistryUtil.PriceListPath, null, true); try { TargetFile = new TargetPriceList(wb); } - catch (Exception ex) + catch (Exception exception) { MessageBox.Show - (ex.Message, + (exception.Message, "Ошибка открытия шаблонного прайс-листа", MessageBoxButtons.OK, MessageBoxIcon.Information); - wb.Close(); - throw ex; + + if (wb != null) + { + wb.Close(); + } + + throw exception; } } @@ -45,77 +64,50 @@ namespace RehauSku.PriceListTools { foreach (int column in columns) { - Range sumCell = TargetFile.Sheet.Cells[row, column]; - - if (sumCell.Value2 == null) - { - sumCell.Value2 = positionAmount.Value; - } - - else - { - sumCell.Value2 += positionAmount.Value; - } + Range cell = TargetFile.Sheet.Cells[row, column]; + cell.AddValue(positionAmount.Value); } ResultBar.IncrementSuccess(); - return; } - if (TargetFile.oldSkuCell != null) + else if (TargetFile.oldSkuCell != null) { - Range foundCell = TargetFile.oldSkuCell.EntireColumn.Find(positionAmount.Key.Sku); + row = GetPositionRow(positionAmount.Key.Sku, positionAmount.Key.Group, TargetFile.oldSkuCell.Column); - if (foundCell != null) + if (row != null) { - row = foundCell.Row; - foreach (int column in columns) { - if (TargetFile.Sheet.Cells[row, column].Value2 == null) - { - TargetFile.Sheet.Cells[row, column].Value2 = positionAmount.Value; - } - - else - { - TargetFile.Sheet.Cells[row, column].Value2 += positionAmount.Value; - } + Range cell = TargetFile.Sheet.Cells[row, column]; + cell.AddValue(positionAmount.Value); } ResultBar.IncrementReplaced(); - return; } } - string sku = positionAmount.Key.Sku.Substring(1, 6); - row = GetPositionRow(sku, positionAmount.Key.Group, TargetFile.skuCell.Column); - - if (row != null) - { - foreach (int column in columns) - { - Range amountCell = TargetFile.Sheet.Cells[row, column]; - - if (amountCell.Value2 == null) - { - amountCell.Value2 = positionAmount.Value; - } - - else - { - amountCell.Value2 += positionAmount.Value; - } - } - - ResultBar.IncrementReplaced(); - return; - } - else { - FillMissing(positionAmount, columns); - ResultBar.IncrementNotFound(); + string sku = positionAmount.Key.Sku.Substring(1, 6); + row = GetPositionRow(sku, positionAmount.Key.Group, TargetFile.skuCell.Column); + + if (row != null) + { + foreach (int column in columns) + { + Range cell = TargetFile.Sheet.Cells[row, column]; + cell.AddValue(positionAmount.Value); + } + + ResultBar.IncrementReplaced(); + } + + else + { + FillMissing(positionAmount, columns); + ResultBar.IncrementNotFound(); + } } } @@ -151,15 +143,8 @@ namespace RehauSku.PriceListTools foreach (int column in columns) { - if (TargetFile.Sheet.Cells[row, column].Value2 == null) - { - TargetFile.Sheet.Cells[row, column].Value2 = positionAmount.Value; - } - - else - { - TargetFile.Sheet.Cells[row, column].Value2 += positionAmount.Value; - } + Range cell = TargetFile.Sheet.Cells[row, column]; + cell.AddValue(positionAmount.Value); } } diff --git a/src/PriceListTools/TargetPriceList.cs b/src/PriceListTools/TargetPriceList.cs index 32b071c..5fb2bf9 100644 --- a/src/PriceListTools/TargetPriceList.cs +++ b/src/PriceListTools/TargetPriceList.cs @@ -11,6 +11,12 @@ namespace RehauSku.PriceListTools public TargetPriceList(Workbook workbook) { + if (workbook == null) + { + throw new ArgumentException("Невозможно открыть книгу шаблонного файла. " + + "Возможно открыт файл с именем, совпадающим с именем шаблонного файла."); + } + Sheet = workbook.ActiveSheet; Name = workbook.FullName;