Change find amount column algorithm

This commit is contained in:
Sergey Chebotar 2023-05-26 06:33:35 +03:00
parent d058b3ed4e
commit aa4c949270

View File

@ -1,5 +1,4 @@
using Microsoft.Office.Interop.Excel;
using System.IO;
using System.IO;
namespace RhSolutions.Services;
@ -15,20 +14,14 @@ public class GuessReader : IReader
public Dictionary<Product, double> ReadProducts(Range range)
{
_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 +30,41 @@ public class GuessReader : IReader
throw new ArgumentException("Колонка с артикулом не определена");
}
if (amountColumnIndeces.Count == 0)
int? amountColumnIndex = null;
int currentIndex = productColumnIndex.Value + 1;
while (currentIndex > 0)
{
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)
{
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]);
}