Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enhanced Table Cell content #12

Open
wants to merge 3 commits into
base: vNext
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion OpenXmlPowerTools.Tests/DocumentAssemblerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,8 @@ public class DaTests
[InlineData("DA264-InvalidRunLevelRepeat.docx", "DA-Data.xml", true)]
[InlineData("DA265-RunLevelRepeatWithWhiteSpaceBefore.docx", "DA-Data.xml", false)]
[InlineData("DA266-RunLevelRepeat-NoData.docx", "DA-Data.xml", true)]

[InlineData("DA300-TableWithContentInCells.docx", "DA-Data.xml", false)]

public void DA101(string name, string data, bool err)
{
DirectoryInfo sourceDir = new DirectoryInfo("../../../../TestFiles/");
Expand Down
37 changes: 36 additions & 1 deletion OpenXmlPowerTools/DocumentAssembler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -717,10 +717,18 @@ static object ContentReplacementTransform(XNode node, XElement data, TemplateErr
XElement paragraph = tc.Elements(W.p).FirstOrDefault();
XElement cellRun = paragraph.Elements(W.r).FirstOrDefault();
string xPath = paragraph.Value;
bool cellIsOptional = false;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe this could be simplified to:

    var xPath = paragraph.Value;
    var optionalString = "";
    try
    {
        var xElement = XElement.Parse(xPath);
        xPath = (string)xElement.Attribute(PA.Select);
        optionalString = (string)xElement.Attribute(PA.Optional);
    }
    catch { }

    try
    {
        newValue = EvaluateXPathToString(d, xPath, optionalString?.ToLower() == "true");
    }


// There is probably a much better way of doing this,
// similar to the ContentReplacementTransform used for the footer
// but it means you probably couldn't use the simple xpath text
// and you would need to always use <Content/> tag in each cell!
TableCellContent(paragraph.Value, out xPath, out cellIsOptional);

string newValue = null;
try
{
newValue = EvaluateXPathToString(d, xPath, false);
newValue = EvaluateXPathToString(d, xPath, cellIsOptional);
}
catch (XPathException e)
{
Expand Down Expand Up @@ -781,6 +789,33 @@ static object ContentReplacementTransform(XNode node, XElement data, TemplateErr
return node;
}

/// <summary>
/// Convert the cell content to data using 2 different formats
/// 1. the text is xpath
/// 2. the text has the format <Content Select="xpath" Optional='true' />
/// </summary>
/// <param name="para">Paragraph text</param>
/// <param name="xpath">The resulting xpath of paragraph or Select attribute value</param>
/// <param name="optional">Incase 2 the value of the Optional attribute if it exists otherwise false</param>
private static void TableCellContent(string para, out string xpath, out bool optional)
{
XElement element;
try
{
element = XElement.Parse(para);
//the next few lines are used a couple of time in the ContentReplacementTransform method
xpath = (string)element.Attribute(PA.Select);
var optionalString = (string)element.Attribute(PA.Optional);
optional = (optionalString != null && optionalString.ToLower() == "true");
}
catch (Exception)
{
//can't be processed as Content so just assume xpath
xpath = para;
optional = false;
}
}

private static object CreateContextErrorMessage(XElement element, string errorMessage, TemplateError templateError)
{
XElement para = element.Descendants(W.p).FirstOrDefault();
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
NuGet Feed for CI build: https://ci.appveyor.com/nuget/open-xml-powertools
NuGet Feed for CI build: https://ci.appveyor.com/nuget/open-xml-powertools

No NuGet.org feed at this time. We are working on it.
[https://github.com/EricWhiteDev/Open-Xml-PowerTools](https://github.com/EricWhiteDev/Open-Xml-PowerTools)

News
====
Expand Down
Binary file added TestFiles/DA300-TableWithContentInCells.docx
Binary file not shown.