Add CombineTool for conjoining files into columns
This commit is contained in:
parent
fbe97d706c
commit
8672fa8f6b
95
src/PriceListTools/CombineTool.cs
Normal file
95
src/PriceListTools/CombineTool.cs
Normal file
@ -0,0 +1,95 @@
|
||||
using ExcelDna.Integration;
|
||||
using Microsoft.Office.Interop.Excel;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
|
||||
namespace RehauSku.PriceListTools
|
||||
{
|
||||
class CombineTool : ConjoinTool, IDisposable, IConjoinTool
|
||||
{
|
||||
private Dictionary<string, double>[] SkuAmount { get; set; }
|
||||
private string[] FileNames { get; set; }
|
||||
|
||||
public CombineTool()
|
||||
{
|
||||
ExcelApp = (Application)ExcelDnaUtil.Application;
|
||||
}
|
||||
|
||||
public void CollectSkuAmount(string[] files)
|
||||
{
|
||||
FileNames = files.Select(x => Path.GetFileNameWithoutExtension(x)).ToArray();
|
||||
SkuAmount = new Dictionary<string, double>[files.Length];
|
||||
|
||||
ExcelApp.ScreenUpdating = false;
|
||||
|
||||
for (int i = 0; i < files.Length; i++)
|
||||
{
|
||||
Workbook wb = ExcelApp.Workbooks.Open(files[i]);
|
||||
|
||||
try
|
||||
{
|
||||
PriceList priceList = new PriceList(wb);
|
||||
SkuAmount[i] = new Dictionary<string, double>();
|
||||
SkuAmount[i].AddValuesFromPriceList(priceList);
|
||||
}
|
||||
|
||||
catch (Exception ex)
|
||||
{
|
||||
System.Windows.Forms.MessageBox.Show
|
||||
($"{wb.Name} не является файлом прайс-листа \n\n {ex.Message}",
|
||||
"Неверный файл прайс-листа!",
|
||||
System.Windows.Forms.MessageBoxButtons.OK,
|
||||
System.Windows.Forms.MessageBoxIcon.Error);
|
||||
}
|
||||
|
||||
finally
|
||||
{
|
||||
wb.Close();
|
||||
}
|
||||
}
|
||||
|
||||
ExcelApp.ScreenUpdating = true;
|
||||
}
|
||||
|
||||
public void ExportToFile(string exportFile)
|
||||
{
|
||||
if (SkuAmount.Sum(d => d.Count) < 1)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
Workbook wb = ExcelApp.Workbooks.Open(exportFile);
|
||||
PriceList priceList;
|
||||
|
||||
try
|
||||
{
|
||||
priceList = new PriceList(wb);
|
||||
priceList.FillWithValues(SkuAmount, FileNames);
|
||||
}
|
||||
|
||||
catch (Exception ex)
|
||||
{
|
||||
System.Windows.Forms.MessageBox.Show
|
||||
($"{RegistryUtil.PriceListPath} не является файлом прайс-листа \n\n {ex.Message}",
|
||||
"Неверный файл прайс-листа!",
|
||||
System.Windows.Forms.MessageBoxButtons.OK,
|
||||
System.Windows.Forms.MessageBoxIcon.Error);
|
||||
|
||||
wb.Close();
|
||||
}
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
//Dispose(true);
|
||||
GC.SuppressFinalize(this);
|
||||
}
|
||||
|
||||
//protected virtual void Dispose(bool disposing)
|
||||
//{
|
||||
|
||||
//}
|
||||
}
|
||||
}
|
9
src/PriceListTools/ConjoinTool.cs
Normal file
9
src/PriceListTools/ConjoinTool.cs
Normal file
@ -0,0 +1,9 @@
|
||||
using Microsoft.Office.Interop.Excel;
|
||||
|
||||
namespace RehauSku.PriceListTools
|
||||
{
|
||||
internal class ConjoinTool
|
||||
{
|
||||
protected Application ExcelApp;
|
||||
}
|
||||
}
|
@ -85,7 +85,7 @@ namespace RehauSku.PriceListTools
|
||||
try
|
||||
{
|
||||
PriceList priceList = new PriceList(wb);
|
||||
priceList.Fill(SkuAmount);
|
||||
priceList.FillWithValues(SkuAmount);
|
||||
}
|
||||
|
||||
catch(Exception ex)
|
||||
|
8
src/PriceListTools/IConjoinTool.cs
Normal file
8
src/PriceListTools/IConjoinTool.cs
Normal file
@ -0,0 +1,8 @@
|
||||
namespace RehauSku.PriceListTools
|
||||
{
|
||||
internal interface IConjoinTool
|
||||
{
|
||||
void CollectSkuAmount(string[] files);
|
||||
void ExportToFile(string exportFile);
|
||||
}
|
||||
}
|
@ -5,18 +5,17 @@ using System.Collections.Generic;
|
||||
|
||||
namespace RehauSku.PriceListTools
|
||||
{
|
||||
class MergeTool : IDisposable
|
||||
class MergeTool : ConjoinTool, IDisposable, IConjoinTool
|
||||
{
|
||||
private Application ExcelApp;
|
||||
private Dictionary<string, double> SkuAmount { get; set; }
|
||||
|
||||
public MergeTool()
|
||||
{
|
||||
this.ExcelApp = (Application)ExcelDnaUtil.Application;
|
||||
this.SkuAmount = new Dictionary<string, double>();
|
||||
ExcelApp = (Application)ExcelDnaUtil.Application;
|
||||
SkuAmount = new Dictionary<string, double>();
|
||||
}
|
||||
|
||||
public void AddSkuAmountToDict(string[] files)
|
||||
public void CollectSkuAmount(string[] files)
|
||||
{
|
||||
ExcelApp.ScreenUpdating = false;
|
||||
foreach (string file in files)
|
||||
@ -26,13 +25,13 @@ namespace RehauSku.PriceListTools
|
||||
try
|
||||
{
|
||||
PriceList priceList = new PriceList(wb);
|
||||
SkuAmount.AddValues(priceList);
|
||||
SkuAmount.AddValuesFromPriceList(priceList);
|
||||
}
|
||||
|
||||
catch (Exception ex)
|
||||
{
|
||||
System.Windows.Forms.MessageBox.Show
|
||||
( $"{wb.Name} не является файлом прайс-листа \n\n {ex.Message}",
|
||||
($"{wb.Name} не является файлом прайс-листа \n\n {ex.Message}",
|
||||
"Неверный файл прайс-листа!",
|
||||
System.Windows.Forms.MessageBoxButtons.OK,
|
||||
System.Windows.Forms.MessageBoxIcon.Error);
|
||||
@ -46,7 +45,7 @@ namespace RehauSku.PriceListTools
|
||||
ExcelApp.ScreenUpdating = true;
|
||||
}
|
||||
|
||||
public void ExportToNewFile(string exportFile)
|
||||
public void ExportToFile(string exportFile)
|
||||
{
|
||||
if (SkuAmount.Count < 1)
|
||||
{
|
||||
@ -59,7 +58,7 @@ namespace RehauSku.PriceListTools
|
||||
try
|
||||
{
|
||||
priceList = new PriceList(wb);
|
||||
priceList.Fill(SkuAmount);
|
||||
priceList.FillWithValues(SkuAmount);
|
||||
}
|
||||
|
||||
catch (Exception ex)
|
||||
|
@ -1,5 +1,6 @@
|
||||
using Microsoft.Office.Interop.Excel;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
|
||||
namespace RehauSku.PriceListTools
|
||||
{
|
||||
@ -9,31 +10,25 @@ namespace RehauSku.PriceListTools
|
||||
public readonly PriceListSheet OfferSheet;
|
||||
public readonly PriceListSheet ActiveSheet;
|
||||
|
||||
private const string _amountHeader = "Кол-во";
|
||||
private const string _skuHeader = "Актуальный материал";
|
||||
private const string _offerSheetHeader = "КП";
|
||||
private const string amountHeader = "Кол-во";
|
||||
private const string skuHeader = "Актуальный материал";
|
||||
private const string offerSheetHeader = "КП";
|
||||
|
||||
public PriceList(Workbook workbook)
|
||||
{
|
||||
Workbook = workbook;
|
||||
OfferSheet = new PriceListSheet(workbook.Sheets[_offerSheetHeader]);
|
||||
OfferSheet = new PriceListSheet(workbook.Sheets[offerSheetHeader]);
|
||||
|
||||
Worksheet active = workbook.ActiveSheet;
|
||||
|
||||
if (active.Name == _offerSheetHeader)
|
||||
if (active.Name == offerSheetHeader)
|
||||
ActiveSheet = OfferSheet;
|
||||
|
||||
else
|
||||
ActiveSheet = new PriceListSheet(active);
|
||||
}
|
||||
|
||||
public bool IsValid()
|
||||
{
|
||||
return OfferSheet.IsValid() &&
|
||||
ActiveSheet.IsValid();
|
||||
}
|
||||
|
||||
public void Fill(Dictionary<string, double> values)
|
||||
public void FillWithValues(Dictionary<string, double> values)
|
||||
{
|
||||
Worksheet ws = OfferSheet.sheet;
|
||||
ws.Activate();
|
||||
@ -45,6 +40,7 @@ namespace RehauSku.PriceListTools
|
||||
foreach (KeyValuePair<string, double> kvp in values)
|
||||
{
|
||||
Range cell = ws.Columns[skuColumn].Find(kvp.Key);
|
||||
|
||||
if (cell == null)
|
||||
{
|
||||
System.Windows.Forms.MessageBox.Show
|
||||
@ -53,6 +49,7 @@ namespace RehauSku.PriceListTools
|
||||
System.Windows.Forms.MessageBoxButtons.OK,
|
||||
System.Windows.Forms.MessageBoxIcon.Information);
|
||||
}
|
||||
|
||||
else
|
||||
{
|
||||
ws.Cells[cell.Row, amountColumn].Value = kvp.Value;
|
||||
@ -68,33 +65,82 @@ namespace RehauSku.PriceListTools
|
||||
ws.Application.StatusBar = $"Экспортировано {exportedValues} строк из {values.Count}";
|
||||
}
|
||||
|
||||
public void FillWithValues(Dictionary<string, double>[] values, string[] filenames)
|
||||
{
|
||||
Worksheet ws = OfferSheet.sheet;
|
||||
ws.Activate();
|
||||
|
||||
int amountColumn = OfferSheet.amountColumn.Value;
|
||||
int skuColumn = OfferSheet.skuColumn.Value;
|
||||
int headerColumn = OfferSheet.headerRow.Value;
|
||||
|
||||
int exportedValues = 0;
|
||||
|
||||
for (int i = 0; i < values.Length; i++)
|
||||
{
|
||||
ws.Columns[amountColumn]
|
||||
.EntireColumn
|
||||
.Insert(XlInsertShiftDirection.xlShiftToRight, XlInsertFormatOrigin.xlFormatFromRightOrBelow);
|
||||
|
||||
foreach (var kvp in values[i])
|
||||
{
|
||||
Range cell = ws.Columns[skuColumn].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);
|
||||
}
|
||||
|
||||
else
|
||||
{
|
||||
ws.Cells[cell.Row, amountColumn].Value2 = kvp.Value;
|
||||
Range sumCell = ws.Cells[cell.Row, amountColumn + i + 1];
|
||||
|
||||
if (sumCell.Value2 == null)
|
||||
sumCell.Value2 = kvp.Value;
|
||||
else
|
||||
sumCell.Value2 += kvp.Value;
|
||||
|
||||
exportedValues++;
|
||||
}
|
||||
}
|
||||
|
||||
ws.Cells[headerColumn, amountColumn].Value2 = filenames[i];
|
||||
}
|
||||
|
||||
AutoFilter filter = ws.AutoFilter;
|
||||
int firstFilterColumn = filter.Range.Column;
|
||||
|
||||
filter.Range.AutoFilter(amountColumn - firstFilterColumn + 1 + values.Length, "<>");
|
||||
ws.Range["A1"].Activate();
|
||||
ws.Application.StatusBar = $"Экспортировано {exportedValues} строк из {values.Sum(x => x.Count)}";
|
||||
}
|
||||
|
||||
public class PriceListSheet
|
||||
{
|
||||
public readonly Worksheet sheet;
|
||||
public readonly int? headerRow;
|
||||
public readonly int? amountColumn;
|
||||
|
||||
public readonly int? skuColumn;
|
||||
public object[,] amountCells;
|
||||
public readonly int? amountColumn;
|
||||
|
||||
public object[,] skuCells;
|
||||
public object[,] amountCells;
|
||||
|
||||
public PriceListSheet(Worksheet sheet)
|
||||
{
|
||||
this.sheet = sheet;
|
||||
headerRow = sheet.Cells.Find(_amountHeader).Row;
|
||||
amountColumn = sheet.Cells.Find(_amountHeader).Column;
|
||||
skuColumn = sheet.Cells.Find(_skuHeader).Column;
|
||||
headerRow = sheet.Cells.Find(amountHeader).Row;
|
||||
amountColumn = sheet.Cells.Find(amountHeader).Column;
|
||||
skuColumn = sheet.Cells.Find(skuHeader).Column;
|
||||
|
||||
amountCells = sheet.Columns[amountColumn].Value2;
|
||||
skuCells = sheet.Columns[skuColumn].Value2;
|
||||
}
|
||||
|
||||
public bool IsValid()
|
||||
{
|
||||
return sheet != null &&
|
||||
headerRow != null &&
|
||||
amountColumn != null &&
|
||||
skuColumn != null;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -14,7 +14,7 @@ namespace RehauSku.PriceListTools
|
||||
return path;
|
||||
}
|
||||
|
||||
public static void AddValues(this Dictionary<string, double> SkuAmount, PriceList priceList)
|
||||
public static void AddValuesFromPriceList(this Dictionary<string, double> SkuAmount, PriceList priceList)
|
||||
{
|
||||
object[,] amountCells = priceList.ActiveSheet.amountCells;
|
||||
object[,] skuCells = priceList.ActiveSheet.skuCells;
|
||||
|
@ -106,6 +106,9 @@
|
||||
<Compile Include="Assistant\ParseUtil.cs" />
|
||||
<Compile Include="Assistant\RequestModifier.cs" />
|
||||
<Compile Include="Assistant\SkuExtensions.cs" />
|
||||
<Compile Include="PriceListTools\CombineTool.cs" />
|
||||
<Compile Include="PriceListTools\ConjoinTool.cs" />
|
||||
<Compile Include="PriceListTools\IConjoinTool.cs" />
|
||||
<Compile Include="PriceListTools\MergeTool.cs" />
|
||||
<Compile Include="PriceListTools\PriceList.cs" />
|
||||
<Compile Include="PriceListTools\PriceListUtil.cs" />
|
||||
|
@ -18,7 +18,10 @@ namespace RehauSku.Ribbon
|
||||
<tab id='rau' label='REHAU'>
|
||||
<group id='priceList' label='Прайс-лист'>
|
||||
<button id='exportToPrice' label='Экспорт в новый файл' size='normal' imageMso='PivotExportToExcel' onAction='OnExportPressed'/>
|
||||
<button id='mergeFiles' label='Объединить' size='normal' imageMso='Copy' onAction='OnMergePressed'/>
|
||||
<menu id='conjoinMenu' label='Объединить' imageMso='Copy'>
|
||||
<button id='mergeFiles' label='Сложить' onAction='OnMergePressed'/>
|
||||
<button id='combineFiles' label='По колонкам' onAction='OnCombinePressed'/>
|
||||
</menu>
|
||||
</group>
|
||||
<group id='rausettings' label='Настройки'>
|
||||
<button id='setPriceList' label='Файл прайс-листа' size='normal' imageMso='CurrentViewSettings' onAction='OnSetPricePressed'/>
|
||||
@ -29,14 +32,27 @@ namespace RehauSku.Ribbon
|
||||
</customUI>";
|
||||
}
|
||||
|
||||
// <dropDown id = 'dd1' label = 'Drop dynamic' getItemCount = 'fncGetItemCountDrop' getItemLabel = 'fncGetItemLabelDrop' onAction = 'fncOnActionDrop'/>
|
||||
|
||||
public void OnMergePressed(IRibbonControl control)
|
||||
{
|
||||
using (MergeTool mergeTool = new MergeTool())
|
||||
{
|
||||
string[] files = Dialog.GetMultiplyFiles();
|
||||
mergeTool.AddSkuAmountToDict(files);
|
||||
mergeTool.CollectSkuAmount(files);
|
||||
string exportFile = PriceListUtil.CreateNewExportFile();
|
||||
mergeTool.ExportToNewFile(exportFile);
|
||||
mergeTool.ExportToFile(exportFile);
|
||||
}
|
||||
}
|
||||
|
||||
public void OnCombinePressed(IRibbonControl control)
|
||||
{
|
||||
using (CombineTool combineTool = new CombineTool())
|
||||
{
|
||||
string[] files = Dialog.GetMultiplyFiles();
|
||||
combineTool.CollectSkuAmount(files);
|
||||
string exportFile = PriceListUtil.CreateNewExportFile();
|
||||
combineTool.ExportToFile(exportFile);
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user