diff --git a/README.md b/README.md index 56ebdabb3910da0cd8b781f802db2e52ed25c6e1..d83cdde6aff8202ceafc5ebfe961e6d16aec8707 100644 --- a/README.md +++ b/README.md @@ -227,18 +227,13 @@ MiniWord.SaveAsByTemplate(path, templatePath, value); ![before_foreach](https://user-images.githubusercontent.com/38832863/220123955-063c9345-3998-4fd7-982c-8d1e3b48bbf8.PNG) -Screenshot 2023-08-08 at 17 59 37 - ##### Result ![after_foreach](https://user-images.githubusercontent.com/38832863/220123960-913a7140-2fa2-415e-bb3e-456e04167382.PNG) -Screenshot 2023-08-08 at 18 00 15 - ### If statement inside template -For multip paragraph, use @if and @endif tags. -For single paragraph and inside foreach, use `{{if` and `endif}}` tags to template is required. +Adding `@if` and `@endif` tags to template is required. ##### Example @@ -264,21 +259,14 @@ var value = new Dictionary() MiniWord.SaveAsByTemplate(path, templatePath, value); ``` -##### Template For Multi Paragraph +##### Template ![before_if](https://user-images.githubusercontent.com/38832863/220125429-7dd6ce94-35c6-478e-8903-064f9cf9361a.PNG) -##### Result Of Multi Paragraph +##### Result ![after_if](https://user-images.githubusercontent.com/38832863/220125435-72ea24b4-2412-45de-961a-ad4b2134417b.PNG) -##### Template For Single Paragraph - -Screenshot 2023-08-08 at 17 55 46 - -##### Result Of Single Paragraph - -Screenshot 2023-08-08 at 17 56 47 ### ColorText diff --git a/release-note/README.md b/release-note/README.md index d5f39fcaf823662338e0c0401c5edacf4ff8b9ee..6507bf48749255af2c1e05105fb573528d57c37e 100644 --- a/release-note/README.md +++ b/release-note/README.md @@ -22,13 +22,6 @@ --- -### 0.8.0 - -- [New] Support new OpenXml to solve the problem of line wrapping to multiple lines #68 (via @ping9719) - -- [New] Support if statement inside foreach statement inside templates. Please refer to samples. (via @eynarhaji) -- [New] Change tags for if statements for single paragraph if statement {{if and endif}} inside templates. Please refer to samples. (via @eynarhaji) -- [Bug] The table should be inserted at the template tag position instead of the last row #47 (via @itldg) ### 0.7.0 - [New] Add support to List inside List via `IEnumerable` and `{{foreach`/`endforeach}}` tags (via @eynarhaji) diff --git a/release-note/README.zh-CN.md b/release-note/README.zh-CN.md index e45d4d8e97410344c7b9b2d8288b4f0b0b351574..68e6dac79a0f8a0c14297fe387a3751658b3e83c 100644 --- a/release-note/README.zh-CN.md +++ b/release-note/README.zh-CN.md @@ -22,13 +22,6 @@ --- -### 0.8.0 - -- [New] 支持 new OpenXml to solve the problem of line wrapping to multiple lines #68 (via @ping9719) -- [New] 支持 to if statement inside foreach statement inside templates. Please refer to samples. (via @eynarhaji) -- [New] 变更 tags for if statements for single paragraph if statement {{if and endif}} inside templates. Please refer to samples. (via @eynarhaji) -- [Bug] The table should be inserted at the template tag position instead of the last row #47 (via @itldg) - ### 0.7.0 - [New] 支持 List inside List via `IEnumerable` and `{{foreach`/`endforeach}}` tags (via @eynarhaji) - [New] 支持 @if statements inside templates (via @eynarhaji) diff --git a/release-note/README.zh-Hant.md b/release-note/README.zh-Hant.md index effddd21d59d7afde5b61222208bfe6e1117f877..d244d1ea54e47f89080fc8641d49e06eea0e9deb 100644 --- a/release-note/README.zh-Hant.md +++ b/release-note/README.zh-Hant.md @@ -22,13 +22,6 @@ --- -### 0.8.0 - -- [New] 支持 new OpenXml to solve the problem of line wrapping to multiple lines #68 (via @ping9719) -- [New] 支持 to if statement inside foreach statement inside templates. Please refer to samples. (via @eynarhaji) -- [New] 变更 tags for if statements for single paragraph if statement {{if and endif}} inside templates. Please refer to samples. (via @eynarhaji) -- [Bug] The table should be inserted at the template tag position instead of the last row #47 (via @itldg) - ### 0.7.0 - [New] 支持 List inside List via `IEnumerable` and `{{foreach`/`endforeach}}` tags (via @eynarhaji) - [New] 支持 @if statements inside templates (via @eynarhaji) diff --git a/samples/docx/TestForeachInTablesWithIfStatementDemo.docx b/samples/docx/TestForeachInTablesWithIfStatementDemo.docx deleted file mode 100644 index 168c5cbaa4bfdcd60f4b25032c54ddbba609aff1..0000000000000000000000000000000000000000 Binary files a/samples/docx/TestForeachInTablesWithIfStatementDemo.docx and /dev/null differ diff --git a/samples/docx/TestIfStatement.docx b/samples/docx/TestIfStatement.docx index 23fab8a9ff405c2275a7c20935d8602c2faf0ec3..8e39d67afceeb07d19e336f1bd805fa1771d1e15 100644 Binary files a/samples/docx/TestIfStatement.docx and b/samples/docx/TestIfStatement.docx differ diff --git a/samples/docx/TestIssue47.docx b/samples/docx/TestIssue47.docx deleted file mode 100644 index ec5b5e2da32426fbe1244572b035c11c31d5de6f..0000000000000000000000000000000000000000 Binary files a/samples/docx/TestIssue47.docx and /dev/null differ diff --git a/src/MiniWord/MiniWord.Implment.cs b/src/MiniWord/MiniWord.Implment.cs index 3234ebaa6c3cac0f183b2b2400e8a86f00bce4df..48fa3112609876efcf354d49c76a4669db3a9b3f 100644 --- a/src/MiniWord/MiniWord.Implment.cs +++ b/src/MiniWord/MiniWord.Implment.cs @@ -1,13 +1,14 @@ -namespace MiniSoftware +namespace MiniSoftware { using DocumentFormat.OpenXml; using DocumentFormat.OpenXml.Packaging; using DocumentFormat.OpenXml.Wordprocessing; - using Extensions; - using Utility; + using MiniSoftware.Extensions; + using MiniSoftware.Utility; using System; using System.Collections; using System.Collections.Generic; + using System.Dynamic; using System.IO; using System.Linq; using System.Text; @@ -28,12 +29,10 @@ private static void SaveAsByTemplateImpl(Stream stream, byte[] template, Diction ms.Position = 0; using (var docx = WordprocessingDocument.Open(ms, true)) { - var hc = docx.MainDocumentPart.HeaderParts.Count(); - var fc = docx.MainDocumentPart.FooterParts.Count(); - for (int i = 0; i < hc; i++) - docx.MainDocumentPart.HeaderParts.ElementAt(i).Header.Generate(docx, value); - for (int i = 0; i < fc; i++) - docx.MainDocumentPart.FooterParts.ElementAt(i).Footer.Generate(docx, value); + foreach (var hdr in docx.MainDocumentPart.HeaderParts) + hdr.Header.Generate(docx, value); + foreach (var ftr in docx.MainDocumentPart.FooterParts) + ftr.Footer.Generate(docx, value); docx.MainDocumentPart.Document.Body.Generate(docx, value); docx.Save(); } @@ -57,9 +56,7 @@ private static void Generate(this OpenXmlElement xmlElement, WordprocessingDocum foreach (var tr in trs) { - var innerText = tr.InnerText.Replace("{{foreach", "").Replace("endforeach}}", "") - .Replace("{{if(", "").Replace(")if", "").Replace("endif}}", ""); - var matchs = (Regex.Matches(innerText, "(?<={{).*?\\..*?(?=}})") + var matchs = (Regex.Matches(tr.InnerText, "(?<={{).*?\\..*?(?=}})") .Cast().GroupBy(x => x.Value).Select(varGroup => varGroup.First().Value)).ToArray(); if (matchs.Length > 0) { @@ -85,19 +82,10 @@ private static void Generate(this OpenXmlElement xmlElement, WordprocessingDocum dic.Add(dicKey, e.Value); } - ReplaceStatements(newTr, tags: dic); + ReplaceStatements(xmlElement, tags); ReplaceText(newTr, docx, tags: dic); - //Fix #47 The table should be inserted at the template tag position instead of the last row - if (table.Contains(tr)) - { - table.InsertBefore(newTr, tr); - } - else - { - // If it is a nested table, temporarily append it to the end according to the original plan. - table.Append(newTr); - } + table.Append(newTr); } tr.Remove(); } @@ -107,7 +95,7 @@ private static void Generate(this OpenXmlElement xmlElement, WordprocessingDocum } ReplaceStatements(xmlElement, tags); - + ReplaceText(xmlElement, docx, tags); } @@ -117,6 +105,7 @@ private static void AvoidSplitTagText(OpenXmlElement xmlElement) var pool = new List(); var sb = new StringBuilder(); var needAppend = false; + var foreachIncluded = false; foreach (var text in texts) { var clear = false; @@ -133,21 +122,28 @@ private static void AvoidSplitTagText(OpenXmlElement xmlElement) // TODO: check tag exist // TODO: record tag text if without tag then system need to clear them // TODO: every {{tag}} one for them and add text before first text and copy first one and remove {{, tagname, }} - - const string foreachTag = "{{foreach"; - const string endForeachTag = "endforeach}}"; - const string ifTag = "{{if"; - const string endifTag = "endif}}"; - const string tagStart = "{{"; - const string tagEnd = "}}"; - - var foreachTagContains = s.Split(new []{foreachTag}, StringSplitOptions.None).Length - 1 == - s.Split(new []{endForeachTag}, StringSplitOptions.None).Length - 1; - var ifTagContains = s.Split(new []{ifTag}, StringSplitOptions.None).Length - 1 == - s.Split(new []{endifTag}, StringSplitOptions.None).Length - 1; - var tagContains = s.StartsWith(tagStart) && s.Contains(tagEnd); - - if (foreachTagContains && ifTagContains && tagContains) + + if(s.StartsWith("{{foreach")) + foreachIncluded = true; + + if (!s.StartsWith("{{")) + clear = true; + else if (s.Contains("{{") && s.Contains("}}") && !foreachIncluded) + { + if (sb.Length <= 1000) // avoid too big tag + { + var first = pool.First(); + var newText = first.Clone() as Text; + newText.Text = s; + first.Parent.InsertBefore(newText, first); + foreach (var t in pool) + { + t.Text = ""; + } + } + clear = true; + } + else if (s.Contains("{{foreach") && s.Contains("endforeach}}") && foreachIncluded) { if (sb.Length <= 1000) // avoid too big tag { @@ -161,7 +157,9 @@ private static void AvoidSplitTagText(OpenXmlElement xmlElement) } } clear = true; + foreachIncluded = false; } + } if (clear) @@ -214,34 +212,60 @@ private static List GetReplaceKeys(Dictionary tags) return keys; } - private static bool EvaluateStatement(string tagValue, string comparisonOperator, string value) + private static void ReplaceStatements(OpenXmlElement xmlElement, Dictionary tags) { - var checkStatement = false; + var paragraphs = xmlElement.Descendants().ToList(); + + while (paragraphs.Any(s => s.InnerText.Contains("@if"))) + { + var ifIndex = paragraphs.FindIndex(0, s => s.InnerText.Contains("@if")); + var endIfFinalIndex = paragraphs.FindIndex(ifIndex, s => s.InnerText.Contains("@endif")); + + var statement = paragraphs[ifIndex].InnerText.Split(' '); + + var checkStatement = EvaluateStatement(tags[statement[1]], statement[2], statement[3]); + + if (checkStatement) + { + for (int i = ifIndex+1; i <= endIfFinalIndex-1; i++) + { + paragraphs[i].Remove(); + } + } + + paragraphs[ifIndex].Remove(); + paragraphs[endIfFinalIndex].Remove(); + + paragraphs = xmlElement.Descendants().ToList(); + } + } - var tagValueEvaluation = EvaluateValue(tagValue); + private static bool EvaluateStatement(object tagValue, string comparisonOperator, string value) + { + var checkStatement = false; - switch (tagValueEvaluation) + switch (tagValue) { case double dtg when double.TryParse(value, out var doubleNumber): switch (comparisonOperator) { case "==": - checkStatement = dtg.Equals(doubleNumber); + checkStatement = !dtg.Equals(doubleNumber); break; case "!=": - checkStatement = !dtg.Equals(doubleNumber); + checkStatement = dtg.Equals(doubleNumber); break; case ">": - checkStatement = dtg > doubleNumber; + checkStatement = dtg <= doubleNumber; break; case "<": - checkStatement = dtg < doubleNumber; + checkStatement = dtg >= doubleNumber; break; case ">=": - checkStatement = dtg >= doubleNumber; + checkStatement = dtg < doubleNumber; break; case "<=": - checkStatement = dtg <= doubleNumber; + checkStatement = dtg > doubleNumber; break; } @@ -250,22 +274,22 @@ private static bool EvaluateStatement(string tagValue, string comparisonOperator switch (comparisonOperator) { case "==": - checkStatement = itg.Equals(intNumber); + checkStatement = !itg.Equals(intNumber); break; case "!=": - checkStatement = !itg.Equals(intNumber); + checkStatement = itg.Equals(intNumber); break; case ">": - checkStatement = itg > intNumber; + checkStatement = itg <= intNumber; break; case "<": - checkStatement = itg < intNumber; + checkStatement = itg >= intNumber; break; case ">=": - checkStatement = itg >= intNumber; + checkStatement = itg < intNumber; break; case "<=": - checkStatement = itg <= intNumber; + checkStatement = itg > intNumber; break; } @@ -274,22 +298,22 @@ private static bool EvaluateStatement(string tagValue, string comparisonOperator switch (comparisonOperator) { case "==": - checkStatement = dttg.Equals(date); + checkStatement = !dttg.Equals(date); break; case "!=": - checkStatement = !dttg.Equals(date); + checkStatement = dttg.Equals(date); break; case ">": - checkStatement = dttg > date; + checkStatement = dttg <= date; break; case "<": - checkStatement = dttg < date; + checkStatement = dttg >= date; break; case ">=": - checkStatement = dttg >= date; + checkStatement = dttg < date; break; case "<=": - checkStatement = dttg <= date; + checkStatement = dttg > date; break; } @@ -298,42 +322,19 @@ private static bool EvaluateStatement(string tagValue, string comparisonOperator switch (comparisonOperator) { case "==": - checkStatement = stg == value; + checkStatement = stg != value; break; case "!=": - checkStatement = stg != value; + checkStatement = stg == value; break; } - break; - case bool btg when bool.TryParse(value, out var boolean): - switch (comparisonOperator) - { - case "==": - checkStatement = btg != boolean; - break; - case "!=": - checkStatement = btg == boolean; - break; - } - break; - } + break; + } return checkStatement; } - private static object EvaluateValue(string value) - { - if (double.TryParse(value, out var doubleNumber)) - return doubleNumber; - else if (int.TryParse(value, out var intNumber)) - return intNumber; - else if (DateTime.TryParse(value, out var date)) - return date; - - return value; - } - private static void ReplaceText(OpenXmlElement xmlElement, WordprocessingDocument docx, Dictionary tags) { var paragraphs = xmlElement.Descendants().ToArray(); @@ -363,7 +364,7 @@ private static void ReplaceText(OpenXmlElement xmlElement, WordprocessingDocumen isMatch = true; } } - + if (isMatch) { if (tag.Value is string[] || tag.Value is IList || tag.Value is List) @@ -379,7 +380,6 @@ private static void ReplaceText(OpenXmlElement xmlElement, WordprocessingDocumen isFirst = false; else run.Append(new Break()); - newT.Text = EvaluateIfStatement(newT.Text); run.Append(newT); currentT = newT; } @@ -390,8 +390,6 @@ private static void ReplaceText(OpenXmlElement xmlElement, WordprocessingDocumen var currentT = t; var generatedText = new Text(); currentT.Text = currentT.Text.Replace(@"{{foreach", "").Replace(@"endforeach}}", ""); - - var newTexts = new Dictionary(); for (var i = 0; i < vs.Count; i++) { var newT = t.CloneNode(true) as Text; @@ -400,22 +398,13 @@ private static void ReplaceText(OpenXmlElement xmlElement, WordprocessingDocumen { newT.Text = newT.Text.Replace("{{" + tag.Key + "." + vv.Key + "}}", vv.Value.ToString()); } - - newT.Text = EvaluateIfStatement(newT.Text); - - if(!string.IsNullOrEmpty(newT.Text)) - newTexts.Add(i, newT.Text); - } - for (var i = 0; i < newTexts.Count; i++) - { - var dict = newTexts.ElementAt(i); - generatedText.Text += dict.Value; - - if (i != newTexts.Count - 1) + if (i != vs.Count) { - generatedText.Text += vs[dict.Key].Separator; + newT.Text += vs[i].Separator; } + + generatedText.Text += newT.Text; } run.Append(generatedText); @@ -469,21 +458,35 @@ private static void ReplaceText(OpenXmlElement xmlElement, WordprocessingDocumen { newText = tag.Value?.ToString(); } - + if (newText.IsNotBlank()) + { + var vs = newText.Split(new char[] { '\r', '\n' }); + var currentT = t; + var isFirst = true; + foreach (var v in vs) + { + var newT = t.CloneNode(true) as Text; + newT.Text = t.Text.Replace($"{{{{{tag.Key}}}}}", v?.ToString()); + if (isFirst) + isFirst = false; + else + run.Append(new Break()); + run.Append(newT); + currentT = newT; + } + } t.Text = t.Text.Replace($"{{{{{tag.Key}}}}}", newText); } } } - t.Text = EvaluateIfStatement(t.Text); - // add breakline { var newText = t.Text; - var splits = Regex.Split(newText, "(<[a-zA-Z/].*?>|\n|\r\n)").Where(o => o != "\n" && o != "\r\n"); + var splits = Regex.Split(newText, "(<[a-zA-Z/].*?>|\n)"); var currentT = t; var isFirst = true; - if (splits.Count() > 1) + if (splits.Length > 1) { foreach (var v in splits) { @@ -503,67 +506,6 @@ private static void ReplaceText(OpenXmlElement xmlElement, WordprocessingDocumen } } } - - private static void ReplaceStatements(OpenXmlElement xmlElement, Dictionary tags) - { - var paragraphs = xmlElement.Descendants().ToList(); - - while (paragraphs.Any(s => s.InnerText.Contains("@if"))) - { - var ifIndex = paragraphs.FindIndex(0, s => s.InnerText.Contains("@if")); - var endIfFinalIndex = paragraphs.FindIndex(ifIndex, s => s.InnerText.Contains("@endif")); - - var statement = paragraphs[ifIndex].InnerText.Split(' '); - - var tagValue = tags[statement[1]] ?? "NULL"; - - var checkStatement = statement.Length == 4 ? EvaluateStatement(tagValue.ToString(), statement[2], statement[3]) : !bool.Parse(tagValue.ToString()); - - if (!checkStatement) - { - for (int i = ifIndex + 1; i <= endIfFinalIndex - 1; i++) - { - paragraphs[i].Remove(); - } - } - - paragraphs[ifIndex].Remove(); - paragraphs[endIfFinalIndex].Remove(); - - paragraphs = xmlElement.Descendants().ToList(); - } - } - - private static string EvaluateIfStatement(string text) - { - const string ifStartTag = "{{if("; - const string ifEndTag = ")if"; - const string endIfTag = "endif}}"; - - while (text.Contains(ifStartTag)) - { - var ifIndex = text.IndexOf(ifStartTag, StringComparison.Ordinal); - var ifEndIndex = text.IndexOf(")if", ifIndex, StringComparison.Ordinal); - - var statement = text.Substring(ifIndex + ifStartTag.Length, ifEndIndex - (ifIndex + ifStartTag.Length)).Split(','); - - var checkStatement = EvaluateStatement(statement[0], statement[1], statement[2]); - - if (checkStatement) - { - text = text.Remove(ifIndex, ifEndIndex - ifIndex + ifEndTag.Length); - var endIfFinalIndex = text.IndexOf(endIfTag, StringComparison.Ordinal); - text = text.Remove(endIfFinalIndex, endIfTag.Length); - } - else - { - var endIfFinalIndex = text.IndexOf(endIfTag, StringComparison.Ordinal); - text = text.Remove(ifIndex, endIfFinalIndex - ifIndex + endIfTag.Length); - } - } - - return text; - } private static bool IsHyperLink(object value) { @@ -611,6 +553,7 @@ private static Hyperlink GetHyperLink(MainDocumentPart mainPart, MiniWordHyperLi } private static RunProperties AddColorText(MiniWordColorText[] miniWordColorTextArray) { + RunProperties runPro = new RunProperties(); foreach (var miniWordColorText in miniWordColorTextArray) { @@ -661,7 +604,7 @@ private static void AddPicture(OpenXmlElement appendElement, string relationship new A.BlipExtension() { Uri = - $"{{{Guid.NewGuid().ToString("n")}}}" + $"{{{ Guid.NewGuid().ToString("n")}}}" }) ) { diff --git a/src/MiniWord/MiniWord.csproj b/src/MiniWord/MiniWord.csproj index 53ce9fc9823a244c274d1f487d006451e2e6609f..042d9386d250b8baacd06b5517ec1b91beff14c8 100644 --- a/src/MiniWord/MiniWord.csproj +++ b/src/MiniWord/MiniWord.csproj @@ -1,7 +1,7 @@  - net45;netstandard2.0; - 0.8.0 + net45;netstandard2.0;net5.0 + 0.7.0 MiniWord @@ -10,9 +10,9 @@ word;template;micro-helper;mini;openxml;helper; .NET Word(docx) exporting template engine without COM+ and interop (support Linux and Mac) - mini-software, Wei Lin, eynarhaji + LIN,WEI-HAN, eynarhaji MiniWord - mini-software, Wei Lin, 2022 onwards + LIN,WEI-HAN, 2022 onwards en https://raw.githubusercontent.com/mini-software/MiniWord/main/LICENSE MiniSoftware @@ -32,6 +32,6 @@ - + diff --git a/src/MiniWord/MiniWordPicture.cs b/src/MiniWord/MiniWordPicture.cs index 8dab97c027c2e3a6a7e7ba048e18d0007ffd3ca5..38f8b9499ec1b8ade1c80ab366952adf258ba2ac 100644 --- a/src/MiniWord/MiniWordPicture.cs +++ b/src/MiniWord/MiniWordPicture.cs @@ -24,7 +24,7 @@ public string Extension } set { _extension = value; } } - internal PartTypeInfo GetImagePartType + internal ImagePartType GetImagePartType { get { diff --git a/tests/MiniWordTests/IssueTests.cs b/tests/MiniWordTests/IssueTests.cs index 64f0935570c76393a0a92adde49c340d43ca1818..4223536afc5e77b9d2afeddc9ebfa3f293a5ece7 100644 --- a/tests/MiniWordTests/IssueTests.cs +++ b/tests/MiniWordTests/IssueTests.cs @@ -9,46 +9,6 @@ namespace MiniWordTests { public class IssueTests { - [Fact] - public void TestIssue69() - { - { - var path = PathHelper.GetTempFilePath(); - var templatePath = PathHelper.GetFile("TestIssue47.docx"); - var value = new Dictionary() - { - { "line",new List> - { - new Dictionary - { - {"num", "1"}, - {"spc", "Specification1"}, - {"desc", "Description1"}, - {"qty", "10"}, - {"up", "20.00"}, - {"tax_rate", "0.20"}, - {"tax_amount", "4.00"} - }, - new Dictionary - { - {"num", "2"}, - {"spc", "Specification2"}, - {"desc", "Description2"}, - {"qty", "5"}, - {"up", "30.00"}, - {"tax_rate", "0.15"}, - {"tax_amount", "2.25"} - } - } - } - }; - MiniWord.SaveAsByTemplate(path, templatePath, value); - // TODO: waiting solution - //var xml = Helpers.GetZipFileContent(path, "word/document.xml"); - //Assert.Contains(@"MiniWord", xml); - } - } - [Fact] public void TestIssueAvoidSplitTag() { @@ -189,7 +149,7 @@ public void TestIssue18() new User (){ name="Jack",department="HR"}, new User (){ name="Loan",department="IT"}, }, - employees = new List() + employees = new List() { new User (){ name="Jack",department="HR"}, new User (){ name="Loan",department="HR"}, @@ -233,7 +193,7 @@ public void TestIssue17_new() { var path = PathHelper.GetTempFilePath(); var templatePath = PathHelper.GetFile("TestIssue17.docx"); - var value = new + var value = new { Content = "Test", Content2 = "Test2", @@ -290,7 +250,7 @@ public void TestIssue13_new() { var path = PathHelper.GetTempFilePath(); var templatePath = PathHelper.GetFile("TestExpenseDemo.docx"); - var value = new + var value = new { TripHs = new List> { @@ -383,7 +343,7 @@ public void TestIssue12() [Fact] public void TestIssue12_dynamic() { - + var path = PathHelper.GetTempFilePath(); var templatePath = PathHelper.GetFile("TestBasicFill.docx"); dynamic value = new ExpandoObject(); @@ -393,7 +353,7 @@ public void TestIssue12_dynamic() value.VIP = true; value.Points = 123; value.APP = "Demo APP\n"; - + MiniWord.SaveAsByTemplate(path, templatePath, value); var xml = Helpers.GetZipFileContent(path, "word/document.xml"); Assert.Contains(@"MiniSofteware", xml); @@ -409,8 +369,7 @@ public void TestIssue12_new() var path = PathHelper.GetTempFilePath(); var templatePath = PathHelper.GetFile("TestBasicFill.docx"); - object value = new - { + object value = new { Company_Name = "MiniSofteware\n", Name = "Jack", CreateDate = new DateTime(2021, 01, 01), @@ -475,7 +434,7 @@ Lorem Ipsum has been the industry's standard dummy text [Fact] public void TestIssueDemo03_dynamic() { - + var path = PathHelper.GetTempFilePath(); var templatePath = PathHelper.GetFile("TestDemo02.docx"); dynamic value = new ExpandoObject(); @@ -612,8 +571,7 @@ public void TestIssue5() var templatePath = PathHelper.GetFile("TestBasicFill.docx"); var value = new Dictionary() { - ["Name"] = new MiniWordHyperLink() - { + ["Name"] = new MiniWordHyperLink(){ Url = "https://google.com", Text = "測試連結!!" }, @@ -711,7 +669,7 @@ public void MiniWordHyperLink_AnonymousArray() var templatePath = PathHelper.GetFile("TestBasicFill.docx"); var value = new Dictionary() { - ["Name"] = new[]{ + ["Name"] = new []{ new MiniWordHyperLink(){ Url = "https://google.com", Text = "測試連結22!!" @@ -748,10 +706,9 @@ public void TestIssue5_new() { var path = PathHelper.GetTempFilePath(); var templatePath = PathHelper.GetFile("TestBasicFill.docx"); - var value = new + var value = new { - Name = new MiniWordHyperLink() - { + Name =new MiniWordHyperLink(){ Url = "https://google.com", Text = "測試連結!!" }, @@ -798,7 +755,7 @@ public void TestIssue4_new() { var path = PathHelper.GetTempFilePath(); var templatePath = PathHelper.GetFile("TestBasicFill.docx"); - var value = new + var value = new { Company_Name = "MiniSofteware", Name = "Jack", @@ -818,13 +775,8 @@ public void TestColor() { Company_Name = new MiniWordColorText { Text = "MiniSofteware", FontColor = "#eb70AB" }, Name = new MiniWordColorText { Text = "Jack", HighlightColor = "#eb70AB" }, - CreateDate = new MiniWordColorText - { - Text = new DateTime(2021, 01, 01).ToString() - , - HighlightColor = "#eb70AB", - FontColor = "#ffffff" - }, + CreateDate = new MiniWordColorText { Text = new DateTime(2021, 01, 01).ToString() + , HighlightColor = "#eb70AB", FontColor = "#ffffff" }, VIP = true, Points = 123, APP = "Demo APP", diff --git a/tests/MiniWordTests/MiniWordTests.cs b/tests/MiniWordTests/MiniWordTests.cs index 78cf72dc367ffa4a032e6d3791df92a130a5aebf..fa58d7a8ddb2bda57c1bae8943fece12b65ff6d6 100644 --- a/tests/MiniWordTests/MiniWordTests.cs +++ b/tests/MiniWordTests/MiniWordTests.cs @@ -118,8 +118,6 @@ public void TestForeachLoopInTables() var xml = Helpers.GetZipFileContent(path, "word/document.xml"); Assert.Contains(@"Discussion requirement part2 and development", xml); Assert.Contains(@"Discussion requirement part1", xml); - Assert.Contains( - "Air way to the Airplane | Parking way to the Car / Hotel way to the Room, Food way to the Plate", xml); } [Fact] @@ -154,118 +152,5 @@ public void MiniWordIfStatement_FirstIf() Assert.Contains("CreateDate is not less than 2021", docXml); Assert.DoesNotContain("CreateDate is not greater than 2021", docXml); } - - [Fact] - public void TestForeachLoopInTablesWithIfStatement() - { - var path = PathHelper.GetTempFilePath(); - var templatePath = PathHelper.GetFile("TestForeachInTablesWithIfStatementDemo.docx"); - var value = new Dictionary() - { - ["TripHs"] = new List> - { - new Dictionary - { - { "sDate", DateTime.Parse("2022-09-08 08:30:00") }, - { "eDate", DateTime.Parse("2022-09-08 15:00:00") }, - { "How", "Discussion requirement part1" }, - { - "Details", new List() - { - new MiniWordForeach() - { - Value = new Dictionary() - { - {"Text", "Air"}, - {"Value", "Airplane"} - }, - Separator = " | " - }, - new MiniWordForeach() - { - Value = new Dictionary() - { - {"Text", "Parking"}, - {"Value", "Car"} - }, - Separator = " / " - }, - new MiniWordForeach() - { - Value = new Dictionary() - { - {"Text", "Hotel"}, - {"Value", "Room"} - }, - Separator = ", " - }, - new MiniWordForeach() - { - Value = new Dictionary() - { - {"Text", "Food"}, - {"Value", "Plate"} - }, - Separator = "" - } - } - } - }, - new Dictionary - { - { "sDate", DateTime.Parse("2022-09-09 08:30:00") }, - { "eDate", DateTime.Parse("2022-09-09 17:00:00") }, - { "How", "Discussion requirement part2 and development" }, - { - "Details", new List() - { - new MiniWordForeach() - { - Value = new Dictionary() - { - {"Text", "Air"}, - {"Value", "Airplane"} - }, - Separator = " | " - }, - new MiniWordForeach() - { - Value = new Dictionary() - { - {"Text", "Parking"}, - {"Value", "Car"} - }, - Separator = " / " - }, - new MiniWordForeach() - { - Value = new Dictionary() - { - {"Text", "Hotel"}, - {"Value", "Room"} - }, - Separator = ", " - }, - new MiniWordForeach() - { - Value = new Dictionary() - { - {"Text", "Food"}, - {"Value", "Plate"} - }, - Separator = "" - } - } - } - } - } - }; - MiniWord.SaveAsByTemplate(path, templatePath, value); - //System.Diagnostics.Process.Start("explorer.exe", path); - var xml = Helpers.GetZipFileContent(path, "word/document.xml"); - Assert.Contains(@"Discussion requirement part2 and development", xml); - Assert.Contains(@"Discussion requirement part1", xml); - Assert.Contains("Air way to the Airplane | Hotel way to the Room", xml); - } } } \ No newline at end of file diff --git a/tests/MiniWordTests/MiniWordTests.csproj b/tests/MiniWordTests/MiniWordTests.csproj index 953e880f7aadedaea4301edd56420519c6ee359c..69fdce85c9580c44b64ceeb3fb2bac72604edf6a 100644 --- a/tests/MiniWordTests/MiniWordTests.csproj +++ b/tests/MiniWordTests/MiniWordTests.csproj @@ -1,7 +1,7 @@ - net5.0; + net5.0 false