-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
update RemoveControlsAndKeepContent to support nested table rows
- Loading branch information
1 parent
094cb44
commit c19afa2
Showing
6 changed files
with
144 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
using System.IO; | ||
using System.Linq; | ||
using NUnit.Framework; | ||
using OpenXMLTemplates; | ||
using OpenXMLTemplates.ControlReplacers; | ||
using OpenXMLTemplates.Documents; | ||
using OpenXMLTemplates.Engine; | ||
using OpenXMLTemplates.Variables; | ||
namespace OpenXMLTempaltesTest.ControlRemovalTest | ||
{ | ||
public class Tests | ||
{ | ||
private TemplateDocument GetDoc => new TemplateDocument(this.CurrentFolder() + "Doc.docx"); | ||
private string GetData => File.ReadAllText(this.CurrentFolder() + "data.json"); | ||
|
||
[Test] | ||
public void TestControlRemoval() | ||
{ | ||
using var doc = GetDoc; | ||
var data = GetData; | ||
|
||
var src = new VariableSource(); | ||
src.LoadDataFromJson(data); | ||
|
||
var engine = new DefaultOpenXmlTemplateEngine | ||
{ | ||
KeepContentControlAfterReplacement = false | ||
}; | ||
engine.ReplaceAll(doc, src); | ||
|
||
doc.SaveAs(this.CurrentFolder() + "result.docx"); | ||
|
||
// confirm new content has been included in document | ||
string? docText = null; | ||
Check warning on line 34 in OpenXMLTemplatesTest/ControlRemovalTest/Tests.cs GitHub Actions / Build and test
|
||
|
||
using (StreamReader sr = new StreamReader(doc.WordprocessingDocument.MainDocumentPart.GetStream())) | ||
{ | ||
docText = sr.ReadToEnd(); | ||
} | ||
|
||
Assert.IsTrue(docText != null); | ||
var replacedText = src.GetVariable<string>("nested.[1].nestedList.[1]"); | ||
Assert.IsTrue(docText.Contains(replacedText)); | ||
|
||
// confirm controls have been removed | ||
Assert.AreEqual(0, | ||
doc.WordprocessingDocument.ContentControls().Count(cc => | ||
cc.GetContentControlTag() != null && cc.GetContentControlTag().StartsWith("repeatingitem"))); | ||
|
||
Assert.AreEqual(0, | ||
doc.WordprocessingDocument.ContentControls().Count(cc => | ||
cc.GetContentControlTag() != null && cc.GetContentControlTag() == "repeating_nestedList")); | ||
|
||
doc.WordprocessingDocument.AssertValid(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
{ | ||
"items": [ | ||
"Item 1", | ||
"Item 2", | ||
"Item 3", | ||
"Item 4" | ||
], | ||
"complexItems": [ | ||
{ | ||
"name": "First Name", | ||
"address": "First Address" | ||
}, | ||
{ | ||
"name": "Second name", | ||
"address": "Second address" | ||
} | ||
], | ||
"nested": [ | ||
{ | ||
"name": "List 1", | ||
"nestedList": [ | ||
"item1-1", | ||
"item1-2", | ||
"item1-3" | ||
] | ||
}, | ||
{ | ||
"name": "List 2", | ||
"nestedList": [ | ||
"item2-1", | ||
"item2-2" | ||
] | ||
} | ||
] | ||
} |
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters