aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--RhSolutions.AddIn/Services/GuessReader.cs61
1 files changed, 30 insertions, 31 deletions
diff --git a/RhSolutions.AddIn/Services/GuessReader.cs b/RhSolutions.AddIn/Services/GuessReader.cs
index 93609d0..7d8b60d 100644
--- a/RhSolutions.AddIn/Services/GuessReader.cs
+++ b/RhSolutions.AddIn/Services/GuessReader.cs
@@ -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)
{
- 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],
- 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]);
}