Compare commits

..

2 Commits

Author SHA1 Message Date
Sergey Chebotar
3ecfc82b7e Lookup for amount untill last used row only 2023-05-26 07:02:49 +03:00
Sergey Chebotar
aa4c949270 Change find amount column algorithm 2023-05-26 06:33:35 +03:00

View File

@ -15,20 +15,16 @@ 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 (productColumnIndex == null && IsProductColumn(range.Columns[column]))
if (IsProductColumn(range.Columns[column]))
{
productColumnIndex = column;
}
else if (IsAmountColumn(range.Columns[column]))
{
amountColumnIndeces.Add(column);
break;
}
}
@ -37,35 +33,43 @@ public class GuessReader : IReader
throw new ArgumentException("Колонка с артикулом не определена");
}
if (amountColumnIndeces.Count == 0)
int? amountColumnIndex = null;
int currentIndex = productColumnIndex.Value + 1;
_progressBar = new("Ищу колонку с количеством...", range.Columns.Count);
while (currentIndex > 0)
{
throw new ArgumentException("Колонка с количеством не определена");
_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;
}
}
}
else if (amountColumnIndeces.Count == 1)
if (amountColumnIndex == null)
{
return GetDictionaryFromColumns(range.Columns[productColumnIndex],
range.Columns[amountColumnIndeces.First()]);
throw new ArgumentException("Колонка с количеством не определена");
}
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]);
}
@ -145,23 +149,22 @@ public class GuessReader : IReader
private Dictionary<Product, double> GetDictionaryFromColumns(Range productColumn, Range amountColumn)
{
Dictionary<Product, double> result = new();
_progressBar = new("Заполняю словарь значений...", productColumn.Rows.Count);
for (int row = 1; row < productColumn.Rows.Count + 1; row++)
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++)
{
_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];
if (currentProductCell == null)
double? amountValue = currentAmountCell as double?;
if (amountValue == null || amountValue == 0 || currentProductCell == null)
{
continue;
}