From aa4c949270075ef70b83032165b6ab9281cbe89a Mon Sep 17 00:00:00 2001 From: Sergey Chebotar Date: Fri, 26 May 2023 06:33:35 +0300 Subject: [PATCH] Change find amount column algorithm --- RhSolutions.AddIn/Services/GuessReader.cs | 65 +++++++++++------------ 1 file changed, 32 insertions(+), 33 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 ReadProducts(Range range) { - _progressBar = new("Поиск возможных пар артикул-количество...", range.Columns.Count); int? productColumnIndex = null; - List 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]); }