Add CombineTool for conjoining files into columns

This commit is contained in:
Sergey Chebotar 2022-01-07 19:04:07 +03:00
parent fbe97d706c
commit 8672fa8f6b
9 changed files with 219 additions and 43 deletions

View 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)
//{
//}
}
}

View File

@ -0,0 +1,9 @@
using Microsoft.Office.Interop.Excel;
namespace RehauSku.PriceListTools
{
internal class ConjoinTool
{
protected Application ExcelApp;
}
}

View File

@ -85,7 +85,7 @@ namespace RehauSku.PriceListTools
try try
{ {
PriceList priceList = new PriceList(wb); PriceList priceList = new PriceList(wb);
priceList.Fill(SkuAmount); priceList.FillWithValues(SkuAmount);
} }
catch(Exception ex) catch(Exception ex)

View File

@ -0,0 +1,8 @@
namespace RehauSku.PriceListTools
{
internal interface IConjoinTool
{
void CollectSkuAmount(string[] files);
void ExportToFile(string exportFile);
}
}

View File

@ -5,18 +5,17 @@ using System.Collections.Generic;
namespace RehauSku.PriceListTools namespace RehauSku.PriceListTools
{ {
class MergeTool : IDisposable class MergeTool : ConjoinTool, IDisposable, IConjoinTool
{ {
private Application ExcelApp;
private Dictionary<string, double> SkuAmount { get; set; } private Dictionary<string, double> SkuAmount { get; set; }
public MergeTool() public MergeTool()
{ {
this.ExcelApp = (Application)ExcelDnaUtil.Application; ExcelApp = (Application)ExcelDnaUtil.Application;
this.SkuAmount = new Dictionary<string, double>(); SkuAmount = new Dictionary<string, double>();
} }
public void AddSkuAmountToDict(string[] files) public void CollectSkuAmount(string[] files)
{ {
ExcelApp.ScreenUpdating = false; ExcelApp.ScreenUpdating = false;
foreach (string file in files) foreach (string file in files)
@ -26,13 +25,13 @@ namespace RehauSku.PriceListTools
try try
{ {
PriceList priceList = new PriceList(wb); PriceList priceList = new PriceList(wb);
SkuAmount.AddValues(priceList); SkuAmount.AddValuesFromPriceList(priceList);
} }
catch (Exception ex) catch (Exception ex)
{ {
System.Windows.Forms.MessageBox.Show 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.MessageBoxButtons.OK,
System.Windows.Forms.MessageBoxIcon.Error); System.Windows.Forms.MessageBoxIcon.Error);
@ -46,7 +45,7 @@ namespace RehauSku.PriceListTools
ExcelApp.ScreenUpdating = true; ExcelApp.ScreenUpdating = true;
} }
public void ExportToNewFile(string exportFile) public void ExportToFile(string exportFile)
{ {
if (SkuAmount.Count < 1) if (SkuAmount.Count < 1)
{ {
@ -59,7 +58,7 @@ namespace RehauSku.PriceListTools
try try
{ {
priceList = new PriceList(wb); priceList = new PriceList(wb);
priceList.Fill(SkuAmount); priceList.FillWithValues(SkuAmount);
} }
catch (Exception ex) catch (Exception ex)

View File

@ -1,5 +1,6 @@
using Microsoft.Office.Interop.Excel; using Microsoft.Office.Interop.Excel;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq;
namespace RehauSku.PriceListTools namespace RehauSku.PriceListTools
{ {
@ -9,31 +10,25 @@ namespace RehauSku.PriceListTools
public readonly PriceListSheet OfferSheet; public readonly PriceListSheet OfferSheet;
public readonly PriceListSheet ActiveSheet; public readonly PriceListSheet ActiveSheet;
private const string _amountHeader = "Кол-во"; private const string amountHeader = "Кол-во";
private const string _skuHeader = "Актуальный материал"; private const string skuHeader = "Актуальный материал";
private const string _offerSheetHeader = "КП"; private const string offerSheetHeader = "КП";
public PriceList(Workbook workbook) public PriceList(Workbook workbook)
{ {
Workbook = workbook; Workbook = workbook;
OfferSheet = new PriceListSheet(workbook.Sheets[_offerSheetHeader]); OfferSheet = new PriceListSheet(workbook.Sheets[offerSheetHeader]);
Worksheet active = workbook.ActiveSheet; Worksheet active = workbook.ActiveSheet;
if (active.Name == _offerSheetHeader) if (active.Name == offerSheetHeader)
ActiveSheet = OfferSheet; ActiveSheet = OfferSheet;
else else
ActiveSheet = new PriceListSheet(active); ActiveSheet = new PriceListSheet(active);
} }
public bool IsValid() public void FillWithValues(Dictionary<string, double> values)
{
return OfferSheet.IsValid() &&
ActiveSheet.IsValid();
}
public void Fill(Dictionary<string, double> values)
{ {
Worksheet ws = OfferSheet.sheet; Worksheet ws = OfferSheet.sheet;
ws.Activate(); ws.Activate();
@ -45,6 +40,7 @@ namespace RehauSku.PriceListTools
foreach (KeyValuePair<string, double> kvp in values) foreach (KeyValuePair<string, double> kvp in values)
{ {
Range cell = ws.Columns[skuColumn].Find(kvp.Key); Range cell = ws.Columns[skuColumn].Find(kvp.Key);
if (cell == null) if (cell == null)
{ {
System.Windows.Forms.MessageBox.Show System.Windows.Forms.MessageBox.Show
@ -53,6 +49,7 @@ namespace RehauSku.PriceListTools
System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxButtons.OK,
System.Windows.Forms.MessageBoxIcon.Information); System.Windows.Forms.MessageBoxIcon.Information);
} }
else else
{ {
ws.Cells[cell.Row, amountColumn].Value = kvp.Value; ws.Cells[cell.Row, amountColumn].Value = kvp.Value;
@ -68,33 +65,82 @@ namespace RehauSku.PriceListTools
ws.Application.StatusBar = $"Экспортировано {exportedValues} строк из {values.Count}"; 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 class PriceListSheet
{ {
public readonly Worksheet sheet; public readonly Worksheet sheet;
public readonly int? headerRow; public readonly int? headerRow;
public readonly int? amountColumn;
public readonly int? skuColumn; public readonly int? skuColumn;
public object[,] amountCells; public readonly int? amountColumn;
public object[,] skuCells; public object[,] skuCells;
public object[,] amountCells;
public PriceListSheet(Worksheet sheet) public PriceListSheet(Worksheet sheet)
{ {
this.sheet = sheet; this.sheet = sheet;
headerRow = sheet.Cells.Find(_amountHeader).Row; headerRow = sheet.Cells.Find(amountHeader).Row;
amountColumn = sheet.Cells.Find(_amountHeader).Column; amountColumn = sheet.Cells.Find(amountHeader).Column;
skuColumn = sheet.Cells.Find(_skuHeader).Column; skuColumn = sheet.Cells.Find(skuHeader).Column;
amountCells = sheet.Columns[amountColumn].Value2; amountCells = sheet.Columns[amountColumn].Value2;
skuCells = sheet.Columns[skuColumn].Value2; skuCells = sheet.Columns[skuColumn].Value2;
} }
public bool IsValid()
{
return sheet != null &&
headerRow != null &&
amountColumn != null &&
skuColumn != null;
}
} }
} }
} }

View File

@ -14,7 +14,7 @@ namespace RehauSku.PriceListTools
return path; 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[,] amountCells = priceList.ActiveSheet.amountCells;
object[,] skuCells = priceList.ActiveSheet.skuCells; object[,] skuCells = priceList.ActiveSheet.skuCells;

View File

@ -106,6 +106,9 @@
<Compile Include="Assistant\ParseUtil.cs" /> <Compile Include="Assistant\ParseUtil.cs" />
<Compile Include="Assistant\RequestModifier.cs" /> <Compile Include="Assistant\RequestModifier.cs" />
<Compile Include="Assistant\SkuExtensions.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\MergeTool.cs" />
<Compile Include="PriceListTools\PriceList.cs" /> <Compile Include="PriceListTools\PriceList.cs" />
<Compile Include="PriceListTools\PriceListUtil.cs" /> <Compile Include="PriceListTools\PriceListUtil.cs" />

View File

@ -18,7 +18,10 @@ namespace RehauSku.Ribbon
<tab id='rau' label='REHAU'> <tab id='rau' label='REHAU'>
<group id='priceList' label='Прайс-лист'> <group id='priceList' label='Прайс-лист'>
<button id='exportToPrice' label='Экспорт в новый файл' size='normal' imageMso='PivotExportToExcel' onAction='OnExportPressed'/> <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>
<group id='rausettings' label='Настройки'> <group id='rausettings' label='Настройки'>
<button id='setPriceList' label='Файл прайс-листа' size='normal' imageMso='CurrentViewSettings' onAction='OnSetPricePressed'/> <button id='setPriceList' label='Файл прайс-листа' size='normal' imageMso='CurrentViewSettings' onAction='OnSetPricePressed'/>
@ -29,14 +32,27 @@ namespace RehauSku.Ribbon
</customUI>"; </customUI>";
} }
// <dropDown id = 'dd1' label = 'Drop dynamic' getItemCount = 'fncGetItemCountDrop' getItemLabel = 'fncGetItemLabelDrop' onAction = 'fncOnActionDrop'/>
public void OnMergePressed(IRibbonControl control) public void OnMergePressed(IRibbonControl control)
{ {
using (MergeTool mergeTool = new MergeTool()) using (MergeTool mergeTool = new MergeTool())
{ {
string[] files = Dialog.GetMultiplyFiles(); string[] files = Dialog.GetMultiplyFiles();
mergeTool.AddSkuAmountToDict(files); mergeTool.CollectSkuAmount(files);
string exportFile = PriceListUtil.CreateNewExportFile(); 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);
} }
} }