From b9c96ae180ecd6386e35e97d4f5185bca0379965 Mon Sep 17 00:00:00 2001 From: Sergey Chebotar Date: Tue, 8 Feb 2022 13:40:23 +0300 Subject: [PATCH 1/8] Open target worbook readonly. Turn off Save As dialog. --- src/PriceListTools/AbstractTool.cs | 2 +- src/PriceListTools/CombineTool.cs | 2 +- src/PriceListTools/ConvertTool.cs | 2 +- src/PriceListTools/ExportTool.cs | 2 +- src/PriceListTools/MergeTool.cs | 2 +- 5 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/PriceListTools/AbstractTool.cs b/src/PriceListTools/AbstractTool.cs index b4e34cc..77126ff 100644 --- a/src/PriceListTools/AbstractTool.cs +++ b/src/PriceListTools/AbstractTool.cs @@ -18,7 +18,7 @@ namespace RehauSku.PriceListTools public void OpenNewPrice() { - Workbook wb = ExcelApp.Workbooks.Open(RegistryUtil.PriceListPath); + Workbook wb = ExcelApp.Workbooks.Open(RegistryUtil.PriceListPath, 0, true); try { diff --git a/src/PriceListTools/CombineTool.cs b/src/PriceListTools/CombineTool.cs index af9378c..cdb8d20 100644 --- a/src/PriceListTools/CombineTool.cs +++ b/src/PriceListTools/CombineTool.cs @@ -34,7 +34,7 @@ namespace RehauSku.PriceListTools FilterByAmount(); ResultBar.Update(); - Interface.Dialog.SaveWorkbookAs(); + //Interface.Dialog.SaveWorkbookAs(); ExcelApp.StatusBar = false; } } diff --git a/src/PriceListTools/ConvertTool.cs b/src/PriceListTools/ConvertTool.cs index d13c803..0e1250d 100644 --- a/src/PriceListTools/ConvertTool.cs +++ b/src/PriceListTools/ConvertTool.cs @@ -39,7 +39,7 @@ namespace RehauSku.PriceListTools FilterByAmount(); ResultBar.Update(); - Dialog.SaveWorkbookAs(); + //Dialog.SaveWorkbookAs(); ExcelApp.StatusBar = false; } } diff --git a/src/PriceListTools/ExportTool.cs b/src/PriceListTools/ExportTool.cs index f341671..9436d1a 100644 --- a/src/PriceListTools/ExportTool.cs +++ b/src/PriceListTools/ExportTool.cs @@ -30,7 +30,7 @@ namespace RehauSku.PriceListTools FilterByAmount(); ResultBar.Update(); - Interface.Dialog.SaveWorkbookAs(); + //Interface.Dialog.SaveWorkbookAs(); ExcelApp.StatusBar = false; } diff --git a/src/PriceListTools/MergeTool.cs b/src/PriceListTools/MergeTool.cs index 0e3f1dc..a7d6469 100644 --- a/src/PriceListTools/MergeTool.cs +++ b/src/PriceListTools/MergeTool.cs @@ -25,7 +25,7 @@ namespace RehauSku.PriceListTools FilterByAmount(); ResultBar.Update(); - Dialog.SaveWorkbookAs(); + //Dialog.SaveWorkbookAs(); ExcelApp.StatusBar = false; } } From d341ef82c75fe964d2924c86b27e1421dd44ec2a Mon Sep 17 00:00:00 2001 From: Sergey Chebotar Date: Tue, 8 Feb 2022 16:03:28 +0300 Subject: [PATCH 2/8] Revert "Open target worbook readonly. Turn off Save As dialog." This reverts commit b9c96ae180ecd6386e35e97d4f5185bca0379965. --- src/PriceListTools/AbstractTool.cs | 2 +- src/PriceListTools/CombineTool.cs | 2 +- src/PriceListTools/ConvertTool.cs | 2 +- src/PriceListTools/ExportTool.cs | 2 +- src/PriceListTools/MergeTool.cs | 2 +- 5 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/PriceListTools/AbstractTool.cs b/src/PriceListTools/AbstractTool.cs index 77126ff..b4e34cc 100644 --- a/src/PriceListTools/AbstractTool.cs +++ b/src/PriceListTools/AbstractTool.cs @@ -18,7 +18,7 @@ namespace RehauSku.PriceListTools public void OpenNewPrice() { - Workbook wb = ExcelApp.Workbooks.Open(RegistryUtil.PriceListPath, 0, true); + Workbook wb = ExcelApp.Workbooks.Open(RegistryUtil.PriceListPath); try { diff --git a/src/PriceListTools/CombineTool.cs b/src/PriceListTools/CombineTool.cs index cdb8d20..af9378c 100644 --- a/src/PriceListTools/CombineTool.cs +++ b/src/PriceListTools/CombineTool.cs @@ -34,7 +34,7 @@ namespace RehauSku.PriceListTools FilterByAmount(); ResultBar.Update(); - //Interface.Dialog.SaveWorkbookAs(); + Interface.Dialog.SaveWorkbookAs(); ExcelApp.StatusBar = false; } } diff --git a/src/PriceListTools/ConvertTool.cs b/src/PriceListTools/ConvertTool.cs index 0e1250d..d13c803 100644 --- a/src/PriceListTools/ConvertTool.cs +++ b/src/PriceListTools/ConvertTool.cs @@ -39,7 +39,7 @@ namespace RehauSku.PriceListTools FilterByAmount(); ResultBar.Update(); - //Dialog.SaveWorkbookAs(); + Dialog.SaveWorkbookAs(); ExcelApp.StatusBar = false; } } diff --git a/src/PriceListTools/ExportTool.cs b/src/PriceListTools/ExportTool.cs index 9436d1a..f341671 100644 --- a/src/PriceListTools/ExportTool.cs +++ b/src/PriceListTools/ExportTool.cs @@ -30,7 +30,7 @@ namespace RehauSku.PriceListTools FilterByAmount(); ResultBar.Update(); - //Interface.Dialog.SaveWorkbookAs(); + Interface.Dialog.SaveWorkbookAs(); ExcelApp.StatusBar = false; } diff --git a/src/PriceListTools/MergeTool.cs b/src/PriceListTools/MergeTool.cs index a7d6469..0e3f1dc 100644 --- a/src/PriceListTools/MergeTool.cs +++ b/src/PriceListTools/MergeTool.cs @@ -25,7 +25,7 @@ namespace RehauSku.PriceListTools FilterByAmount(); ResultBar.Update(); - //Dialog.SaveWorkbookAs(); + Dialog.SaveWorkbookAs(); ExcelApp.StatusBar = false; } } From 14a122546258005783703329424b9992a8db501b Mon Sep 17 00:00:00 2001 From: Sergey Chebotar Date: Tue, 8 Feb 2022 16:06:30 +0300 Subject: [PATCH 3/8] Open template readonly --- src/PriceListTools/AbstractTool.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/PriceListTools/AbstractTool.cs b/src/PriceListTools/AbstractTool.cs index b4e34cc..ea713c7 100644 --- a/src/PriceListTools/AbstractTool.cs +++ b/src/PriceListTools/AbstractTool.cs @@ -18,7 +18,7 @@ namespace RehauSku.PriceListTools public void OpenNewPrice() { - Workbook wb = ExcelApp.Workbooks.Open(RegistryUtil.PriceListPath); + Workbook wb = ExcelApp.Workbooks.Open(RegistryUtil.PriceListPath, null, true); try { From b954844de9e6ab3f45ee90a67e900ba57ef74ec4 Mon Sep 17 00:00:00 2001 From: Sergey Chebotar Date: Tue, 8 Feb 2022 16:10:45 +0300 Subject: [PATCH 4/8] Keep workbook open on save as dialog cancellation --- src/Interface/Dialog.cs | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) 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); From 81f30303735c0a9cd367a2e624e2444726611fcf Mon Sep 17 00:00:00 2001 From: Sergey Chebotar Date: Tue, 8 Feb 2022 16:19:49 +0300 Subject: [PATCH 5/8] Add range.Addvalue extension --- src/AddIn/WorksheetExtensions.cs | 13 +++++++++++++ 1 file changed, 13 insertions(+) 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; + } + } } } From 1272c0104e19a111674beab2558cd0a8a9f9295b Mon Sep 17 00:00:00 2001 From: Sergey Chebotar Date: Tue, 8 Feb 2022 16:27:53 +0300 Subject: [PATCH 6/8] Refactoring AbstractTool --- src/PriceListTools/AbstractTool.cs | 90 ++++++++++-------------------- 1 file changed, 28 insertions(+), 62 deletions(-) diff --git a/src/PriceListTools/AbstractTool.cs b/src/PriceListTools/AbstractTool.cs index ea713c7..b9e3c29 100644 --- a/src/PriceListTools/AbstractTool.cs +++ b/src/PriceListTools/AbstractTool.cs @@ -45,77 +45,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 +124,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); } } From 0b5bea215529e6d8aa2b76197cf3fe8a49a9e2b7 Mon Sep 17 00:00:00 2001 From: Sergey Chebotar Date: Tue, 8 Feb 2022 16:46:53 +0300 Subject: [PATCH 7/8] Exception on same name file try open --- src/PriceListTools/AbstractTool.cs | 13 +++++++++---- src/PriceListTools/TargetPriceList.cs | 6 ++++++ 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/src/PriceListTools/AbstractTool.cs b/src/PriceListTools/AbstractTool.cs index b9e3c29..f959fb5 100644 --- a/src/PriceListTools/AbstractTool.cs +++ b/src/PriceListTools/AbstractTool.cs @@ -25,15 +25,20 @@ namespace RehauSku.PriceListTools 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; } } 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; From acaea679201b5b05af705ef3f6af632ec5bddc81 Mon Sep 17 00:00:00 2001 From: Sergey Chebotar Date: Tue, 8 Feb 2022 17:26:05 +0300 Subject: [PATCH 8/8] Throw exception if template target file is open --- src/PriceListTools/AbstractTool.cs | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/src/PriceListTools/AbstractTool.cs b/src/PriceListTools/AbstractTool.cs index f959fb5..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,6 +19,19 @@ namespace RehauSku.PriceListTools public void OpenNewPrice() { + 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