Lookup for amount untill last used row only

This commit is contained in:
Sergey Chebotar 2023-05-26 07:02:49 +03:00
parent aa4c949270
commit 3ecfc82b7e

View File

@ -1,4 +1,5 @@
using System.IO; using Microsoft.Office.Interop.Excel;
using System.IO;
namespace RhSolutions.Services; namespace RhSolutions.Services;
@ -14,10 +15,12 @@ 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;
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 (IsProductColumn(range.Columns[column]))
{ {
productColumnIndex = column; productColumnIndex = column;
@ -32,9 +35,11 @@ public class GuessReader : IReader
int? amountColumnIndex = null; int? amountColumnIndex = null;
int currentIndex = productColumnIndex.Value + 1; int currentIndex = productColumnIndex.Value + 1;
_progressBar = new("Ищу колонку с количеством...", range.Columns.Count);
while (currentIndex > 0) while (currentIndex > 0)
{ {
_progressBar.Update();
if (currentIndex > range.Columns.Count + 1) if (currentIndex > range.Columns.Count + 1)
{ {
currentIndex = productColumnIndex.Value - 1; currentIndex = productColumnIndex.Value - 1;
@ -144,23 +149,22 @@ public class GuessReader : IReader
private Dictionary<Product, double> GetDictionaryFromColumns(Range productColumn, Range amountColumn) private Dictionary<Product, double> GetDictionaryFromColumns(Range productColumn, Range amountColumn)
{ {
Dictionary<Product, double> result = new(); 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(); _progressBar.Update();
var amountCells = amountColumn.Value2; var amountCells = amountColumn.Value2;
object currentAmountCell = productColumn.Rows.Count == 1 ? amountCells : amountCells[row, 1]; object currentAmountCell = productColumn.Rows.Count == 1 ? amountCells : amountCells[row, 1];
double? amountValue = currentAmountCell as double?;
if (amountValue == null)
{
continue;
}
var productCells = productColumn.Value2; var productCells = productColumn.Value2;
object currentProductCell = productColumn.Rows.Count == 1 ? productCells : productCells[row, 1]; 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; continue;
} }