Skip to content

Commit 2f0f977

Browse files
Cleaned up the fix for issue 773
Reimplemented the refactoring that was reverted to fix issue #773 correcting the bug that were introduced the previous time.
2 parents 38eb60c + 8c21989 commit 2f0f977

File tree

3 files changed

+25
-42
lines changed

3 files changed

+25
-42
lines changed

src/MiniExcel/SaveByTemplate/ExcelOpenXmlTemplate.Impl.cs

Lines changed: 13 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -69,11 +69,11 @@ public XMergeCell(XmlElement mergeCell)
6969

7070
//TODO: width,height
7171
var xy1 = refs[0];
72-
X1 = ColumnHelper.GetColumnIndex(StringHelper.GetLetter(refs[0]));
72+
X1 = ColumnHelper.GetColumnIndex(StringHelper.GetLetters(refs[0]));
7373
Y1 = StringHelper.GetNumber(xy1);
7474

7575
var xy2 = refs[1];
76-
X2 = ColumnHelper.GetColumnIndex(StringHelper.GetLetter(refs[1]));
76+
X2 = ColumnHelper.GetColumnIndex(StringHelper.GetLetters(refs[1]));
7777
Y2 = StringHelper.GetNumber(xy2);
7878

7979
Width = Math.Abs(X1 - X2) + 1;
@@ -718,15 +718,13 @@ private void GenerateCellValues(string endPrefix, StreamWriter writer, ref int r
718718
// note: only first time need add diff https://user-images.githubusercontent.com/12729184/114494728-6bceda80-9c4f-11eb-9685-8b5ed054eabe.png
719719
if (!isFirst)
720720
rowIndexDiff += rowInfo.IEnumerableMercell?.Height ?? 1; //TODO:base on the merge size
721+
721722
if (isFirst)
722723
{
723724
// https://github.com/mini-software/MiniExcel/issues/771 Saving by template introduces unintended value replication in each row #771
724725
cleanInnerXml = cleanNotFirstRowInnerXmlElement;
725-
726-
727726
isFirst = false;
728727
}
729-
730728

731729
var mergeBaseRowIndex = newRowIndex;
732730
newRowIndex += rowInfo.IEnumerableMercell?.Height ?? 1;
@@ -795,7 +793,7 @@ private static void MergeCells(List<XRowInfo> xRowInfos)
795793
return new XChildNode
796794
{
797795
InnerText = s.InnerText,
798-
ColIndex = StringHelper.GetLetter(att),
796+
ColIndex = StringHelper.GetLetters(att),
799797
RowIndex = StringHelper.GetNumber(att)
800798
};
801799
})
@@ -837,7 +835,7 @@ private static void MergeCells(List<XRowInfo> xRowInfos)
837835
foreach (var childNode in childNodes)
838836
{
839837
var att = childNode.GetAttribute("r");
840-
var childNodeLetter = StringHelper.GetLetter(att);
838+
var childNodeLetter = StringHelper.GetLetters(att);
841839
var childNodeNumber = StringHelper.GetNumber(att);
842840

843841
if (!string.IsNullOrEmpty(childNode.InnerText))
@@ -952,8 +950,8 @@ private static string ConvertToDateTimeString(PropertyInfo propInfo, object cell
952950
{
953951
//TODO:c.SetAttribute("t", "d"); and custom format
954952
var format = propInfo?.GetAttributeValue((ExcelFormatAttribute x) => x.Format)
955-
?? propInfo?.GetAttributeValue((ExcelColumnAttribute x) => x.Format)
956-
?? "yyyy-MM-dd HH:mm:ss";
953+
?? propInfo?.GetAttributeValue((ExcelColumnAttribute x) => x.Format)
954+
?? "yyyy-MM-dd HH:mm:ss";
957955

958956
return (cellValue as DateTime?)?.ToString(format);
959957
}
@@ -962,8 +960,7 @@ private static string ConvertToDateTimeString(PropertyInfo propInfo, object cell
962960
private static string CleanXml(string xml, string endPrefix) => CleanXml(new StringBuilder(xml), endPrefix).ToString();
963961
private static StringBuilder CleanXml(StringBuilder xml, string endPrefix) => xml
964962
.Replace("xmlns:x14ac=\"http://schemas.microsoft.com/office/spreadsheetml/2009/9/ac\"", "")
965-
.Replace($"xmlns{endPrefix}=\"http://schemas.openxmlformats.org/spreadsheetml/2006/main\"", "")
966-
;
963+
.Replace($"xmlns{endPrefix}=\"http://schemas.openxmlformats.org/spreadsheetml/2006/main\"", "");
967964

968965
private static void ReplaceSharedStringsToStr(IDictionary<int, string> sharedStrings, XmlNodeList rows)
969966
{
@@ -1028,7 +1025,7 @@ private void UpdateDimensionAndGetRowsInfo(IDictionary<string, object> inputMaps
10281025

10291026
if (changeRowIndex)
10301027
{
1031-
c.SetAttribute("r", $"{StringHelper.GetLetter(r)}{{{{$rowindex}}}}");
1028+
c.SetAttribute("r", $"{StringHelper.GetLetters(r)}{{{{$rowindex}}}}");
10321029
}
10331030

10341031
var v = c.SelectSingleNode("x:v", _ns);
@@ -1256,20 +1253,15 @@ private void UpdateDimensionAndGetRowsInfo(IDictionary<string, object> inputMaps
12561253
var refs = dimension.GetAttribute("ref").Split(':');
12571254
if (refs.Length == 2)
12581255
{
1259-
//var letter = StringHelper.GetLetter(refs[1]);
1260-
//var digit = StringHelper.GetDigit(refs[1]);
1261-
var letter = new String(refs[1].Where(Char.IsLetter).ToArray());
1262-
var digit = int.Parse(new String(refs[1].Where(Char.IsDigit).ToArray()));
1256+
var letter = StringHelper.GetLetters(refs[1]);
1257+
var digit = StringHelper.GetNumber(refs[1]);
12631258

12641259
dimension.SetAttribute("ref", $"{refs[0]}:{letter}{digit + maxRowIndexDiff}");
12651260
}
12661261
else
12671262
{
1268-
//var letter = StringHelper.GetLetter(refs[0]);
1269-
//var digit = StringHelper.GetDigit(refs[0]);
1270-
1271-
var letter = new String(refs[0].Where(Char.IsLetter).ToArray());
1272-
var digit = int.Parse(new String(refs[0].Where(Char.IsDigit).ToArray()));
1263+
var letter = StringHelper.GetLetters(refs[0]);
1264+
var digit = StringHelper.GetNumber(refs[0]);
12731265

12741266
dimension.SetAttribute("ref", $"A1:{letter}{digit + maxRowIndexDiff}");
12751267
}

src/MiniExcel/Utils/StringHelper.cs

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,19 +9,9 @@ namespace MiniExcelLibs.Utils
99
internal static class StringHelper
1010
{
1111
private static readonly string[] _ns = { Config.SpreadsheetmlXmlns, Config.SpreadsheetmlXmlStrictns };
12-
//public static string GetLetter(string content) => content.FirstOrDefault(char.IsLetter).ToString();
13-
public static int GetDigit(string content) => content.FirstOrDefault(char.IsDigit) - '0';
14-
//public static int GetNumber(string content) => int.Parse(new string(content.Where(char.IsNumber).ToArray()));
15-
public static string GetLetter(string content)
16-
{
17-
//TODO:need to chekc
18-
return new String(content.Where(Char.IsLetter).ToArray());
19-
}
2012

21-
public static int GetNumber(string content)
22-
{
23-
return int.Parse(new String(content.Where(Char.IsNumber).ToArray()));
24-
}
13+
public static string GetLetters(string content) => new string(content.Where(char.IsLetter).ToArray());
14+
public static int GetNumber(string content) => int.Parse(new string(content.Where(char.IsNumber).ToArray()));
2515

2616
/// <summary>
2717
/// Copied and modified from ExcelDataReader - @MIT License

tests/MiniExcelTests/MiniExcelIssueTests.cs

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4255,18 +4255,19 @@ public void TestIssue772()
42554255
public void TestIssue773()
42564256
{
42574257
var templatePath = PathHelper.GetFile("xlsx/TestIssue773_Template.xlsx");
4258-
List<dynamic> a = new List<dynamic>{
4259-
new { Id = 1, Name = "Bill", A = "a1", B = "b1", C = "c1", D = "d1", E = "e1", F = "f1", G = "g1", H = "H1" },
4260-
new { Id = 2, Name = "Steve", A = "a2", B = "b2", C = "c2", D = "d2", E = "e2", F = "f2", G = "g2", H = "H2" },
4261-
new { Id = 3, Name = "Ram", A = "a3", B = "b3", C = "c3", D = "d3", E = "e3", F = "f3", G = "g3", H = "H3" },
4262-
};
4258+
List<dynamic> a =
4259+
[
4260+
new { Id = 1, Name = "Bill", A = "a1", B = "b1", C = "c1", D = "d1", E = "e1", F = "f1", G = "g1", H = "H1" },
4261+
new { Id = 2, Name = "Steve", A = "a2", B = "b2", C = "c2", D = "d2", E = "e2", F = "f2", G = "g2", H = "H2" },
4262+
new { Id = 3, Name = "Ram", A = "a3", B = "b3", C = "c3", D = "d3", E = "e3", F = "f3", G = "g3", H = "H3" }
4263+
];
42634264

42644265
var fill = new { t = a };
4265-
var path = Path.GetTempPath() + Guid.NewGuid() + ".xlsx";
4266-
Console.WriteLine(path);
4267-
MiniExcel.SaveAsByTemplate(path, templatePath, fill);
4266+
using var path = AutoDeletingPath.Create();
42684267

4269-
var rows = MiniExcel.Query(path, false).ToList();
4268+
MiniExcel.SaveAsByTemplate(path.FilePath, templatePath, fill);
4269+
var rows = MiniExcel.Query(path.FilePath).ToList();
4270+
42704271
Assert.Equal("H1", rows[4].AF);
42714272
Assert.Equal("c3", rows[6].AA);
42724273
Assert.Equal("Ram", rows[6].B);

0 commit comments

Comments
 (0)