1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
|
using Microsoft.Office.Interop.Excel;
using RhSolutions.AddIn;
using RhSolutions.Models;
using RhSolutions.Services;
using System;
using System.Collections.Generic;
using System.Linq;
namespace RhSolutions.Controllers
{
internal abstract class ToolBase
{
protected Application ExcelApp = RhSolutionsAddIn.Excel;
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<Workbook>()
.FirstOrDefault(w => w.FullName == RegistryUtil.PriceListPath) != null)
{
throw new ArgumentException("Шаблонный файл редактируется в другом месте");
}
Workbook wb = ExcelApp.Workbooks.Open(RegistryUtil.PriceListPath, null, true);
try
{
TargetFile = new TargetPriceList(wb);
}
catch (Exception exception)
{
if (wb != null)
{
wb.Close();
}
throw exception;
}
}
protected void FillPositionAmountToColumns(KeyValuePair<Product, double> positionAmount, params int[] columns)
{
Range worksheetCells = TargetFile.Sheet.Cells;
Range skuColumn = TargetFile.SkuCell.EntireColumn;
Range oldSkuColumn = TargetFile.OldSkuCell.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(oldSkuColumn, 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<Product, double> 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, "<>");
TargetFile.Sheet.Range["A1"].Activate();
}
}
}
|