RhSolutions-AddIn/src/PriceListTools/AbstractTool.cs

175 lines
5.9 KiB
C#
Raw Normal View History

2022-01-27 10:22:30 +03:00
using ExcelDna.Integration;
using Microsoft.Office.Interop.Excel;
using System;
using System.Collections.Generic;
2022-01-28 18:15:13 +03:00
using System.Windows.Forms;
using Application = Microsoft.Office.Interop.Excel.Application;
2022-01-27 10:22:30 +03:00
namespace RehauSku.PriceListTools
{
2022-02-02 18:02:17 +03:00
internal abstract class AbstractTool
2022-01-27 10:22:30 +03:00
{
2022-01-27 17:34:03 +03:00
protected private Application ExcelApp = (Application)ExcelDnaUtil.Application;
2022-02-02 18:05:49 +03:00
protected private TargetPriceList TargetFile;
2022-01-27 10:22:30 +03:00
2022-01-27 17:34:03 +03:00
public void OpenNewPrice()
2022-01-27 10:22:30 +03:00
{
2022-01-27 17:34:03 +03:00
Workbook wb = ExcelApp.Workbooks.Open(RegistryUtil.PriceListPath);
2022-01-27 10:22:30 +03:00
try
{
2022-02-02 18:05:49 +03:00
TargetFile = new TargetPriceList(wb);
2022-01-27 10:22:30 +03:00
}
catch (Exception ex)
{
2022-01-28 18:15:13 +03:00
MessageBox.Show
2022-01-27 10:22:30 +03:00
(ex.Message,
"Ошибка открытия шаблонного прайс-листа",
2022-01-28 18:15:13 +03:00
MessageBoxButtons.OK,
MessageBoxIcon.Information);
2022-01-27 10:22:30 +03:00
wb.Close();
2022-01-27 17:34:03 +03:00
throw ex;
2022-01-27 10:22:30 +03:00
}
}
protected private void FillColumnsWithDictionary(KeyValuePair<Position, double> positionAmount, params int[] columns)
2022-01-27 10:22:30 +03:00
{
int? row = GetPositionRow(positionAmount.Key.Sku, positionAmount.Key.Group, TargetFile.skuCell.Column);
if (row != null)
2022-01-27 10:22:30 +03:00
{
foreach (int column in columns)
2022-01-28 18:15:13 +03:00
{
Range sumCell = TargetFile.Sheet.Cells[row, column];
if (sumCell.Value2 == null)
{
sumCell.Value2 = positionAmount.Value;
}
2022-01-28 18:15:13 +03:00
else
2022-01-28 18:15:13 +03:00
{
sumCell.Value2 += positionAmount.Value;
2022-01-28 18:15:13 +03:00
}
}
}
else
2022-01-28 18:15:13 +03:00
{
string sku = positionAmount.Key.Sku.Substring(1, 6);
2022-01-28 16:19:39 +03:00
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;
}
Range oldSkuCell = TargetFile.Sheet.Cells[row, TargetFile.oldSkuCell.Column];
oldSkuCell.Value2 = positionAmount.Key.Sku;
}
}
else
{
FillMissing(positionAmount, columns);
}
}
}
protected private void FillMissing(KeyValuePair<Position, double> positionAmount, params int[] columns)
2022-01-28 18:15:13 +03:00
{
Range foundCell = TargetFile.oldSkuCell.EntireColumn.Find(positionAmount.Key.Sku);
int row;
2022-01-28 18:15:13 +03:00
if (foundCell == null)
{
row = TargetFile.Sheet.Cells[TargetFile.Sheet.Rows.Count, TargetFile.skuCell.Column]
.End[XlDirection.xlUp]
.Row + 1;
2022-01-28 18:15:13 +03:00
TargetFile.Sheet.Rows[row]
.EntireRow
.Insert(XlInsertShiftDirection.xlShiftDown, XlInsertFormatOrigin.xlFormatFromLeftOrAbove);
2022-01-28 18:15:13 +03:00
Range previous = TargetFile.Sheet.Rows[row - 1];
Range current = TargetFile.Sheet.Rows[row];
previous.Copy(current);
current.ClearContents();
TargetFile.Sheet.Cells[row, TargetFile.groupCell.Column].Value2 = positionAmount.Key.Group;
TargetFile.Sheet.Cells[row, TargetFile.oldSkuCell.Column].Value2 = positionAmount.Key.Sku;
TargetFile.Sheet.Cells[row, TargetFile.nameCell.Column].Value2 = positionAmount.Key.Name;
TargetFile.Sheet.Cells[row, TargetFile.skuCell.Column].Value2 = "Не найден";
2022-01-28 18:15:13 +03:00
}
else
2022-01-28 18:15:13 +03:00
{
row = foundCell.Row;
2022-01-28 18:15:13 +03:00
}
foreach (int column in columns)
2022-01-28 18:15:13 +03:00
{
if (TargetFile.Sheet.Cells[row, column].Value2 == null)
2022-01-28 18:15:13 +03:00
{
TargetFile.Sheet.Cells[row, column].Value2 = positionAmount.Value;
2022-01-28 18:15:13 +03:00
}
else
{
TargetFile.Sheet.Cells[row, column].Value2 += positionAmount.Value;
2022-01-28 18:15:13 +03:00
}
}
}
protected private int? GetPositionRow(string sku, string group, int column)
2022-01-28 18:15:13 +03:00
{
int? row = null;
Range foundCell = TargetFile.Sheet.Columns[column].Find(sku);
string foundGroupValue;
2022-01-28 18:15:13 +03:00
if (foundCell == null) return null;
2022-01-28 18:15:13 +03:00
else
{
row = foundCell.Row;
foundGroupValue = TargetFile.Sheet.Cells[foundCell.Row, TargetFile.groupCell.Column].Value2.ToString();
}
2022-01-28 18:15:13 +03:00
if (string.IsNullOrEmpty(group) || group.Equals(foundGroupValue))
return row;
else
while (true)
{
foundCell = TargetFile.skuCell.EntireColumn.FindNext(foundCell);
if (foundCell == null) return row;
foundGroupValue = TargetFile.Sheet.Cells[foundCell.Row, TargetFile.groupCell.Column].Value2.ToString();
if (group.Equals(foundGroupValue)) return foundCell.Row;
}
2022-01-28 18:15:13 +03:00
}
2022-01-27 10:22:30 +03:00
protected private void FilterByAmount()
{
2022-01-27 17:34:03 +03:00
AutoFilter filter = TargetFile.Sheet.AutoFilter;
2022-01-28 18:46:31 +03:00
int startColumn = filter.Range.Column;
2022-01-27 10:22:30 +03:00
2022-01-28 18:46:31 +03:00
filter.Range.AutoFilter(TargetFile.amountCell.Column - startColumn + 1, "<>");
2022-01-27 17:34:03 +03:00
TargetFile.Sheet.Range["A1"].Activate();
2022-01-27 10:22:30 +03:00
}
}
}