RhSolutions-AddIn/src/PriceListTools/Source.cs

68 lines
2.2 KiB
C#
Raw Normal View History

2022-01-27 09:59:33 +03:00
using Microsoft.Office.Interop.Excel;
using System;
2022-01-27 10:22:30 +03:00
using System.Collections.Generic;
2022-01-27 09:59:33 +03:00
namespace RehauSku.PriceListTools
{
2022-01-27 10:22:30 +03:00
internal class Source : PriceList
2022-01-27 09:59:33 +03:00
{
2022-01-28 11:41:35 +03:00
public Dictionary<Position, double> PositionAmount { get; private set; }
2022-01-27 09:59:33 +03:00
2022-01-27 10:22:30 +03:00
public Source(Workbook workbook)
2022-01-27 09:59:33 +03:00
{
2022-01-28 12:44:37 +03:00
if (workbook == null)
{
throw new ArgumentException($"Нет рабочего файла");
}
2022-01-27 09:59:33 +03:00
Sheet = workbook.ActiveSheet;
2022-01-27 10:22:30 +03:00
Name = workbook.Name;
2022-01-27 09:59:33 +03:00
amountCell = Sheet.Cells.Find(amountHeader);
skuCell = Sheet.Cells.Find(skuHeader);
groupCell = Sheet.Cells.Find(groupHeader);
2022-01-28 11:41:35 +03:00
nameCell = Sheet.Cells.Find(nameHeader);
2022-01-27 09:59:33 +03:00
2022-01-28 11:41:35 +03:00
if (amountCell == null || skuCell == null || groupCell == null || nameCell == null)
2022-01-27 09:59:33 +03:00
{
2022-01-27 17:34:03 +03:00
throw new ArgumentException($"Файл {Name} не распознан");
2022-01-27 09:59:33 +03:00
}
2022-01-28 11:41:35 +03:00
CreatePositionsDict();
2022-01-27 09:59:33 +03:00
}
2022-01-28 11:41:35 +03:00
private void CreatePositionsDict()
2022-01-27 09:59:33 +03:00
{
2022-01-28 11:41:35 +03:00
PositionAmount = new Dictionary<Position, double>();
2022-01-27 09:59:33 +03:00
2022-01-28 11:41:35 +03:00
object[,] amountColumn = amountCell.EntireColumn.Value2;
object[,] skuColumn = skuCell.EntireColumn.Value2;
object[,] nameColumn = nameCell.EntireColumn.Value2;
object[,] groupColumn = groupCell.EntireColumn.Value2;
2022-01-27 09:59:33 +03:00
for (int row = amountCell.Row + 1; row < amountColumn.GetLength(0); row++)
{
object amount = amountColumn[row, 1];
2022-01-28 11:41:35 +03:00
object group = groupColumn[row, 1];
object name = nameColumn[row, 1];
2022-01-27 09:59:33 +03:00
object sku = skuColumn[row, 1];
if (amount != null && (double)amount != 0)
{
2022-01-28 11:41:35 +03:00
Position p = new Position(group.ToString(), sku.ToString(), name.ToString());
2022-01-27 09:59:33 +03:00
2022-01-28 11:41:35 +03:00
if (PositionAmount.ContainsKey(p))
{
PositionAmount[p] += (double)amount;
}
2022-01-27 09:59:33 +03:00
else
2022-01-28 11:41:35 +03:00
{
PositionAmount.Add(p, (double)amount);
}
2022-01-27 09:59:33 +03:00
}
}
}
}
}