Skip to content
This repository has been archived by the owner on Sep 11, 2021. It is now read-only.

Commit

Permalink
#12 Style 적용에 대한 기초 구현
Browse files Browse the repository at this point in the history
  • Loading branch information
jkwchunjae committed Apr 15, 2019
1 parent 0300a17 commit 638dc09
Show file tree
Hide file tree
Showing 6 changed files with 111 additions and 42 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,7 @@
</Compile>
<Compile Include="Extensions\IJsonTokenExtensions.cs" />
<Compile Include="EditorData\CellData.cs" />
<Compile Include="Extensions\StyleExtensions.cs" />
<Compile Include="Extensions\WorksheetExtensions.cs" />
<Compile Include="JsonTokenModel\JsonObjectArray.cs" />
<Compile Include="JsonTokenModel\JsonBoolean.cs" />
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
using ExcelJsonEditorAddin.Theme;
using Microsoft.Office.Core;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Excel = Microsoft.Office.Interop.Excel;

namespace ExcelJsonEditorAddin.Extensions
{
public static class StyleExtensions
{
public static Excel.Style SetDefaultStyle(this Excel.Style style, ThemeType themeType)
{
var ignoreList = new List<Excel.XlBordersIndex>
{
Excel.XlBordersIndex.xlDiagonalDown,
Excel.XlBordersIndex.xlDiagonalUp,
};

switch (themeType)
{
case ThemeType.White:
style.Interior.ColorIndex = XlColorIndex.xlColorIndexNone;
style.Font.Color = Color.Black;
style.Borders.LineStyle = Excel.XlLineStyle.xlLineStyleNone;
break;
case ThemeType.Dark:
style.Interior.Color = Color.FromArgb(30, 30, 30);
style.Font.Color = Color.FromArgb(220, 220, 220);

style.Borders.LineStyle = Excel.XlLineStyle.xlContinuous;
style.Borders.Color = Color.FromArgb(80, 80, 80);
style.Borders.Weight = Excel.XlBorderWeight.xlThin;

ignoreList.ForEach(index =>
{
style.Borders[index].LineStyle = Excel.XlLineStyle.xlLineStyleNone;
});
break;
}
return style;
}

public static Excel.Style SetNumberStyle(this Excel.Style style, ThemeType themeType)
{
style = style.SetDefaultStyle(themeType);

style.Font.Name = "Consolas";

switch (themeType)
{
case ThemeType.White:
break;
case ThemeType.Dark:
style.Font.Color = Color.FromArgb(181, 206, 168);
break;
}
return style;
}

public static Excel.Style SetStringStyle(this Excel.Style style, ThemeType themeType)
{
style = style.SetDefaultStyle(themeType);

style.NumberFormat = "@";

switch (themeType)
{
case ThemeType.White:
break;
case ThemeType.Dark:
break;
}
return style;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@ public static class WorkbookExtensions
}
}

public static IEnumerable<Excel.Style> ToEnumerable(this Excel.Styles styles)
public static IEnumerable<Excel.Style> StyleList(this Excel.Workbook book)
{
foreach (Excel.Style style in styles)
foreach (Excel.Style style in book.Styles)
{
yield return style;
}
Expand All @@ -45,9 +45,10 @@ public static Excel.Workbook Initialize(this Excel.Workbook book, string jsonFil
var jtoken = JsonConvert.DeserializeObject<JToken>(File.ReadAllText(jsonFilePath, Encoding.UTF8));
var jsonToken = jtoken.CreateJsonToken();

book.ChangeTheme(settings.Theme);

Excel.Worksheet sheet = book.SheetList().First();
sheet.Spread(jsonToken, fileName);
book.ChangeTheme(settings.Theme);

book.SaveForJsonEditor(fileName);

Expand Down Expand Up @@ -131,48 +132,20 @@ public static void SaveForJsonEditor(this Excel.Workbook book, string jsonFileNa

public static void ChangeTheme(this Excel.Workbook book, ThemeType themeType)
{
var funcDic = new Dictionary<ThemeType, Action<Excel.Workbook>>()
{
[ThemeType.White] = ChangeThemeWhite,
[ThemeType.Dark] = ChangeThemeDark,
};
var normalStyle = book.Styles[StyleName.Normal];
normalStyle.SetDefaultStyle(themeType);

if (!funcDic.ContainsKey(themeType))
{
return;
}

funcDic[themeType](book);
book.CreateStyle(StyleName.Number).SetNumberStyle(themeType);
book.CreateStyle(StyleName.String).SetStringStyle(themeType);
}

private static void ChangeThemeWhite(this Excel.Workbook book)
public static Excel.Style CreateStyle(this Excel.Workbook book, string styleName)
{
var style = book.Styles["Normal"];
style.Interior.ColorIndex = XlColorIndex.xlColorIndexNone;
style.Font.Color = Color.Black;
style.Borders.LineStyle = Excel.XlLineStyle.xlLineStyleNone;
}

private static void ChangeThemeDark(this Excel.Workbook book)
{
var style = book.Styles["Normal"];
style.Interior.Color = Color.FromArgb(30, 30, 30);
style.Font.Color = Color.FromArgb(220, 220, 220);

var borderIndexList = new List<Excel.XlBordersIndex>
{
Excel.XlBordersIndex.xlDiagonalDown,
Excel.XlBordersIndex.xlDiagonalUp,
};

style.Borders.LineStyle = Excel.XlLineStyle.xlContinuous;
style.Borders.Color = Color.FromArgb(80, 80, 80);
style.Borders.Weight = Excel.XlBorderWeight.xlThin;

borderIndexList.ForEach(index =>
if (book.StyleList().Empty(x => x.Name == styleName))
{
style.Borders[index].LineStyle = Excel.XlLineStyle.xlLineStyleNone;
});
book.Styles.Add(styleName);
}
return book.Styles[styleName];
}

public static Excel.Workbook SpreadJsonToken(this Excel.Workbook book, Excel.Worksheet currentSheet, IJsonToken jsonToken)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Newtonsoft.Json.Linq;
using ExcelJsonEditorAddin.Theme;
using Newtonsoft.Json.Linq;
using Excel = Microsoft.Office.Interop.Excel;

namespace ExcelJsonEditorAddin.JsonTokenModel
Expand All @@ -23,6 +24,7 @@ public void Spread(Excel.Worksheet ws)
public void Spread(Excel.Range cell)
{
cell.Value2 = _token.Value;
cell.Style = StyleName.Number;
}

public bool OnDoubleClick(Excel.Workbook book, Excel.Range target)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Newtonsoft.Json.Linq;
using ExcelJsonEditorAddin.Theme;
using Newtonsoft.Json.Linq;
using Excel = Microsoft.Office.Interop.Excel;

namespace ExcelJsonEditorAddin.JsonTokenModel
Expand All @@ -24,6 +25,7 @@ public void Spread(Excel.Worksheet ws)
public void Spread(Excel.Range cell)
{
cell.Value2 = (string)_token.Value;
cell.Style = StyleName.String;
if (cell.Value2.ToString() != (string) _token.Value)
{
cell.Value2 = "'" + (string) _token.Value;
Expand Down
11 changes: 11 additions & 0 deletions ExcelJsonEditorAddin/ExcelJsonEditorAddin/Theme/ThemeType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,15 @@ public enum ThemeType
White,
Dark,
}

public static class StyleName
{
public static string Normal => "Normal";
public static string Title => "JsonTitle";
public static string Number => "JsonNumber";
public static string String => "JsonString";
public static string Boolean => "JsonBoolean";
public static string Array => "JsonArray";
public static string Object => "JsonObject";
}
}

0 comments on commit 638dc09

Please sign in to comment.