MergeTool, MemoryUtil anf stuff

This commit is contained in:
Sergey Chebotar 2021-12-24 16:22:03 +03:00
parent 2a4076d6ee
commit 24024b5729
15 changed files with 384 additions and 177 deletions

View File

@ -100,25 +100,28 @@
<Reference Include="WindowsBase" />
</ItemGroup>
<ItemGroup>
<Compile Include="Source\AddIn\FileDialog.cs" />
<Compile Include="Source\Forms\Dialog.cs" />
<Compile Include="Source\AddIn\RegistryUtil.cs" />
<Compile Include="Source\Assistant\MemoryCacheExtensions.cs" />
<Compile Include="Source\AddIn\MemoryCacheUtil.cs" />
<Compile Include="Source\Assistant\ParseUtil.cs" />
<Compile Include="Source\Assistant\RequestModifier.cs" />
<Compile Include="Source\Assistant\SkuExtensions.cs" />
<Compile Include="Source\PriceListTools\MergeTool.cs" />
<Compile Include="Source\PriceListTools\PriceList.cs" />
<Compile Include="Source\PriceListTools\PriceListUtil.cs" />
<Compile Include="Source\Ribbon\RibbonController.cs" />
<Compile Include="Source\Assistant\HttpClientUtil.cs" />
<Compile Include="Source\Assistant\StoreResponse.cs" />
<Compile Include="Source\DataExport\ExportTool.cs" />
<Compile Include="Source\PriceListTools\ExportTool.cs" />
<Compile Include="Source\AddIn\AddIn.cs" />
<Compile Include="Source\Assistant\IProduct.cs" />
<Compile Include="Source\AddIn\Functions.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Source\Assistant\SkuAssist.cs" />
<Compile Include="Source\Settings\SettingsForm.cs">
<Compile Include="Source\Forms\SettingsForm.cs">
<SubType>Form</SubType>
</Compile>
<Compile Include="Source\Settings\SettingsForm.Designer.cs">
<Compile Include="Source\Forms\SettingsForm.Designer.cs">
<DependentUpon>SettingsForm.cs</DependentUpon>
</Compile>
</ItemGroup>
@ -128,6 +131,9 @@
<None Include="packages.config" />
<None Include="Properties\ExcelDna.Build.props" />
</ItemGroup>
<ItemGroup>
<Folder Include="Source\Settings\" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<Import Project="packages\ExcelDna.AddIn.1.5.0\build\ExcelDna.AddIn.targets" Condition="Exists('packages\ExcelDna.AddIn.1.5.0\build\ExcelDna.AddIn.targets')" />
<Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild">

View File

@ -2,6 +2,8 @@
using ExcelDna.IntelliSense;
using ExcelDna.Registration;
using System.Net.Http;
using System.Runtime.Caching;
namespace RehauSku
{
@ -16,10 +18,13 @@ namespace RehauSku
public class AddIn : IExcelAddIn
{
public static HttpClient httpClient = new HttpClient();
public static HttpClient httpClient;
public static MemoryCache memoryCache;
public void AutoOpen()
{
httpClient = new HttpClient();
memoryCache = new MemoryCache("RehauSku");
RegisterFunctions();
IntelliSenseServer.Install();
RegistryUtil.Initialize();
@ -29,6 +34,7 @@ namespace RehauSku
{
IntelliSenseServer.Uninstall();
RegistryUtil.Uninitialize();
memoryCache.Dispose();
}
void RegisterFunctions()

View File

@ -1,24 +0,0 @@
using System.Windows.Forms;
namespace RehauSku
{
static class FileDialog
{
public static string GetFilePath()
{
string filePath = string.Empty;
using (OpenFileDialog dialog = new OpenFileDialog())
{
dialog.Filter = "Все файлы (*.*)|*.*";
if (dialog.ShowDialog() == DialogResult.OK)
{
filePath = dialog.FileName;
}
}
return filePath;
}
}
}

View File

@ -1,19 +1,20 @@
using System;
using System.Runtime.Caching;
using System.Threading.Tasks;
using RehauSku.Assistant;
namespace RehauSku.Assistant
namespace RehauSku
{
static class MemoryCacheExtensions
static class MemoryCacheUtil
{
public static bool IsCached(this string request)
{
return MemoryCache.Default.Contains(request);
return AddIn.memoryCache.Contains(request);
}
public static IProduct GetFromCache(this string request)
{
return MemoryCache.Default[request] as IProduct;
return AddIn.memoryCache[request] as IProduct;
}
public static async Task<IProduct> RequestAndCache(this string request)
@ -23,8 +24,14 @@ namespace RehauSku.Assistant
if (product == null)
return null;
MemoryCache.Default.Add(request, product, DateTime.Now.AddMinutes(10));
AddIn.memoryCache.Add(request, product, DateTime.Now.AddMinutes(10));
return product;
}
public static void ClearCache()
{
AddIn.memoryCache.Dispose();
AddIn.memoryCache = new MemoryCache("RehauSku");
}
}
}

View File

@ -1,5 +1,7 @@
using Microsoft.Win32;
using System.IO;
using RehauSku.Forms;
using System.Windows.Forms;
namespace RehauSku
{
@ -19,6 +21,7 @@ namespace RehauSku
public static void Uninitialize()
{
_RootKey.Close();
}
public static bool IsPriceListPathEmpty()
@ -32,7 +35,8 @@ namespace RehauSku
{
if (IsPriceListPathEmpty() || !File.Exists(_priceListPath))
{
string fileName = FileDialog.GetFilePath();
MessageBox.Show("Прайс-лист отсутствует или неверный файл прайс-листа", "Укажите файл прайс-листа", MessageBoxButtons.OK, MessageBoxIcon.Warning);
string fileName = Dialog.GetFilePath();
_priceListPath = fileName;
_RootKey.SetValue("PriceListPath", fileName);
return _priceListPath;

View File

@ -1,129 +0,0 @@
using ExcelDna.Integration;
using Microsoft.Office.Interop.Excel;
using System;
using System.Collections.Generic;
using System.IO;
using RehauSku.Assistant;
namespace RehauSku.DataExport
{
public class ExportTool : IDisposable
{
private Application xlApp;
private Dictionary<string, double> SkuAmount { get; set; }
private Range Selection { get; set; }
private string CurrentFilePath { get; set; }
public ExportTool()
{
this.xlApp = (Application)ExcelDnaUtil.Application;
this.CurrentFilePath = xlApp.ActiveWorkbook.FullName;
_GetSelectedCells();
}
private void _GetSelectedCells()
{
Selection = xlApp.Selection;
}
public bool IsRangeValid()
{
return Selection.Columns.Count == 2;
}
private void FillSkuAmountDict()
{
object[,] cells = Selection.Value2;
SkuAmount = new Dictionary<string, double>();
int rowsCount = Selection.Rows.Count;
for (int row = 1; row <= rowsCount; row++)
{
if (cells[row, 1] == null || cells[row, 2] == null)
continue;
string sku = null;
double? amount = null;
for (int column = 1; column <= 2; column++)
{
object current = cells[row, column];
if (current.GetType() == typeof(string)
&& ((string)current).IsRehauSku())
sku = (string)current;
else if (current.GetType() == typeof(string)
&& double.TryParse((string)current, out _))
amount = double.Parse((string)current);
else if (current.GetType() == typeof(double))
amount = (double)current;
}
if (sku == null || amount == null)
continue;
if (SkuAmount.ContainsKey(sku))
SkuAmount[sku] += amount.Value;
else
SkuAmount.Add(sku, amount.Value);
}
}
public void FillNewPriceList()
{
const string amountHeader = "Кол-во";
const string skuHeader = "Актуальный материал";
FillSkuAmountDict();
string exportFile = _GetExportFullPath();
File.Copy(RegistryUtil.PriceListPath, exportFile, true);
Workbook wb = xlApp.Workbooks.Open(exportFile);
Worksheet ws = wb.Sheets["КП"];
ws.Activate();
int amountColumn = ws.Cells.Find(amountHeader).Column;
int skuColumn = ws.Cells.Find(skuHeader).Column;
foreach (KeyValuePair<string, double> kvp in SkuAmount)
{
Range cell = ws.Columns[skuColumn].Find(kvp.Key);
ws.Cells[cell.Row, amountColumn].Value = kvp.Value;
}
AutoFilter filter = ws.AutoFilter;
int firstFilterColumn = filter.Range.Column;
filter.Range.AutoFilter(amountColumn - firstFilterColumn + 1, "<>");
ws.Range["A1"].Activate();
}
private string _GetExportFullPath()
{
string fileExtension = Path.GetExtension(RegistryUtil.PriceListPath);
return Path.GetTempFileName() + fileExtension;
}
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
protected virtual void Dispose(bool disposing)
{
}
}
class SelectionCheck
{
}
}

46
Source/Forms/Dialog.cs Normal file
View File

@ -0,0 +1,46 @@
using System.Collections.Generic;
using System.Windows.Forms;
namespace RehauSku.Forms
{
static class Dialog
{
public static string GetFilePath()
{
string filePath = string.Empty;
using (OpenFileDialog dialog = new OpenFileDialog())
{
dialog.Filter = "Все файлы (*.*)|*.*";
if (dialog.ShowDialog() == DialogResult.OK)
{
filePath = dialog.FileName;
}
}
return filePath;
}
public static string[] GetMultiplyFiles()
{
List<string> fileNames = new List<string>();
using (OpenFileDialog dialog = new OpenFileDialog())
{
dialog.Filter = "Все файлы (*.*)|*.*";
dialog.Multiselect = true;
if (dialog.ShowDialog() == DialogResult.OK)
{
foreach (string file in dialog.FileNames)
{
fileNames.Add(file);
}
}
}
return fileNames.ToArray();
}
}
}

View File

@ -1,4 +1,4 @@
namespace RehauSku.Settings
namespace RehauSku.Forms
{
partial class SettingsForm
{

View File

@ -8,7 +8,7 @@ using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace RehauSku.Settings
namespace RehauSku.Forms
{
public partial class SettingsForm : Form
{
@ -17,9 +17,5 @@ namespace RehauSku.Settings
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
}
}
}

View File

@ -0,0 +1,97 @@
using ExcelDna.Integration;
using Microsoft.Office.Interop.Excel;
using RehauSku.Assistant;
using System;
using System.Collections.Generic;
namespace RehauSku.PriceListTools
{
class ExportTool : IDisposable
{
private Application ExcelApp;
private Dictionary<string, double> SkuAmount { get; set; }
private Range Selection { get; set; }
public ExportTool()
{
this.ExcelApp = (Application)ExcelDnaUtil.Application;
Selection = ExcelApp.Selection;
if (IsRangeValid())
_FillSkuAmountDict();
}
public bool IsRangeValid()
{
return Selection != null &&
Selection.Columns.Count == 2;
}
private void _FillSkuAmountDict()
{
object[,] cells = Selection.Value2;
SkuAmount = new Dictionary<string, double>();
int rowsCount = Selection.Rows.Count;
for (int row = 1; row <= rowsCount; row++)
{
if (cells[row, 1] == null || cells[row, 2] == null)
continue;
string sku = null;
double? amount = null;
for (int column = 1; column <= 2; column++)
{
object current = cells[row, column];
if (current.ToString().IsRehauSku())
{
sku = current.ToString();
}
else if (current.GetType() == typeof(string)
&& double.TryParse(current.ToString(), out _))
{
amount = double.Parse((string)current);
}
else if (current.GetType() == typeof(double))
{
amount = (double)current;
}
}
if (sku == null || amount == null)
continue;
if (SkuAmount.ContainsKey(sku))
SkuAmount[sku] += amount.Value;
else
SkuAmount.Add(sku, amount.Value);
}
}
public void ExportToNewFile()
{
string exportFile = PriceListUtil.CreateNewExportFile();
Workbook wb = ExcelApp.Workbooks.Open(exportFile);
PriceList priceList = new PriceList(wb);
if (priceList.IsValid())
priceList.Fill(SkuAmount);
}
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
protected virtual void Dispose(bool disposing)
{
}
}
}

View File

@ -0,0 +1,55 @@
using ExcelDna.Integration;
using Microsoft.Office.Interop.Excel;
using System;
using System.Collections.Generic;
namespace RehauSku.PriceListTools
{
class MergeTool : IDisposable
{
private Application ExcelApp;
private Dictionary<string, double> SkuAmount { get; set; }
public MergeTool()
{
this.ExcelApp = (Application)ExcelDnaUtil.Application;
this.SkuAmount = new Dictionary<string, double>();
}
public void AddSkuAmountToDict(string[] files)
{
ExcelApp.ScreenUpdating = false;
foreach (string file in files)
{
Workbook wb = ExcelApp.Workbooks.Open(file);
PriceList priceList = new PriceList(wb);
if (priceList.IsValid())
SkuAmount.AddValues(priceList);
wb.Close();
}
ExcelApp.ScreenUpdating = true;
}
public void ExportToNewFile(string exportFile)
{
Workbook wb = ExcelApp.Workbooks.Open(exportFile);
PriceList priceList = new PriceList(wb);
if (priceList.IsValid())
priceList.Fill(SkuAmount);
}
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
protected virtual void Dispose(bool disposing)
{
}
}
}

View File

@ -0,0 +1,87 @@
using Microsoft.Office.Interop.Excel;
using System.Collections.Generic;
namespace RehauSku.PriceListTools
{
class PriceList
{
public readonly Workbook Workbook;
public readonly PriceListSheet OfferSheet;
public readonly PriceListSheet ActiveSheet;
private const string _amountHeader = "Кол-во";
private const string _skuHeader = "Актуальный материал";
private const string _offerSheetHeader = "КП";
public PriceList(Workbook workbook)
{
Workbook = workbook;
OfferSheet = new PriceListSheet(workbook.Sheets[_offerSheetHeader]);
Worksheet active = workbook.ActiveSheet;
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)
{
Worksheet ws = OfferSheet.sheet;
ws.Activate();
int amountColumn = OfferSheet.amountColumn.Value;
int skuColumn = OfferSheet.skuColumn.Value;
foreach (KeyValuePair<string, double> kvp in values)
{
Range cell = ws.Columns[skuColumn].Find(kvp.Key);
ws.Cells[cell.Row, amountColumn].Value = kvp.Value;
}
AutoFilter filter = ws.AutoFilter;
int firstFilterColumn = filter.Range.Column;
filter.Range.AutoFilter(amountColumn - firstFilterColumn + 1, "<>");
ws.Range["A1"].Activate();
}
public class PriceListSheet
{
public readonly Worksheet sheet;
public readonly int? headerRow;
public readonly int? amountColumn;
public readonly int? skuColumn;
public object[,] amountCells;
public object[,] skuCells;
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;
amountCells = sheet.Columns[amountColumn].Value2;
skuCells = sheet.Columns[skuColumn].Value2;
}
public bool IsValid()
{
return sheet != null &&
headerRow != null &&
amountColumn != null &&
skuColumn != null;
}
}
}
}

View File

@ -0,0 +1,41 @@
using System.Collections.Generic;
using System.IO;
namespace RehauSku.PriceListTools
{
static class PriceListUtil
{
public static string CreateNewExportFile()
{
string fileExtension = Path.GetExtension(RegistryUtil.PriceListPath);
string path = Path.GetTempFileName() + fileExtension;
File.Copy(RegistryUtil.PriceListPath, path);
return path;
}
public static void AddValues(this Dictionary<string, double> SkuAmount, PriceList priceList)
{
object[,] amountCells = priceList.ActiveSheet.amountCells;
object[,] skuCells = priceList.ActiveSheet.skuCells;
for (int row = priceList.ActiveSheet.headerRow.Value + 1; row < amountCells.GetLength(0); row++)
{
object amount = amountCells[row, 1];
object sku = skuCells[row, 1];
if (amount != null && (double)amount != 0)
{
if (SkuAmount.ContainsKey(sku.ToString()))
{
SkuAmount[sku.ToString()] += (double)amount;
}
else
SkuAmount.Add(sku.ToString(), (double)amount);
}
}
}
}
}

View File

@ -1,7 +1,8 @@
using System.Runtime.InteropServices;
using System.Windows.Forms;
using ExcelDna.Integration.CustomUI;
using RehauSku.DataExport;
using RehauSku.PriceListTools;
using RehauSku.Forms;
namespace RehauSku.Ribbon
{
@ -17,6 +18,7 @@ namespace RehauSku.Ribbon
<tab id='rau' label='REHAU'>
<group id='priceList' label='Прайс-лист'>
<button id='exportToPrice' label='Экспорт' size='large' imageMso='PivotExportToExcel' onAction='OnExportPressed'/>
<button id='mergeFiles' label='Объединить' size='large' imageMso='Copy' onAction='OnMergePressed'/>
</group>
<group id='rausettings' label='Настройки'>
<button id='set' label='Настройки' size='large' imageMso='CurrentViewSettings' onAction='OnSettingsPressed'/>
@ -27,11 +29,22 @@ namespace RehauSku.Ribbon
</customUI>";
}
public void OnMergePressed(IRibbonControl control)
{
using (MergeTool mergeTool = new MergeTool())
{
string[] files = Dialog.GetMultiplyFiles();
mergeTool.AddSkuAmountToDict(files);
string exportFile = PriceListUtil.CreateNewExportFile();
mergeTool.ExportToNewFile(exportFile);
}
}
public void OnExportPressed(IRibbonControl control)
{
using (ExportTool dw = new ExportTool())
using (ExportTool exportTool = new ExportTool())
{
if (!dw.IsRangeValid())
if (!exportTool.IsRangeValid())
{
MessageBox.Show("Выделен неверный диапазон!",
"Неверный диапазон",
@ -42,15 +55,17 @@ namespace RehauSku.Ribbon
else
{
dw.FillNewPriceList();
exportTool.ExportToNewFile();
}
}
}
public void OnSettingsPressed(IRibbonControl control)
{
Form settingsForm = new Settings.SettingsForm();
Form settingsForm = new SettingsForm();
settingsForm.Show();
}
}
}