using Microsoft.Office.Interop.Excel; using RhSolutions.AddIn; using RhSolutions.Models; using RhSolutions.Services; using System; using System.Collections.Generic; using System.Linq; using Range = Microsoft.Office.Interop.Excel.Range; namespace RhSolutions.Controllers { internal abstract class ToolBase { protected Application ExcelApp = RhSolutionsAddIn.Excel; protected IAddInConfiguration Configuration = RhSolutionsAddIn.Configuration; protected TargetPriceList TargetFile { get; set; } protected ResultBar ResultBar { get; set; } protected ProgressBar ProgressBar { get; set; } public abstract void FillTarget(); public void OpenNewPrice() { if (ExcelApp.Workbooks .Cast() .FirstOrDefault(w => w.FullName == Configuration.GetPriceListPath()) != null) { throw new ArgumentException("Шаблонный файл редактируется в другом месте"); } Workbook wb = ExcelApp.Workbooks.Open(Configuration.GetPriceListPath(), null, true); try { TargetFile = new TargetPriceList(wb); } catch (Exception exception) { if (wb != null) { wb.Close(); } throw exception; } } protected void FillPositionAmountToColumns(KeyValuePair positionAmount, params int[] columns) { Range worksheetCells = TargetFile.Sheet.Cells; Range skuColumn = TargetFile.SkuCell.EntireColumn; int? row = GetPositionRow(skuColumn, positionAmount.Key.ProductSku, positionAmount.Key.ProductLine); if (row != null) { foreach (int column in columns) { Range cell = worksheetCells[row, column]; cell.AddValue(positionAmount.Value); } ResultBar.IncrementSuccess(); return; } if (TargetFile.OldSkuCell != null) { row = GetPositionRow(TargetFile.OldSkuCell.EntireColumn, positionAmount.Key.ProductSku, positionAmount.Key.ProductLine); if (row != null) { foreach (int column in columns) { Range cell = worksheetCells[row, column]; cell.AddValue(positionAmount.Value); } ResultBar.IncrementReplaced(); return; } } string sku = positionAmount.Key.ProductSku.Substring(1, 6); row = GetPositionRow(skuColumn, sku, positionAmount.Key.ProductLine); if (row != null) { foreach (int column in columns) { Range cell = worksheetCells[row, column]; cell.AddValue(positionAmount.Value); } ResultBar.IncrementReplaced(); return; } FillMissing(positionAmount, columns); ResultBar.IncrementNotFound(); } protected void FillMissing(KeyValuePair positionAmount, params int[] columns) { Range worksheetCells = TargetFile.Sheet.Cells; Range worksheetRows = TargetFile.Sheet.Rows; int skuColumn = TargetFile.SkuCell.Column; int groupColumn = TargetFile.GroupCell.Column; int nameColumn = TargetFile.NameCell.Column; int row = worksheetCells[worksheetRows.Count, skuColumn] .End[XlDirection.xlUp] .Row + 1; worksheetRows[row] .EntireRow .Insert(XlInsertShiftDirection.xlShiftDown, XlInsertFormatOrigin.xlFormatFromLeftOrAbove); Range previous = worksheetRows[row - 1]; Range current = worksheetRows[row]; previous.Copy(current); current.ClearContents(); worksheetCells[row, groupColumn].Value2 = positionAmount.Key.ProductLine; worksheetCells[row, nameColumn].Value2 = positionAmount.Key.Name; if (TargetFile.OldSkuCell != null) { worksheetCells[row, skuColumn].Value2 = "Не найден"; worksheetCells[row, TargetFile.OldSkuCell.Column].Value2 = positionAmount.Key.ProductSku; } else { worksheetCells[row, skuColumn].Value2 = positionAmount.Key.ProductSku; } foreach (int column in columns) { Range cell = worksheetCells[row, column]; cell.AddValue(positionAmount.Value); } } protected int? GetPositionRow(Range range, string sku, string group) { Range found = range.Find(sku); string foundGroupValue; if (found == null) { return null; } int firstFoundRow = found.Row; if (string.IsNullOrEmpty(group)) { return found.Row; } while (true) { foundGroupValue = TargetFile.Sheet.Cells[found.Row, TargetFile.GroupCell.Column].Value2.ToString(); if (group.Equals(foundGroupValue)) { return found.Row; } found = range.FindNext(found); if (found.Row == firstFoundRow) { return null; } } } protected void FilterByAmount() { AutoFilter filter = TargetFile.Sheet.AutoFilter; int startColumn = filter.Range.Column; filter.Range.AutoFilter(TargetFile.AmountCell.Column - startColumn + 1, "<>0", XlAutoFilterOperator.xlAnd, "<>"); TargetFile.Sheet.Range["A1"].Activate(); } } }