Skip to content

Commit 1ecf4d3

Browse files
committed
Modified the Markdown pipe notation to handle it properly.
1 parent 2ef81c2 commit 1ecf4d3

File tree

7 files changed

+169
-48
lines changed

7 files changed

+169
-48
lines changed

CopyFromExcelToMarkdownAddIn/CopyFromExcelToMarkdownAddIn/CopyFromExcelToMarkdownAddIn.csproj

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,10 @@
3232
<IsWebBootstrapper>False</IsWebBootstrapper>
3333
<TargetFrameworkProfile />
3434
<BootstrapperEnabled>true</BootstrapperEnabled>
35-
<PublishUrl>C:\Repos\CopyToMarkdownAddIn\release\</PublishUrl>
35+
<PublishUrl>release\</PublishUrl>
3636
<InstallUrl />
3737
<TargetCulture>ja</TargetCulture>
38-
<ApplicationVersion>2.1.1.3</ApplicationVersion>
38+
<ApplicationVersion>2.1.2.1</ApplicationVersion>
3939
<AutoIncrementApplicationRevision>true</AutoIncrementApplicationRevision>
4040
<UpdateEnabled>true</UpdateEnabled>
4141
<UpdateInterval>7</UpdateInterval>
@@ -185,6 +185,7 @@
185185
<Compile Include="Grid.cs" />
186186
<Compile Include="GridParser.cs" />
187187
<Compile Include="Alignment.cs" />
188+
<Compile Include="RangeExtensions.cs" />
188189
<Compile Include="TableCell.cs" />
189190
<Compile Include="TableParser.cs" />
190191
<Compile Include="Properties\AssemblyInfo.cs">
@@ -254,7 +255,7 @@
254255
<FlavorProperties GUID="{BAA0C2D2-18E2-41B9-852F-F413020CAA33}">
255256
<ProjectProperties HostName="Excel" HostPackage="{29A7B9D7-A7F1-4328-8EF0-6B2D1A56B2C1}" OfficeVersion="15.0" VstxVersion="4.0" ApplicationType="Excel" Language="cs" TemplatesPath="" DebugInfoExeName="#Software\Microsoft\Office\16.0\Excel\InstallRoot\Path#excel.exe" DebugInfoCommandLine="/x" AddItemTemplatesGuid="{51063C3A-E220-4D12-8922-BDA915ACD783}" />
256257
<Host Name="Excel" GeneratedCodeNamespace="CopyFromExcelToMarkdownAddIn" PublishedHash="69C324AB27932AA2FBF2B7EA72250886FF164DE6" IconIndex="0">
257-
<HostItem Name="ThisAddIn" Code="ThisAddIn.cs" CanonicalName="AddIn" PublishedHash="CC91B31BC641E18A7E391B0BA09134AF7C38EB7E" CanActivate="false" IconIndex="1" Blueprint="ThisAddIn.Designer.xml" GeneratedCode="ThisAddIn.Designer.cs" />
258+
<HostItem Name="ThisAddIn" Code="ThisAddIn.cs" CanonicalName="AddIn" PublishedHash="1A126CC55C794B3652F850A7B0398122F0A29FF5" CanActivate="false" IconIndex="1" Blueprint="ThisAddIn.Designer.xml" GeneratedCode="ThisAddIn.Designer.cs" />
258259
</Host>
259260
</FlavorProperties>
260261
</VisualStudio>
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
using Microsoft.Office.Interop.Excel;
7+
8+
namespace CopyFromExcelToMarkdownAddIn
9+
{
10+
public static class RangeExtensions
11+
{
12+
private const string ExcelEnterNotation = "\n";
13+
private const string MarkdownEnterNotation = "<br>";
14+
private const string ExcelPipeNotation = "|";
15+
private const string MarkdownPipeNotation = "&#124;";
16+
17+
18+
public static string FormatText(this Range range)
19+
{
20+
if (range == null || range.Text == null)
21+
{
22+
return string.Empty;
23+
}
24+
else
25+
{
26+
return range.Text
27+
.Replace(ExcelEnterNotation, MarkdownEnterNotation)
28+
.Replace(ExcelPipeNotation, MarkdownPipeNotation);
29+
}
30+
}
31+
32+
}
33+
}

CopyFromExcelToMarkdownAddIn/CopyFromExcelToMarkdownAddIn/ThisAddIn.cs

Lines changed: 7 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ public partial class ThisAddIn
1111
/// <summary>
1212
/// Minimum required select the number of rows.
1313
/// </summary>
14-
private const int MinRowCount = 3;
14+
private const int MinRowCount = 2;
1515
/// <summary>
1616
/// Alignment Undefined
1717
/// </summary>
@@ -107,7 +107,10 @@ private void CopyFromMarkdown(CommandBarButton ctrl, ref bool canceldefault)
107107
{
108108
var cell = row[j];
109109
var activeSheetCell = (Range)activeSheet.Cells[range.Row + i, range.Column + j];
110-
activeSheetCell.Value2 = cell.Value.Replace("<br>", "\n").Replace("<br/>", "\n");
110+
activeSheetCell.Value2 = cell.Value
111+
.Replace("<br>", "\n")
112+
.Replace("<br/>", "\n")
113+
.Replace("&#124;", "|");
111114
switch (cell.Alignment)
112115
{
113116
case Alignment.Undefined:
@@ -167,7 +170,7 @@ private void CopyToMarkdown(CommandBarButton ctrl, ref bool cancelDefault)
167170
var cell = (Range)range.Cells[1, x];
168171

169172
resultBuffer.Append("|");
170-
resultBuffer.Append(FormatText(cell));
173+
resultBuffer.Append(cell.FormatText());
171174
switch ((int)cell.HorizontalAlignment)
172175
{
173176
case AlignmentLeft:
@@ -199,27 +202,14 @@ private void CopyToMarkdown(CommandBarButton ctrl, ref bool cancelDefault)
199202
var cell = (Range)range.Cells[y, x];
200203

201204
resultBuffer.Append("|");
202-
resultBuffer.Append(FormatText(cell));
205+
resultBuffer.Append(cell.FormatText());
203206
}
204207
resultBuffer.Append("|");
205208
resultBuffer.Append(Environment.NewLine);
206209
}
207210
Clipboard.SetText(resultBuffer.ToString());
208211
}
209212

210-
private static string FormatText(Range range)
211-
{
212-
if (range == null || range.Text == null)
213-
{
214-
return string.Empty;
215-
}
216-
else
217-
{
218-
return range.Text.Replace("\n", "<br>");
219-
}
220-
}
221-
222-
223213
#region VSTO で生成されたコード
224214

225215
/// <summary>

CopyFromExcelToMarkdownAddIn/CopyFromExcelToMarkdownAddIna.Test/CopyFromExcelToMarkdownAddIna.Test.csproj

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,32 @@
3535
<WarningLevel>4</WarningLevel>
3636
</PropertyGroup>
3737
<ItemGroup>
38+
<Reference Include="Castle.Core, Version=4.0.0.0, Culture=neutral, PublicKeyToken=407dd0808d44fbdc, processorArchitecture=MSIL">
39+
<HintPath>..\packages\Castle.Core.4.4.0\lib\net45\Castle.Core.dll</HintPath>
40+
</Reference>
41+
<Reference Include="FluentAssertions, Version=5.10.3.0, Culture=neutral, PublicKeyToken=33f2691a05b67b6a, processorArchitecture=MSIL">
42+
<HintPath>..\packages\FluentAssertions.5.10.3\lib\net45\FluentAssertions.dll</HintPath>
43+
</Reference>
44+
<Reference Include="Microsoft.Office.Interop.Excel, Version=15.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c, processorArchitecture=MSIL">
45+
<SpecificVersion>False</SpecificVersion>
46+
<EmbedInteropTypes>True</EmbedInteropTypes>
47+
<HintPath>..\..\..\..\Program Files (x86)\Microsoft Visual Studio\Shared\Visual Studio Tools for Office\PIA\Office15\Microsoft.Office.Interop.Excel.dll</HintPath>
48+
</Reference>
49+
<Reference Include="Moq, Version=4.14.0.0, Culture=neutral, PublicKeyToken=69f491c39445e920, processorArchitecture=MSIL">
50+
<HintPath>..\packages\Moq.4.14.5\lib\net45\Moq.dll</HintPath>
51+
</Reference>
3852
<Reference Include="System" />
53+
<Reference Include="System.Configuration" />
3954
<Reference Include="System.Core" />
55+
<Reference Include="System.Runtime.CompilerServices.Unsafe, Version=4.0.4.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
56+
<HintPath>..\packages\System.Runtime.CompilerServices.Unsafe.4.5.0\lib\netstandard2.0\System.Runtime.CompilerServices.Unsafe.dll</HintPath>
57+
</Reference>
58+
<Reference Include="System.Threading.Tasks.Extensions, Version=4.2.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51, processorArchitecture=MSIL">
59+
<HintPath>..\packages\System.Threading.Tasks.Extensions.4.5.1\lib\netstandard2.0\System.Threading.Tasks.Extensions.dll</HintPath>
60+
</Reference>
61+
<Reference Include="System.ValueTuple, Version=4.0.2.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51, processorArchitecture=MSIL">
62+
<HintPath>..\packages\System.ValueTuple.4.4.0\lib\net461\System.ValueTuple.dll</HintPath>
63+
</Reference>
4064
<Reference Include="System.Xml.Linq" />
4165
<Reference Include="System.Data.DataSetExtensions" />
4266
<Reference Include="Microsoft.CSharp" />
@@ -59,6 +83,7 @@
5983
<ItemGroup>
6084
<Compile Include="GridCellFixture.cs" />
6185
<Compile Include="GridParserFixture.cs" />
86+
<Compile Include="RangeExtensionsTest.cs" />
6287
<Compile Include="TableParserFixture.cs" />
6388
<Compile Include="Properties\AssemblyInfo.cs" />
6489
<Compile Include="Properties\Resources.Designer.cs">
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
using CopyFromExcelToMarkdownAddIn;
7+
using FluentAssertions;
8+
using Microsoft.Office.Interop.Excel;
9+
using Range = Microsoft.Office.Interop.Excel.Range;
10+
using Moq;
11+
using Xunit;
12+
13+
namespace CopyFromExcelToMarkdownAddIna.Test
14+
{
15+
public class RangeExtensionsTest
16+
{
17+
[Fact]
18+
public void WhenRangeIsNull()
19+
{
20+
var range = (Range)null;
21+
range.FormatText()
22+
.Should().NotBeNull()
23+
.And.BeEmpty();
24+
}
25+
26+
[Fact]
27+
public void WhenRangeTextIsNull()
28+
{
29+
var range = new Mock<Range>();
30+
range.SetupGet(x => x.Text).Returns(null);
31+
range.Object.FormatText()
32+
.Should().NotBeNull()
33+
.And.BeEmpty();
34+
}
35+
36+
[Fact]
37+
public void WhenRangeTextIsEmpty()
38+
{
39+
var range = new Mock<Range>();
40+
range.SetupGet(x => x.Text).Returns(string.Empty);
41+
range.Object.FormatText()
42+
.Should().NotBeNull()
43+
.And.BeEmpty();
44+
}
45+
46+
[Fact]
47+
public void WhenTextCoantaintsEnter()
48+
{
49+
var range = new Mock<Range>();
50+
range.SetupGet(x => x.Text).Returns("a\nb");
51+
range.Object.FormatText()
52+
.Should().Be("a<br>b");
53+
}
54+
55+
[Fact]
56+
public void WhenTextCoantaintsPipe()
57+
{
58+
var range = new Mock<Range>();
59+
range.SetupGet(x => x.Text).Returns("a|b");
60+
range.Object.FormatText()
61+
.Should().Be("a&#124;b");
62+
}
63+
}
64+
}
65+
66+

CopyFromExcelToMarkdownAddIn/CopyFromExcelToMarkdownAddIna.Test/packages.config

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<packages>
3+
<package id="Castle.Core" version="4.4.0" targetFramework="net461" />
4+
<package id="FluentAssertions" version="5.10.3" targetFramework="net461" />
5+
<package id="Moq" version="4.14.5" targetFramework="net461" />
6+
<package id="System.Runtime.CompilerServices.Unsafe" version="4.5.0" targetFramework="net461" />
7+
<package id="System.Threading.Tasks.Extensions" version="4.5.1" targetFramework="net461" />
8+
<package id="System.ValueTuple" version="4.4.0" targetFramework="net461" />
39
<package id="xunit" version="2.4.1" targetFramework="net461" />
410
<package id="xunit.abstractions" version="2.0.3" targetFramework="net461" />
511
<package id="xunit.analyzers" version="0.10.0" targetFramework="net452" />

README-jp.md

Lines changed: 28 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,54 +1,54 @@
11
# Copy to Markdown AddIn
22

3-
選択したExcelのセルをMarkdown形式へコピーと、逆にMarkdown形式の表をExcelに双方向にコピーするExcelアドインです。
3+
選択したExcelのセルをMarkdown形式へコピーと、逆にMarkdown形式の表をExcelに双方向にコピーするExcelアドインです。
44

5-
![screenshot](docs/images/screenshot.ja-JP.gif)
5+
![screenshot](docs/images/screenshot.ja-JP.gif)
66

7-
日付の書式、金額、カンマフォーマットなども生かしたままコピーできます。
8-
右寄せ・左寄はヘッダー行の設定に従います。
9-
Markdown形式のブログでテーブルを記述する際などにご利用ください。
7+
日付の書式、金額、カンマフォーマットなども生かしたままコピーできます。
8+
右寄せ・左寄はヘッダー行の設定に従います。
9+
Markdown形式のブログでテーブルを記述する際などにご利用ください。
1010

11-
### 動作確認済みのOffice&Windows
11+
### 動作確認済みのOffice&Windows
1212

13-
* Windows 7 64bit & Office 2010
13+
* Windows 7 64bit & Office 2010
1414
* Windows 10 64bit & Office 2016
1515

16-
### 必要環境
16+
### 必要環境
1717

18-
* .NET Framework 4.5.2以上
19-
* Visual Studio 2010 Tools for Office Runtimeのインストールが求められるかもしれません
20-
21-
上記モジュールがインストールされていない場合、インストーラーからダウンロードされてインストールされる可能性があります。
18+
* .NET Framework 4.5.2以上
19+
* Visual Studio 2010 Tools for Office Runtimeのインストールが求められるかもしれません
2220

21+
上記モジュールがインストールされていない場合、インストーラーからダウンロードされてインストールされる可能性があります。
2322

24-
### 操作方法
2523

26-
貼り付けたい範囲を選択して、右クリックから「Copy to Markdown」を選択して、あとは好きなところに貼り付けましょう!
27-
それだけです。
24+
### 操作方法
2825

29-
### ダウンロード
26+
貼り付けたい範囲を選択して、右クリックから「Copy to Markdown」を選択して、あとは好きなところに貼り付けましょう!
27+
それだけです。
3028

31-
Github上にzip形式でインストーラーを置いてあります。
29+
### ダウンロード
3230

33-
1. 以下からダウンロードする
31+
Github上にzip形式でインストーラーを置いてあります。
32+
33+
1. 以下からダウンロードする
3434
[https://github.com/nuitsjp/CopyToMarkdownAddIn/releases](https://github.com/nuitsjp/CopyToMarkdownAddIn/releases)
35-
2. <span style="color: #ff0000"><b>zipファイル右クリックしてプロパティを開き、「ブロックの解除」のチェックをONにする</b></span>
36-
3. setup.exeを実行する
35+
2. <span style="color: #ff0000"><b>zipファイル右クリックしてプロパティを開き、「ブロックの解除」のチェックをONにする</b></span>
36+
3. setup.exeを実行する
3737

38-
<b><span style="color: #ff0000">※要注意※
39-
zipファイルのダウンロード後、展開する前に必ずファイルのプロパティを開き、「ブロックの解除」をチェックした上で「OK」ボタンを押してください。
38+
<b><span style="color: #ff0000">※要注意※
39+
zipファイルのダウンロード後、展開する前に必ずファイルのプロパティを開き、「ブロックの解除」をチェックした上で「OK」ボタンを押してください。
4040
</span></b>
4141
![](docs/images/zip.png)
4242

43-
### ライセンス
43+
### ライセンス
4444

45-
個人・商用に関わらず完全に無料でお使いいただけます。
45+
個人・商用に関わらず完全に無料でお使いいただけます。
4646

47-
### お問い合わせ先
47+
### お問い合わせ先
4848

49-
何かありましたら
49+
何かありましたら
5050

51-
* Issue欄
51+
* Issue欄
5252
* Twitter:[@nuits_jp](https://twitter.com/nuits_jp)
5353

54-
いずれかに連絡ください。
54+
いずれかに連絡ください。

0 commit comments

Comments
 (0)