Skip to content

Commit a8b8094

Browse files
committed
高级替换 对表格内的支持优化
1 parent 776e1e1 commit a8b8094

2 files changed

Lines changed: 29 additions & 4 deletions

File tree

src/MiniWord/Extensions/ObjectExtension.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ internal static Dictionary<string, object> ToDictionary(this object value)
2929

3030
if (IsStrongTypeEnumerable(val1))
3131
{
32-
var isValueOrStringType = false; ;
32+
var isValueOrStringType = false;
3333
List<Dictionary<string, object>> sx = new List<Dictionary<string, object>>();
3434
foreach (object val1item in (IEnumerable)val1)
3535
{
@@ -76,7 +76,7 @@ internal static Dictionary<string, object> ToDictionary(this object value)
7676
return reuslt;
7777
}
7878
}
79-
internal static bool IsStrongTypeEnumerable(object obj)
79+
internal static bool IsStrongTypeEnumerable(this object obj)
8080
{
8181
return obj is IEnumerable && !(obj is string) && !(obj is char[]) && !(obj is string[]);
8282
}

src/MiniWord/MiniWord.Implment.cs

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ private static void Generate(this OpenXmlElement xmlElement, WordprocessingDocum
4545
// avoid {{tag}} like <t>{</t><t>{</t>
4646
//AvoidSplitTagText(xmlElement);
4747
// avoid {{tag}} like <t>aa{</t><t>{</t> test in...
48-
AvoidSplitTagText(xmlElement, tags.Select(o => "{{" + o.Key + "}}"));
48+
AvoidSplitTagText(xmlElement, GetReplaceKeys(tags));
4949

5050
//Tables
5151
var tables = xmlElement.Descendants<Table>().ToArray();
@@ -149,7 +149,7 @@ private static void AvoidSplitTagText(OpenXmlElement xmlElement)
149149

150150
private static void AvoidSplitTagText(OpenXmlElement xmlElement, IEnumerable<string> txt)
151151
{
152-
foreach (var paragraph in xmlElement.Elements<Paragraph>())
152+
foreach (var paragraph in xmlElement.Descendants<Paragraph>())
153153
{
154154
foreach (var continuousString in paragraph.GetContinuousString())
155155
{
@@ -161,6 +161,31 @@ private static void AvoidSplitTagText(OpenXmlElement xmlElement, IEnumerable<str
161161
}
162162
}
163163

164+
private static List<string> GetReplaceKeys(Dictionary<string, object> tags)
165+
{
166+
var keys = new List<string>();
167+
foreach (var item in tags)
168+
{
169+
if (item.Value.IsStrongTypeEnumerable())
170+
{
171+
foreach (var item2 in (IEnumerable)item.Value)
172+
{
173+
if (item2 is Dictionary<string, object> dic)
174+
{
175+
foreach (var item3 in dic.Keys)
176+
keys.Add("{{" + item.Key + "." + item3 + "}}");
177+
}
178+
break;
179+
}
180+
}
181+
else
182+
{
183+
keys.Add("{{" + item.Key + "}}");
184+
}
185+
}
186+
return keys;
187+
}
188+
164189
private static void ReplaceText(OpenXmlElement xmlElement, WordprocessingDocument docx, Dictionary<string, object> tags)
165190
{
166191
var paragraphs = xmlElement.Descendants<Paragraph>().ToArray();

0 commit comments

Comments
 (0)