Skip to content

Commit 945c790

Browse files
committed
Implement Copy from Markdown.
1 parent 5776c84 commit 945c790

File tree

9 files changed

+276
-14
lines changed

9 files changed

+276
-14
lines changed
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
7+
namespace CopyFromExcelToMarkdownAddIn
8+
{
9+
public enum Alignment
10+
{
11+
Undefined,
12+
Left,
13+
Center,
14+
Right
15+
}
16+
}

CopyFromExcelToMarkdownAddIn/CopyFromExcelToMarkdownAddIn/CopyFromExcelToMarkdownAddIn.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,7 @@
178178
<Compile Include="GridCell.cs" />
179179
<Compile Include="Grid.cs" />
180180
<Compile Include="GridParser.cs" />
181+
<Compile Include="Alignment.cs" />
181182
<Compile Include="TableCell.cs" />
182183
<Compile Include="TableParser.cs" />
183184
<Compile Include="Properties\AssemblyInfo.cs">

CopyFromExcelToMarkdownAddIn/CopyFromExcelToMarkdownAddIn/GridCell.cs

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,39 @@ public class GridCell
1010
{
1111
public string Value { get; }
1212

13-
public bool IsAlignment =>
14-
System.Text.RegularExpressions.Regex.IsMatch(Value, "^:?-+:?$");
13+
public bool IsAlignment { get; }
14+
15+
16+
public Alignment Alignment { get; }
1517

1618
public GridCell(string value)
1719
{
1820
Value = value;
21+
if (System.Text.RegularExpressions.Regex.IsMatch(Value, "^-+$"))
22+
{
23+
IsAlignment = true;
24+
Alignment = Alignment.Undefined;
25+
}
26+
else if (System.Text.RegularExpressions.Regex.IsMatch(Value, "^:?-+$"))
27+
{
28+
IsAlignment = true;
29+
Alignment = Alignment.Left;
30+
}
31+
else if (System.Text.RegularExpressions.Regex.IsMatch(Value, "^:-+:$"))
32+
{
33+
IsAlignment = true;
34+
Alignment = Alignment.Center;
35+
}
36+
else if (System.Text.RegularExpressions.Regex.IsMatch(Value, "^-+:?$"))
37+
{
38+
IsAlignment = true;
39+
Alignment = Alignment.Right;
40+
}
41+
else
42+
{
43+
IsAlignment = false;
44+
Alignment = Alignment.Undefined;
45+
}
1946
}
2047
}
2148
}

CopyFromExcelToMarkdownAddIn/CopyFromExcelToMarkdownAddIn/Table.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ namespace CopyFromExcelToMarkdownAddIn
88
{
99
public class Table
1010
{
11-
public GridRow Header { get; }
12-
public IList<GridRow> Rows { get; } = new List<GridRow>();
11+
private readonly List<TableRow> _rows = new List<TableRow>();
12+
public IReadOnlyList<TableRow> Rows => _rows;
13+
public void AddRow(TableRow row) => _rows.Add(row);
1314
}
1415
}

CopyFromExcelToMarkdownAddIn/CopyFromExcelToMarkdownAddIn/TableCell.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,13 @@ namespace CopyFromExcelToMarkdownAddIn
88
{
99
public class TableCell
1010
{
11+
public Alignment Alignment { get; }
12+
public string Value { get; }
13+
14+
public TableCell(string value, Alignment alignment)
15+
{
16+
Value = value;
17+
Alignment = alignment;
18+
}
1119
}
1220
}

CopyFromExcelToMarkdownAddIn/CopyFromExcelToMarkdownAddIn/TableParser.cs

Lines changed: 54 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,60 @@ public class TableParser
1111
{
1212
public Table Parse(Grid grid)
1313
{
14-
return null;
14+
if (grid.HasAlignmentRows)
15+
{
16+
return ParseHasAlignmentRows(grid);
17+
}
18+
else
19+
{
20+
return ParseHasNotAlignmentRows(grid);
21+
}
22+
}
23+
24+
private Table ParseHasAlignmentRows(Grid grid)
25+
{
26+
var table = new Table();
27+
table.AddRow(ParseRow(grid, 0));
28+
for (var i = 2; i < grid.Rows.Count; i++)
29+
{
30+
table.AddRow(ParseRow(grid, i));
31+
}
32+
return table;
33+
}
34+
35+
private TableRow ParseRow(Grid grid, int rowIndex)
36+
{
37+
var tableRow = new TableRow();
38+
var gridRow = grid.Rows[rowIndex];
39+
for (int i = 0; i < gridRow.Count; i++)
40+
{
41+
tableRow.AddCell(new TableCell(gridRow[i].Value, GetAlignment(grid, i)));
42+
}
43+
44+
return tableRow;
45+
}
46+
47+
private Alignment GetAlignment(Grid grid, int columnIndex)
48+
{
49+
var row = grid.Rows[1];
50+
if (columnIndex < row.Count)
51+
{
52+
return row[columnIndex].Alignment;
53+
}
54+
else
55+
{
56+
return Alignment.Undefined;
57+
}
58+
}
59+
60+
private Table ParseHasNotAlignmentRows(Grid grid)
61+
{
62+
var table = new Table();
63+
for (var i = 0; i < grid.Rows.Count; i++)
64+
{
65+
table.AddRow(ParseRow(grid, i));
66+
}
67+
return table;
1568
}
1669
}
1770
}

CopyFromExcelToMarkdownAddIn/CopyFromExcelToMarkdownAddIn/ThisAddIn.cs

Lines changed: 66 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,13 @@ public partial class ThisAddIn
1313
/// </summary>
1414
private const int MinRowCount = 3;
1515
/// <summary>
16+
/// Alignment Undefined
17+
/// </summary>
18+
private const int AlignmentUndefined = 1;
19+
/// <summary>
1620
/// Alignment Left
1721
/// </summary>
18-
private const int AlignmentLeft = 1;
22+
private const int AlignmentLeft = -4131;
1923
/// <summary>
2024
/// Alignment Center
2125
/// </summary>
@@ -29,6 +33,11 @@ public partial class ThisAddIn
2933
/// Button in ContextMenu.
3034
/// </summary>
3135
private CommandBarButton _copyToMarkdownButton;
36+
37+
/// <summary>
38+
/// Button in ContextMenu.
39+
/// </summary>
40+
private CommandBarButton _copyFromMarkdownButton;
3241
/// <summary>
3342
/// Startup event handler
3443
/// </summary>
@@ -37,12 +46,62 @@ public partial class ThisAddIn
3746
private void ThisAddIn_Startup(object sender, EventArgs e)
3847
{
3948
// Create button in ContextMenu
40-
var menuItem = MsoControlType.msoControlButton;
41-
_copyToMarkdownButton = (CommandBarButton)Application.CommandBars["GridCell"].Controls.Add(menuItem, missing, missing, 1, true);
49+
_copyToMarkdownButton = (CommandBarButton)Application.CommandBars["Cell"].Controls.Add(MsoControlType.msoControlButton, missing, missing, 1, true);
4250
_copyToMarkdownButton.Style = MsoButtonStyle.msoButtonCaption;
4351
_copyToMarkdownButton.Caption = "Copy to Markdown";
4452
_copyToMarkdownButton.Tag = "0";
4553
_copyToMarkdownButton.Click += CopyToMarkdown;
54+
55+
_copyFromMarkdownButton = (CommandBarButton)Application.CommandBars["Cell"].Controls.Add(MsoControlType.msoControlButton, missing, missing, 2, true);
56+
_copyFromMarkdownButton.Style = MsoButtonStyle.msoButtonCaption;
57+
_copyFromMarkdownButton.Caption = "Copy from Markdown";
58+
_copyFromMarkdownButton.Tag = "1";
59+
_copyFromMarkdownButton.Click += CopyFromMarkdown;
60+
}
61+
62+
private void CopyFromMarkdown(CommandBarButton ctrl, ref bool canceldefault)
63+
{
64+
var text = Clipboard.GetText();
65+
if(string.IsNullOrEmpty(text))
66+
return;
67+
68+
var range = Application.Selection as Range;
69+
if (range == null)
70+
{
71+
MessageBox.Show(Properties.Resources.UnselectedErrorMessage);
72+
return;
73+
}
74+
75+
var table = new TableParser().Parse(new GridParser().Parse(text));
76+
var activeSheet = (Worksheet)Application.ActiveSheet;
77+
78+
for (var i = 0; i < table.Rows.Count; i++)
79+
{
80+
var row = table.Rows[i];
81+
for (var j = 0; j < row.Count; j++)
82+
{
83+
var cell = row[j];
84+
var activeSheetCell = (Range)activeSheet.Cells[range.Row + i, range.Column + j];
85+
activeSheetCell.Value2 = cell.Value.Replace("<br>", Environment.NewLine).Replace("<br/>", Environment.NewLine);
86+
switch (cell.Alignment)
87+
{
88+
case Alignment.Undefined:
89+
activeSheetCell.HorizontalAlignment = AlignmentUndefined;
90+
break;
91+
case Alignment.Left:
92+
activeSheetCell.HorizontalAlignment = AlignmentLeft;
93+
break;
94+
case Alignment.Center:
95+
activeSheetCell.HorizontalAlignment = AlignmentCenter;
96+
break;
97+
case Alignment.Right:
98+
activeSheetCell.HorizontalAlignment = AlignmentRight;
99+
break;
100+
default:
101+
throw new ArgumentOutOfRangeException();
102+
}
103+
}
104+
}
46105
}
47106

48107
/// <summary>
@@ -86,14 +145,17 @@ private void CopyToMarkdown(CommandBarButton ctrl, ref bool cancelDefault)
86145
resultBuffer.Append(FormatText(cell));
87146
switch ((int)cell.HorizontalAlignment)
88147
{
148+
case AlignmentLeft:
149+
separatorBuffer.Append("|:--");
150+
break;
89151
case AlignmentCenter:
90152
separatorBuffer.Append("|:-:");
91153
break;
92154
case AlignmentRight:
93155
separatorBuffer.Append("|--:");
94156
break;
95157
default:
96-
separatorBuffer.Append("|:--");
158+
separatorBuffer.Append("|--");
97159
break;
98160
}
99161
}

CopyFromExcelToMarkdownAddIn/CopyFromExcelToMarkdownAddIna.Test/GridCellFixture.cs

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,47 @@ public class GridCellFixture
1414
public void IsAlignment()
1515
{
1616
Assert.True(new GridCell("-").IsAlignment);
17+
Assert.True(new GridCell("--").IsAlignment);
1718
Assert.True(new GridCell(":-").IsAlignment);
19+
Assert.True(new GridCell(":--").IsAlignment);
1820
Assert.True(new GridCell(":-:").IsAlignment);
21+
Assert.True(new GridCell(":--:").IsAlignment);
1922
Assert.True(new GridCell("-:").IsAlignment);
20-
Assert.True(new GridCell("--").IsAlignment);
23+
Assert.True(new GridCell("--:").IsAlignment);
2124
Assert.False(new GridCell("-:-").IsAlignment);
2225
Assert.False(new GridCell(string.Empty).IsAlignment);
2326
Assert.False(new GridCell(":").IsAlignment);
2427
Assert.False(new GridCell("a").IsAlignment);
2528
Assert.False(new GridCell("-a").IsAlignment);
2629
Assert.False(new GridCell("a-").IsAlignment);
2730
}
31+
32+
[Fact]
33+
public void UndefinedAlignment()
34+
{
35+
Assert.Equal(Alignment.Undefined, new GridCell("-").Alignment);
36+
Assert.Equal(Alignment.Undefined, new GridCell("--").Alignment);
37+
}
38+
39+
[Fact]
40+
public void LeftAlignment()
41+
{
42+
Assert.Equal(Alignment.Left, new GridCell(":-").Alignment);
43+
Assert.Equal(Alignment.Left, new GridCell(":--").Alignment);
44+
}
45+
46+
[Fact]
47+
public void CenterAlignment()
48+
{
49+
Assert.Equal(Alignment.Center, new GridCell(":-:").Alignment);
50+
Assert.Equal(Alignment.Center, new GridCell(":--:").Alignment);
51+
}
52+
53+
[Fact]
54+
public void RightAlignment()
55+
{
56+
Assert.Equal(Alignment.Right, new GridCell("-:").Alignment);
57+
Assert.Equal(Alignment.Right, new GridCell("--:").Alignment);
58+
}
2859
}
2960
}

CopyFromExcelToMarkdownAddIn/CopyFromExcelToMarkdownAddIna.Test/TableParserFixture.cs

Lines changed: 67 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,75 @@ namespace CopyFromExcelToMarkdownAddIna.Test
1111
public class TableParserFixture
1212
{
1313
[Fact]
14-
public void Case01()
14+
public void HasAlignmentRows()
1515
{
1616
var grid = new Grid();
17-
grid.AddRow(new GridRow(new []{new GridCell("a"), new GridCell("b") }));
18-
grid.AddRow(new GridRow(new[] { new GridCell(":-"), new GridCell("-:") }));
19-
grid.AddRow(new GridRow(new[] { new GridCell("1"), new GridCell("2") }));
17+
grid.AddRow(new GridRow(new []{new GridCell("a") }));
18+
grid.AddRow(new GridRow(new[] { new GridCell("-"), new GridCell(":-"), new GridCell(":-:"), new GridCell("-:") }));
19+
grid.AddRow(new GridRow(new[] { new GridCell("1"), new GridCell("2"), new GridCell("3"), new GridCell("4"), new GridCell("5") }));
20+
var table = new TableParser().Parse(grid);
21+
22+
Assert.NotNull(table);
23+
Assert.Equal(2, table.Rows.Count);
24+
25+
Assert.NotNull(table.Rows[0]);
26+
Assert.Single(table.Rows[0]);
27+
Assert.NotNull(table.Rows[0][0]);
28+
Assert.Equal("a", table.Rows[0][0].Value);
29+
Assert.Equal(Alignment.Undefined, table.Rows[0][0].Alignment);
30+
31+
Assert.NotNull(table.Rows[1]);
32+
Assert.Equal(5, table.Rows[1].Count);
33+
34+
Assert.NotNull(table.Rows[1][0]);
35+
Assert.Equal("1", table.Rows[1][0].Value);
36+
Assert.Equal(Alignment.Undefined, table.Rows[1][0].Alignment);
37+
38+
Assert.NotNull(table.Rows[1][1]);
39+
Assert.Equal("2", table.Rows[1][1].Value);
40+
Assert.Equal(Alignment.Left, table.Rows[1][1].Alignment);
41+
42+
Assert.NotNull(table.Rows[1][2]);
43+
Assert.Equal("3", table.Rows[1][2].Value);
44+
Assert.Equal(Alignment.Center, table.Rows[1][2].Alignment);
45+
46+
Assert.NotNull(table.Rows[1][3]);
47+
Assert.Equal("4", table.Rows[1][3].Value);
48+
Assert.Equal(Alignment.Right, table.Rows[1][3].Alignment);
49+
50+
Assert.NotNull(table.Rows[1][4]);
51+
Assert.Equal("5", table.Rows[1][4].Value);
52+
Assert.Equal(Alignment.Undefined, table.Rows[1][4].Alignment);
53+
54+
}
55+
56+
[Fact]
57+
public void HasNotAlignmentRows()
58+
{
59+
var grid = new Grid();
60+
grid.AddRow(new GridRow(new[] { new GridCell("a") }));
61+
grid.AddRow(new GridRow(new[] { new GridCell("-"), new GridCell(":-a") }));
62+
var table = new TableParser().Parse(grid);
63+
64+
Assert.NotNull(table);
65+
Assert.Equal(2, table.Rows.Count);
66+
67+
Assert.NotNull(table.Rows[0]);
68+
Assert.Single(table.Rows[0]);
69+
Assert.NotNull(table.Rows[0][0]);
70+
Assert.Equal("a", table.Rows[0][0].Value);
71+
Assert.Equal(Alignment.Undefined, table.Rows[0][0].Alignment);
72+
73+
Assert.NotNull(table.Rows[1]);
74+
Assert.Equal(2, table.Rows[1].Count);
75+
76+
Assert.NotNull(table.Rows[1][0]);
77+
Assert.Equal("-", table.Rows[1][0].Value);
78+
Assert.Equal(Alignment.Undefined, table.Rows[1][0].Alignment);
79+
80+
Assert.NotNull(table.Rows[1][1]);
81+
Assert.Equal(":-a", table.Rows[1][1].Value);
82+
Assert.Equal(Alignment.Undefined, table.Rows[1][1].Alignment);
2083
}
2184
}
2285
}

0 commit comments

Comments
 (0)