Skip to content

Commit 122cbcb

Browse files
committed
Refactoring Extruct Method & If Block.
1 parent c2a03fd commit 122cbcb

File tree

3 files changed

+60
-50
lines changed

3 files changed

+60
-50
lines changed

CopyFromExcelToMarkdownAddIn/CopyFromExcelToMarkdownAddIn/CopyFromExcelToMarkdownAddIn.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
<PublishUrl>C:\Users\z00h800459\Source\Repos\CopyFromExcelToMarkdownAddIn\publish\</PublishUrl>
3535
<InstallUrl />
3636
<TargetCulture>ja</TargetCulture>
37-
<ApplicationVersion>1.0.1.1</ApplicationVersion>
37+
<ApplicationVersion>1.0.2.1</ApplicationVersion>
3838
<AutoIncrementApplicationRevision>true</AutoIncrementApplicationRevision>
3939
<UpdateEnabled>true</UpdateEnabled>
4040
<UpdateInterval>7</UpdateInterval>

CopyFromExcelToMarkdownAddIn/CopyFromExcelToMarkdownAddIn/Properties/AssemblyInfo.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,6 @@
3131
// すべての値を指定するか、以下のように '*' を使ってビルドおよびリビジョン番号を
3232
// 既定値にすることができます。
3333
// [assembly: AssemblyVersion("1.0.*")]
34-
[assembly: AssemblyVersion("1.0.1.*")]
34+
[assembly: AssemblyVersion("1.0.2.*")]
3535
[assembly: AssemblyFileVersion("1.0.0.0")]
3636

CopyFromExcelToMarkdownAddIn/CopyFromExcelToMarkdownAddIn/ThisAddIn.cs

Lines changed: 58 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -62,63 +62,73 @@ private void ThisAddIn_Shutdown(object sender, EventArgs e)
6262
private void CopyToMarkdown(CommandBarButton ctrl, ref bool cancelDefault)
6363
{
6464
var range = Application.Selection as Range;
65-
if (range != null)
65+
if (range == null)
6666
{
67-
var rowsCount = range.Rows.Count;
68-
if (MinRowCount <= rowsCount)
69-
{
70-
var columnsCount = range.Count / rowsCount;
71-
var resultBuffer = new StringBuilder();
72-
var separatorBuffer = new StringBuilder();
73-
for (int x = 1; x <= columnsCount; x++)
74-
{
75-
var cell = (Range)range.Cells[1, x];
76-
77-
resultBuffer.Append("|");
78-
resultBuffer.Append(cell.Text == null ? string.Empty : cell.Text.Replace("\n", "<br>"));
79-
switch ((int)cell.HorizontalAlignment)
80-
{
81-
case AlignmentCenter:
82-
separatorBuffer.Append("|:-:");
83-
break;
84-
case AlignmentRight:
85-
separatorBuffer.Append("|--:");
86-
break;
87-
default:
88-
separatorBuffer.Append("|:--");
89-
break;
90-
}
91-
}
92-
// Partition of the header and data lines.
93-
// Process only after the first line.
94-
resultBuffer.Append("|");
95-
resultBuffer.Append(Environment.NewLine);
96-
separatorBuffer.Append("|");
97-
separatorBuffer.Append(Environment.NewLine);
98-
resultBuffer.Append(separatorBuffer);
67+
MessageBox.Show(Properties.Resources.UnselectedErrorMessage);
68+
return;
69+
}
70+
71+
var rowsCount = range.Rows.Count;
72+
if (rowsCount < MinRowCount)
73+
{
74+
MessageBox.Show(Properties.Resources.UnselectedErrorMessage);
75+
return;
76+
}
9977

100-
for (int y = 2; y <= rowsCount; y++)
101-
{
102-
for (int x = 1; x <= columnsCount; x++)
103-
{
104-
var cell = (Range)range.Cells[y, x];
78+
var columnsCount = range.Count / rowsCount;
79+
var resultBuffer = new StringBuilder();
80+
var separatorBuffer = new StringBuilder();
81+
for (int x = 1; x <= columnsCount; x++)
82+
{
83+
var cell = (Range)range.Cells[1, x];
10584

106-
resultBuffer.Append("|");
107-
resultBuffer.Append(cell.Text == null ? string.Empty : cell.Text.Replace("\n","<br>"));
108-
}
109-
resultBuffer.Append("|");
110-
resultBuffer.Append(Environment.NewLine);
111-
}
112-
Clipboard.SetText(resultBuffer.ToString());
85+
resultBuffer.Append("|");
86+
resultBuffer.Append(FormatText(cell));
87+
switch ((int)cell.HorizontalAlignment)
88+
{
89+
case AlignmentCenter:
90+
separatorBuffer.Append("|:-:");
91+
break;
92+
case AlignmentRight:
93+
separatorBuffer.Append("|--:");
94+
break;
95+
default:
96+
separatorBuffer.Append("|:--");
97+
break;
11398
}
114-
else
99+
}
100+
// Partition of the header and data lines.
101+
// Process only after the first line.
102+
resultBuffer.Append("|");
103+
resultBuffer.Append(Environment.NewLine);
104+
separatorBuffer.Append("|");
105+
separatorBuffer.Append(Environment.NewLine);
106+
resultBuffer.Append(separatorBuffer);
107+
108+
for (int y = 2; y <= rowsCount; y++)
109+
{
110+
for (int x = 1; x <= columnsCount; x++)
115111
{
116-
MessageBox.Show(Properties.Resources.UnselectedErrorMessage);
112+
var cell = (Range)range.Cells[y, x];
113+
114+
resultBuffer.Append("|");
115+
resultBuffer.Append(FormatText(cell));
117116
}
117+
resultBuffer.Append("|");
118+
resultBuffer.Append(Environment.NewLine);
119+
}
120+
Clipboard.SetText(resultBuffer.ToString());
121+
}
122+
123+
private static string FormatText(Range range)
124+
{
125+
if (range == null || range.Text == null)
126+
{
127+
return string.Empty;
118128
}
119129
else
120130
{
121-
MessageBox.Show(Properties.Resources.UnselectedErrorMessage);
131+
return range.Text.Replace("\n", "<br>");
122132
}
123133
}
124134

0 commit comments

Comments
 (0)