Compare commits

..

No commits in common. "3ecfc82b7e820abbcfb18ad91ea9d4175b3905cc" and "d058b3ed4e9b954e9b264d79e6257a44b31b2d07" have entirely different histories.

View File

@ -15,16 +15,20 @@ public class GuessReader : IReader
public Dictionary<Product, double> ReadProducts(Range range)
{
_progressBar = new("Ищу колонку с артикулами...", range.Columns.Count);
_progressBar = new("Поиск возможных пар артикул-количество...", range.Columns.Count);
int? productColumnIndex = null;
List<int> amountColumnIndeces = new();
for (int column = 1; column < range.Columns.Count + 1; column++)
{
_progressBar.Update();
if (IsProductColumn(range.Columns[column]))
if (productColumnIndex == null && IsProductColumn(range.Columns[column]))
{
productColumnIndex = column;
break;
}
else if (IsAmountColumn(range.Columns[column]))
{
amountColumnIndeces.Add(column);
}
}
@ -33,43 +37,35 @@ public class GuessReader : IReader
throw new ArgumentException("Колонка с артикулом не определена");
}
int? amountColumnIndex = null;
int currentIndex = productColumnIndex.Value + 1;
_progressBar = new("Ищу колонку с количеством...", range.Columns.Count);
while (currentIndex > 0)
{
_progressBar.Update();
if (currentIndex > range.Columns.Count + 1)
{
currentIndex = productColumnIndex.Value - 1;
continue;
}
if (currentIndex > productColumnIndex)
{
if (IsAmountColumn(range.Columns[currentIndex++]))
{
amountColumnIndex = currentIndex - 1;
break;
}
}
else
{
if (IsAmountColumn(range.Columns[currentIndex--]))
{
amountColumnIndex = currentIndex + 1;
break;
}
}
}
if (amountColumnIndex == null)
if (amountColumnIndeces.Count == 0)
{
throw new ArgumentException("Колонка с количеством не определена");
}
else if (amountColumnIndeces.Count == 1)
{
return GetDictionaryFromColumns(range.Columns[productColumnIndex],
range.Columns[amountColumnIndeces.First()]);
}
else
{
int amountColumnIndex = amountColumnIndeces
.Where(i => i > productColumnIndex)
.FirstOrDefault();
if (amountColumnIndex == 0)
{
amountColumnIndex = amountColumnIndeces
.Where(i => i < productColumnIndex)
.LastOrDefault();
}
if (amountColumnIndex == 0)
{
throw new ArgumentException("Колонка с количеством не определена");
}
return GetDictionaryFromColumns(range.Columns[productColumnIndex],
range.Columns[amountColumnIndex]);
}
@ -149,22 +145,23 @@ public class GuessReader : IReader
private Dictionary<Product, double> GetDictionaryFromColumns(Range productColumn, Range amountColumn)
{
Dictionary<Product, double> result = new();
_progressBar = new("Заполняю словарь значений...", productColumn.Rows.Count);
var lastRowIndex = amountColumn.Worksheet
.Cells[amountColumn.Rows.Count, amountColumn.Column]
.End[XlDirection.xlUp].Row;
_progressBar = new("Заполняю словарь значений...", lastRowIndex);
for (int row = 1; row < lastRowIndex + 1; row++)
for (int row = 1; row < productColumn.Rows.Count + 1; row++)
{
_progressBar.Update();
var amountCells = amountColumn.Value2;
object currentAmountCell = productColumn.Rows.Count == 1 ? amountCells : amountCells[row, 1];
double? amountValue = currentAmountCell as double?;
if (amountValue == null)
{
continue;
}
var productCells = productColumn.Value2;
object currentProductCell = productColumn.Rows.Count == 1 ? productCells : productCells[row, 1];
double? amountValue = currentAmountCell as double?;
if (amountValue == null || amountValue == 0 || currentProductCell == null)
if (currentProductCell == null)
{
continue;
}