Create Tool Factory. Exception on empty selection in Export Tool.

This commit is contained in:
Sergey Chebotar 2022-02-12 16:02:55 +03:00
parent 41317cab71
commit 69865e96e3
6 changed files with 99 additions and 103 deletions

View File

@ -20,11 +20,11 @@ namespace RehauSku.Interface
<tabs> <tabs>
<tab id='rau' label='REHAU'> <tab id='rau' label='REHAU'>
<group id='priceList' label='Прайс-лист'> <group id='priceList' label='Прайс-лист'>
<button id='exportToPrice' getEnabled='GetExportEnabled' label='Экспорт в новый файл' size='normal' imageMso='PivotExportToExcel' onAction='OnExportPressed'/> <button id='exportToPrice' getEnabled='GetExportEnabled' label='Экспорт в новый файл' size='normal' imageMso='PivotExportToExcel' onAction='OnToolPressed'/>
<button id='convertPrice' getEnabled='GetConvertEnabled' label='Актуализировать' size='normal' imageMso='FileUpdate' onAction='OnConvertPressed'/> <button id='convertPrice' getEnabled='GetConvertEnabled' label='Актуализировать' size='normal' imageMso='FileUpdate' onAction='OnToolPressed'/>
<menu id='conjoinMenu' label='Объединить' imageMso='Copy'> <menu id='conjoinMenu' label='Объединить' imageMso='Copy'>
<button id='mergeFiles' label='Сложить' onAction='OnMergePressed'/> <button id='mergeFiles' label='Сложить' onAction='OnToolPressed'/>
<button id='combineFiles' label='По колонкам' onAction='OnCombinePressed'/> <button id='combineFiles' label='По колонкам' onAction='OnToolPressed'/>
</menu> </menu>
</group> </group>
<group id='rausettings' label='Настройки'> <group id='rausettings' label='Настройки'>
@ -48,30 +48,50 @@ namespace RehauSku.Interface
ribbonUi.InvalidateControl(id); ribbonUi.InvalidateControl(id);
} }
} }
public void OnSetPricePressed(IRibbonControl control)
public void OnMergePressed(IRibbonControl control)
{ {
MergeTool mergeTool = new MergeTool(); string path = Dialog.GetFilePath();
string[] files = Dialog.GetMultiplyFiles();
if (files != null) if (!string.IsNullOrEmpty(path))
{ {
mergeTool.SourceFiles = SourcePriceList.GetSourceLists(files); RegistryUtil.PriceListPath = path;
mergeTool.OpenNewPrice();
mergeTool.FillTarget();
} }
} }
public void OnCombinePressed(IRibbonControl control) public void OnToolPressed(IRibbonControl control)
{ {
CombineTool combineTool = new CombineTool(); try
string[] files = Dialog.GetMultiplyFiles();
if (files != null)
{ {
combineTool.SourceFiles = SourcePriceList.GetSourceLists(files); AbstractTool tool;
combineTool.OpenNewPrice(); switch (control.Id)
combineTool.FillTarget(); {
case "exportToPrice":
tool = new ExportTool();
break;
case "convertPrice":
tool = new ConvertTool();
break;
case "mergeFiles":
tool = new MergeTool();
break;
case "combineFiles":
tool = new CombineTool();
break;
default:
throw new Exception("Неизвестный инструмент");
}
tool.OpenNewPrice();
tool.FillTarget();
}
catch (Exception exception)
{
MessageBox.Show(exception.Message,
"Ошибка",
MessageBoxButtons.OK,
MessageBoxIcon.Information);
return;
} }
} }
@ -87,25 +107,6 @@ namespace RehauSku.Interface
} }
} }
public void OnExportPressed(IRibbonControl control)
{
try
{
ExportTool exportTool = new ExportTool();
exportTool.OpenNewPrice();
exportTool.FillTarget();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message,
"Ошибка",
MessageBoxButtons.OK,
MessageBoxIcon.Information);
return;
}
}
public bool GetExportEnabled(IRibbonControl control) public bool GetExportEnabled(IRibbonControl control)
{ {
if (AddIn.Excel.ActiveWorkbook == null) if (AddIn.Excel.ActiveWorkbook == null)
@ -117,24 +118,5 @@ namespace RehauSku.Interface
return selection.Columns.Count == 2; return selection.Columns.Count == 2;
} }
} }
public void OnConvertPressed(IRibbonControl control)
{
ConvertTool convertTool = new ConvertTool();
convertTool.GetCurrent();
convertTool.OpenNewPrice();
convertTool.FillTarget();
}
public void OnSetPricePressed(IRibbonControl control)
{
string path = Dialog.GetFilePath();
if (!string.IsNullOrEmpty(path))
{
RegistryUtil.PriceListPath = path;
}
}
} }
} }

View File

@ -4,7 +4,6 @@ using RehauSku.Interface;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Windows.Forms;
using Application = Microsoft.Office.Interop.Excel.Application; using Application = Microsoft.Office.Interop.Excel.Application;
using ProgressBar = RehauSku.Interface.ProgressBar; using ProgressBar = RehauSku.Interface.ProgressBar;
@ -12,10 +11,12 @@ namespace RehauSku.PriceListTools
{ {
internal abstract class AbstractTool internal abstract class AbstractTool
{ {
protected private Application ExcelApp = (Application)ExcelDnaUtil.Application; protected Application ExcelApp = (Application)ExcelDnaUtil.Application;
protected private TargetPriceList TargetFile; protected TargetPriceList TargetFile { get; set; }
protected private ResultBar ResultBar { get; set; } protected ResultBar ResultBar { get; set; }
protected private ProgressBar ProgressBar { get; set; } protected ProgressBar ProgressBar { get; set; }
public abstract void FillTarget();
public void OpenNewPrice() public void OpenNewPrice()
{ {
@ -23,12 +24,6 @@ namespace RehauSku.PriceListTools
.Cast<Workbook>() .Cast<Workbook>()
.FirstOrDefault(w => w.FullName == RegistryUtil.PriceListPath) != null) .FirstOrDefault(w => w.FullName == RegistryUtil.PriceListPath) != null)
{ {
MessageBox.Show
("Шаблонный файл редактируется в другом месте",
"Ошибка открытия шаблонного прайс-листа",
MessageBoxButtons.OK,
MessageBoxIcon.Information);
throw new ArgumentException("Шаблонный файл редактируется в другом месте"); throw new ArgumentException("Шаблонный файл редактируется в другом месте");
} }
@ -41,12 +36,6 @@ namespace RehauSku.PriceListTools
catch (Exception exception) catch (Exception exception)
{ {
MessageBox.Show
(exception.Message,
"Ошибка открытия шаблонного прайс-листа",
MessageBoxButtons.OK,
MessageBoxIcon.Information);
if (wb != null) if (wb != null)
{ {
wb.Close(); wb.Close();
@ -56,7 +45,7 @@ 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(TargetFile.skuCell.EntireColumn, positionAmount.Key.Sku, positionAmount.Key.Group); int? row = GetPositionRow(TargetFile.skuCell.EntireColumn, positionAmount.Key.Sku, positionAmount.Key.Group);
@ -108,7 +97,7 @@ namespace RehauSku.PriceListTools
ResultBar.IncrementNotFound(); 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] .End[XlDirection.xlUp]
@ -145,7 +134,7 @@ namespace RehauSku.PriceListTools
} }
} }
protected private int? GetPositionRow(Range range, string sku, string group) protected int? GetPositionRow(Range range, string sku, string group)
{ {
Range found = range.Find(sku); Range found = range.Find(sku);
string foundGroupValue; string foundGroupValue;
@ -175,7 +164,7 @@ namespace RehauSku.PriceListTools
} }
} }
protected private void FilterByAmount() protected void FilterByAmount()
{ {
AutoFilter filter = TargetFile.Sheet.AutoFilter; AutoFilter filter = TargetFile.Sheet.AutoFilter;
int startColumn = filter.Range.Column; int startColumn = filter.Range.Column;

View File

@ -1,15 +1,32 @@
using Microsoft.Office.Interop.Excel; using Microsoft.Office.Interop.Excel;
using System.Collections.Generic;
using RehauSku.Interface; using RehauSku.Interface;
using System;
using System.Collections.Generic;
using System.Linq; using System.Linq;
using Dialog = RehauSku.Interface.Dialog;
namespace RehauSku.PriceListTools namespace RehauSku.PriceListTools
{ {
internal class CombineTool : AbstractTool internal class CombineTool : AbstractTool
{ {
public List<SourcePriceList> SourceFiles; private List<SourcePriceList> SourceFiles { get; set; }
public void FillTarget() public CombineTool()
{
string[] files = Dialog.GetMultiplyFiles();
if (files != null)
{
SourceFiles = SourcePriceList.GetSourceLists(files);
}
else
{
throw new Exception("Не выбраны файлы");
}
}
public override void FillTarget()
{ {
ProgressBar = new ProgressBar("Заполняю строки...", SourceFiles.Sum(file => file.PositionAmount.Count)); ProgressBar = new ProgressBar("Заполняю строки...", SourceFiles.Sum(file => file.PositionAmount.Count));
ResultBar = new ResultBar(); ResultBar = new ResultBar();

View File

@ -5,27 +5,14 @@ namespace RehauSku.PriceListTools
{ {
internal class ConvertTool : AbstractTool internal class ConvertTool : AbstractTool
{ {
private SourcePriceList Current; private SourcePriceList Current { get; set; }
public void GetCurrent() public ConvertTool()
{ {
try Current = new SourcePriceList(ExcelApp.ActiveWorkbook);
{
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;
}
} }
public void FillTarget() public override void FillTarget()
{ {
ProgressBar = new ProgressBar("Заполняю строки...", Current.PositionAmount.Count); ProgressBar = new ProgressBar("Заполняю строки...", Current.PositionAmount.Count);
ResultBar = new ResultBar(); ResultBar = new ResultBar();

View File

@ -13,11 +13,16 @@ namespace RehauSku.PriceListTools
public ExportTool() public ExportTool()
{ {
Selection = ExcelApp.Selection; Selection = ExcelApp.Selection;
GetSelected();
if (PositionAmount.Count == 0)
{
throw new Exception("В выделенном диапазоне не найдены позиции для экспорта");
}
} }
public void FillTarget() public override void FillTarget()
{ {
GetSelected();
ProgressBar = new ProgressBar("Заполняю строки...", PositionAmount.Count); ProgressBar = new ProgressBar("Заполняю строки...", PositionAmount.Count);
ResultBar = new ResultBar(); ResultBar = new ResultBar();

View File

@ -1,4 +1,5 @@
using RehauSku.Interface; using RehauSku.Interface;
using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
@ -6,9 +7,24 @@ namespace RehauSku.PriceListTools
{ {
internal class MergeTool : AbstractTool 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)); ProgressBar = new ProgressBar("Заполняю строки...", SourceFiles.Sum(x => x.PositionAmount.Count));
ResultBar = new ResultBar(); ResultBar = new ResultBar();