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; namespace RhSolutions.Services;
@ -15,20 +14,14 @@ public class GuessReader : IReader
public Dictionary<Product, double> ReadProducts(Range range) public Dictionary<Product, double> ReadProducts(Range range)
{ {
_progressBar = new("Поиск возможных пар артикул-количество...", range.Columns.Count);
int? productColumnIndex = null; int? productColumnIndex = null;
List<int> amountColumnIndeces = new();
for (int column = 1; column < range.Columns.Count + 1; column++) 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; productColumnIndex = column;
} break;
else if (IsAmountColumn(range.Columns[column]))
{
amountColumnIndeces.Add(column);
} }
} }
@ -37,35 +30,41 @@ public class GuessReader : IReader
throw new ArgumentException("Колонка с артикулом не определена"); throw new ArgumentException("Колонка с артикулом не определена");
} }
if (amountColumnIndeces.Count == 0) int? amountColumnIndex = null;
int currentIndex = productColumnIndex.Value + 1;
while (currentIndex > 0)
{ {
throw new ArgumentException("Колонка с количеством не определена"); 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], throw new ArgumentException("Колонка с количеством не определена");
range.Columns[amountColumnIndeces.First()]);
} }
else 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], return GetDictionaryFromColumns(range.Columns[productColumnIndex],
range.Columns[amountColumnIndex]); range.Columns[amountColumnIndex]);
} }