-
Notifications
You must be signed in to change notification settings - Fork 0
/
TableExtensions.cs
140 lines (122 loc) · 5.55 KB
/
TableExtensions.cs
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
namespace xw
{
using System;
using System.Collections.Generic;
using System.Linq;
using OfficeOpenXml;
using XbrlTable;
public static class TableExtensions
{
public static List<Tuple<string, int>> GetColumns(this Table table)
{
return
table.
Axes.
Where(a => a.IsOpen).
Where(a => a.Direction == Direction.Y).
SelectMany(a => a.Ordinates).
OrderBy(o => o.Path).
Select(o => $"*{o.Code}").
Concat(
table.Axes.
Where(a => !a.IsOpen).
FirstOrDefault(a => a.Direction == Direction.X).
Ordinates.
OrderBy(o => o.Path).
Select(o => o.Code)).
Select((o, i) => Tuple.Create(o, i)).
ToList();
}
public static List<Tuple<string, int>> GetRows(this Table table)
{
return
table.
Axes.
Where(a => a.IsOpen).
Where(a => a.Direction == Direction.X).
SelectMany(a => a.Ordinates).
OrderBy(o => o.Path).
Select(o => $"*{o.Code}").
Concat(
table.
Axes.
Where(a => !a.IsOpen).
Where(a => a.Direction == Direction.Y).
DefaultIfEmpty(Axis.DefaultYAxis).
First().
Ordinates.
OrderBy(o => o.Path).
Select(o => o.Code)).
Select((o, i) => Tuple.Create(o, i)).
ToList();
}
private static void PlaceZAxis(this Table table, ExcelWorksheet worksheet, ExcelCoordinate start)
{
table.
Axes.
Where(a => a.Direction == Direction.Z).
Where(a => a.IsOpen).
OrderBy(a => a.Order).
SelectMany(a => a.Ordinates).
OrderBy(o => o.Path).
Select(o => $"*{o.Code}").
Select((o, i) => Tuple.Create(o, start.Column + i)).
ToList().
ForEach(o => worksheet.Cells[start.Row, o.Item2].Value = o.Item1);
}
public static TableSize PlaceTableAxes(this Table table, ExcelWorksheet worksheet, ExcelCoordinate start)
{
table.PlaceZAxis(worksheet, start.Offset(0, 2));
var columns = table.GetColumns();
PlaceColumns(worksheet, columns, start.Offset(2, 2));
var rows = table.GetRows();
PlaceRows(worksheet, rows, start.Offset(3, 1));
var size = new TableSize(columns.Count, rows.Count);
PlaceCellNames(worksheet, rows, columns, table.Code, start.Offset(3, 2));
PlaceDataArea(worksheet, table.Code, start.Offset(3, 2), size);
return size;
}
public static ExcelCoordinate WriteToWorksheet(this Table table, ExcelWorksheet worksheet, ExcelCoordinate start)
{
worksheet.Cells[start.Row, start.Column].Value = table.Code;
var size = table.PlaceTableAxes(worksheet, start);
return start.Add(size);
}
private static void PlaceCellNames(ExcelWorksheet worksheet, List<Tuple<string, int>> rows, List<Tuple<string, int>> columns, string tableCode, ExcelCoordinate start)
{
var cells = rows.SelectMany(r => columns.Select(c => (new[] { r, c }))).ToList();
cells.ForEach(rc => worksheet.Cells[start.Row + rc.First().Item2, start.Column + rc.Last().Item2].Value = GetCellName(tableCode, rc));
cells.ForEach(rc => worksheet.Names.Add(GetCellName(tableCode, rc), worksheet.Cells[start.Row + rc.First().Item2, start.Column + rc.Last().Item2]));
}
private static void PlaceDataArea(ExcelWorksheet worksheet, string tableCode, ExcelCoordinate start, TableSize size)
=> worksheet.Names.Add(GetDataAreaName(tableCode), worksheet.Cells[start.Row, start.Column, start.Row + size.Rows - 1, start.Column + size.Columns - 1]);
private static string GetDataAreaName(string tableCode)
=> $"{CleanTableCode(tableCode)}_DataArea";
private static string GetCellName(string tableCode, Tuple<string, int>[] rc)
{
var result = $"{CleanTableCode(tableCode)}_{CleanOrdinateCode(rc.First().Item1)}_{CleanOrdinateCode(rc.Last().Item1)}";
result = rc.Last().Item1.StartsWith("*")
? $"{result}_xkey"
: rc.First().Item1.StartsWith("*")
? $"{result}_ykey"
: result;
return result;
}
private static string CleanTableCode(string value)
{
return string.IsNullOrEmpty(value)
? ""
: value.Replace(" ", "").ToUpper();
}
private static string CleanOrdinateCode(string value)
{
return string.IsNullOrEmpty(value)
? ""
: value.Replace(" ", "").Replace("*", "").ToUpper();
}
private static void PlaceColumns(ExcelWorksheet worksheet, List<Tuple<string, int>> columns, ExcelCoordinate start)
=> columns.ForEach(o => worksheet.Cells[start.Row, start.Column + o.Item2].Value = o.Item1);
private static void PlaceRows(ExcelWorksheet worksheet, List<Tuple<string, int>> rows, ExcelCoordinate start)
=> rows.ForEach(o => worksheet.Cells[start.Row + o.Item2, start.Column].Value = o.Item1);
}
}