Skip to content

Commit

Permalink
重整程式碼
Browse files Browse the repository at this point in the history
  • Loading branch information
maxzh1999tw committed Jan 15, 2024
1 parent d1f22f5 commit 2f78379
Show file tree
Hide file tree
Showing 12 changed files with 49 additions and 61 deletions.
14 changes: 7 additions & 7 deletions Excely.ClosedXML.UnitTests/Models/SimpleClass.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
namespace Excely.ClosedXML.UnitTests.Models
{
internal class SimpleClass
{
public int IntValue { get; set; }
public string StrigValue { get; set; } = "";
public DateTime DateTimeValue { get; set; }
public SampleEnum EnumValue { get; set; }
}
internal class SimpleClass
{
public int IntValue { get; set; }
public string StrigValue { get; set; } = "";
public DateTime DateTimeValue { get; set; }
public SampleEnum EnumValue { get; set; }
}
}
48 changes: 24 additions & 24 deletions Excely.ClosedXML.UnitTests/XlsxTableConverterTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,31 +3,31 @@

namespace Excely.ClosedXML.UnitTests
{
[TestClass]
public class XlsxTableConverterTests
{
/// <summary>
/// 確保 ClosedXML 在處理字串時不會自動轉型為數字
/// </summary>
[TestMethod]
public void ConvertPaddingStartWith0Data_ShouldReturnExcelWithCurrectData()
{
// Arrange
using var workbook = new XLWorkbook();
var worksheet = workbook.AddWorksheet("sheet1");
[TestClass]
public class XlsxTableConverterTests
{
/// <summary>
/// 確保 ClosedXML 在處理字串時不會自動轉型為數字
/// </summary>
[TestMethod]
public void ConvertPaddingStartWith0Data_ShouldReturnExcelWithCurrectData()
{
// Arrange
using var workbook = new XLWorkbook();
var worksheet = workbook.AddWorksheet("sheet1");

var converter = new XlsxTableConverter(worksheet);
var table = new ExcelyTable(new List<IList<object?>>
{
new List<object?> { "Name", "No" },
new List<object?> { "John", "01" },
});
var converter = new XlsxTableConverter(worksheet);
var table = new ExcelyTable(new List<IList<object?>>
{
new List<object?> { "Name", "No" },
new List<object?> { "John", "01" },
});

// Act
converter.ConvertFrom(table);
// Act
converter.ConvertFrom(table);

// Assert
Assert.AreEqual("01", worksheet.Cell(2,2).Value.ToString());
}
}
// Assert
Assert.AreEqual("01", worksheet.Cell(2, 2).Value.ToString());
}
}
}
2 changes: 1 addition & 1 deletion Excely.ClosedXML/Shaders/TableThemeShader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public TableThemeShader(int tableWidth, int tableHeight)
protected override void ExcuteOnWorksheet(IXLWorksheet worksheet)
{
var tableWidth = TableWidth;
if(TableWidth == 0)
if (TableWidth == 0)
{
tableWidth = worksheet.LastColumnUsed().ColumnNumber();
}
Expand Down
1 change: 0 additions & 1 deletion Excely.ClosedXML/Workflows/XlsxExporterExtension.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using ClosedXML.Excel;
using Excely.ClosedXML.TableConverters;
using Excely.Workflows;

namespace Excely.Workflows
{
Expand Down
1 change: 0 additions & 1 deletion Excely.ClosedXML/Workflows/XlsxImporter.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using ClosedXML.Excel;
using Excely.ClosedXML.TableFactories;
using Excely.Workflows;

namespace Excely.Workflows
{
Expand Down
9 changes: 2 additions & 7 deletions Excely.UnitTests/CsvStringTableConverterTests.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
using Excely.TableFactories;
using System;
using System.Collections.Generic;
using System.Linq;
using Excely.TableConverters;
using System.Text;
using System.Threading.Tasks;
using Excely.TableConverters;

namespace Excely.UnitTests
{
Expand All @@ -28,7 +23,7 @@ public void ConvertFrom_ConvertToStringWithSimpleData_ShouldReturnCorrectCsvStri

// Assert
Assert.AreEqual(
$"Name,Surname,Age{Environment.NewLine}John,Doe,30{Environment.NewLine}Jane,Doe,",
$"Name,Surname,Age{Environment.NewLine}John,Doe,30{Environment.NewLine}Jane,Doe,",
csvString);
}

Expand Down
12 changes: 6 additions & 6 deletions Excely.UnitTests/DictionaryListTableFactoryTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@ public void GetTable_WithDefaultOptions_ShouldIncludeAllPropertiesWithSchema()
// Arrange
var factory = new DictionaryListTableFactory();
var testData = new List<Dictionary<string, object?>>
{
new Dictionary<string, object?>
{
{ "Id", 1 },
{ "Name", "Alice" }
}
{
new Dictionary<string, object?>
{
{ "Id", 1 },
{ "Name", "Alice" }
}
};

// Act
Expand Down
2 changes: 1 addition & 1 deletion Excely/TableConverters/ClassListTableConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ private IEnumerable<TClass> ImportInternal(ExcelyTable table, Func<PropertyInfo,
{
value = typeConverter.ConvertFrom(value);
}
else if (value is IConvertible &&
else if (value is IConvertible &&
property.PropertyType.GetInterface(nameof(IConvertible)) != null) // 嘗試強制轉型
{
try
Expand Down
5 changes: 1 addition & 4 deletions Excely/TableConverters/DictionaryListTableConverter.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
using System.Formats.Asn1;
using System.Reflection;

namespace Excely.TableConverters
namespace Excely.TableConverters
{
/// <summary>
/// 將 Table 轉換為字典列表。
Expand Down
2 changes: 1 addition & 1 deletion Excely/TableFactories/ClassListTableFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public ClassListTableFactory(ClassListTableFactoryOptions<TClass> options)

public ExcelyTable GetTable(IEnumerable<TClass> sourceData)
{
if(sourceData == null) throw new ArgumentNullException(nameof(sourceData));
if (sourceData == null) throw new ArgumentNullException(nameof(sourceData));

// 取得要匯出的 Properties
var properties = typeof(TClass).GetProperties();
Expand Down
9 changes: 5 additions & 4 deletions Excely/TableFactories/CsvStringTableFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public ExcelyTable GetTable(string sourceData)
isInQuotes = false;
}
}

// 其餘字元一律視為文字
else
{
Expand Down Expand Up @@ -136,20 +136,21 @@ public ExcelyTable GetTable(string sourceData)
}

// 刪除空行
if(result.Any(row => row.Count > 1))
if (result.Any(row => row.Count > 1))
{
result = result.Where(x => x.Count != 1).ToList();
}

return new ExcelyTable(result.Where(x => x.Any()).ToArray());
}

private static void SaveColumn(List<object?> row, StringBuilder colBuilder) {
private static void SaveColumn(List<object?> row, StringBuilder colBuilder)
{
row.Add(colBuilder.ToString());
colBuilder.Clear();
}

private static void SaveRow(List<IList<object?>> result, ref List<object?> row)
private static void SaveRow(List<IList<object?>> result, ref List<object?> row)
{
result.Add(row);
row = new List<object?>();
Expand Down
5 changes: 1 addition & 4 deletions Excely/TableFactories/DictionaryListTableFactory.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
using System.Formats.Asn1;
using System.Reflection;

namespace Excely.TableFactories
namespace Excely.TableFactories
{
/// <summary>
/// 提供以字典 Key 為欄位,將字典集合傾印至表格的功能。
Expand Down

0 comments on commit 2f78379

Please sign in to comment.