Skip to content

Commit

Permalink
Merge pull request #22 from Amberg/workitems/FixInCollectionSeperator…
Browse files Browse the repository at this point in the history
…Block

fix in collection separator block
  • Loading branch information
Amberg authored Oct 3, 2024
2 parents 0eff9c7 + 1714ee1 commit 37225a1
Showing 7 changed files with 73 additions and 26 deletions.
28 changes: 14 additions & 14 deletions DocxTemplater.Images/DocxTemplater.Images.csproj
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<GeneratePackageOnBuild>True</GeneratePackageOnBuild>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="GitVersion.MsBuild" Version="5.12.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="SixLabors.ImageSharp" Version="3.1.5" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\DocxTemplater\DocxTemplater.csproj" />
</ItemGroup>
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<GeneratePackageOnBuild>True</GeneratePackageOnBuild>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="GitVersion.MsBuild" Version="5.12.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="SixLabors.ImageSharp" Version="3.1.5" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\DocxTemplater\DocxTemplater.csproj" />
</ItemGroup>
</Project>
49 changes: 49 additions & 0 deletions DocxTemplater.Test/CollectionInTableTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
namespace DocxTemplater.Test
{
internal class CollectionInTableTest
{
[Test]
public void CollectionInTableTestRender()
{

using var fileStream = File.OpenRead("Resources/CollectionInTableCell.docx");
var docTemplate = new DocxTemplate(fileStream);
var data = new MyLessonsReportModel();
var lesson1 = new LessonReportModel
{
Date = DateTime.Now,
CourseDisplayName = "Course 1",
ParticipantsCount = 10,
Resources = new List<string> { "Resource 1", "Resource 2" }
};
var lesson2 = new LessonReportModel
{
Date = DateTime.Now.AddDays(1),
CourseDisplayName = "Course 2",
ParticipantsCount = 20,
Resources = new List<string> { "Resource 3", "Resource 4" }
};
data.Lessons = new List<LessonReportModel> { lesson1 };
docTemplate.BindModel("ds", data);
var result = docTemplate.Process();
docTemplate.Validate();
result.SaveAsFileAndOpenInWord();
}

public class MyLessonsReportModel
{
public IReadOnlyCollection<LessonReportModel> Lessons { get; set; } = new List<LessonReportModel>();
}

public class LessonReportModel
{
public DateTime Date { get; set; }

public string CourseDisplayName { get; set; }

public ICollection<string> Resources { get; set; } = new List<string>();

public int ParticipantsCount { get; set; }
}
}
}
11 changes: 7 additions & 4 deletions DocxTemplater.Test/DocxTemplater.Test.csproj
Original file line number Diff line number Diff line change
@@ -11,10 +11,10 @@
<ItemGroup>
<PackageReference Include="AutoBogus" Version="2.13.1" />
<PackageReference Include="JetBrains.DotMemoryUnit" Version="3.2.20220510" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.10.0" />
<PackageReference Include="NUnit" Version="4.1.0" />
<PackageReference Include="NUnit3TestAdapter" Version="4.5.0" />
<PackageReference Include="NUnit.Analyzers" Version="4.2.0">
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.11.1" />
<PackageReference Include="NUnit" Version="4.2.2" />
<PackageReference Include="NUnit3TestAdapter" Version="4.6.0" />
<PackageReference Include="NUnit.Analyzers" Version="4.3.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
@@ -35,6 +35,9 @@
<None Update="Resources\BillTemplate2.docx">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="Resources\CollectionInTableCell.docx">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="Resources\ComplexTemplate.docx">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
Binary file not shown.
4 changes: 2 additions & 2 deletions DocxTemplater/Blocks/CollectionSeparatorBlock.cs
Original file line number Diff line number Diff line change
@@ -15,8 +15,8 @@ public CollectionSeparatorBlock(IVariableReplacer variableReplacer, PatternType

public override void Expand(IModelLookup models, OpenXmlElement parentNode)
{
int count = (int)models.GetValue($"{ParentBlock.StartMatch.Variable}._Idx");
int length = (int)models.GetValue($"{ParentBlock.StartMatch.Variable}._Length");
int count = (int)models.GetValue($"{ParentBlock.StartMatch.Variable.TrimStart('.')}._Idx");
int length = (int)models.GetValue($"{ParentBlock.StartMatch.Variable.TrimStart('.')}._Length");
// last element is rendered first - get length and count ot to not render the last separator
if (length - count == 0)
{
2 changes: 1 addition & 1 deletion DocxTemplater/DocxTemplater.csproj
Original file line number Diff line number Diff line change
@@ -4,7 +4,7 @@
<GeneratePackageOnBuild>True</GeneratePackageOnBuild>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="DocumentFormat.OpenXml" Version="3.0.2" />
<PackageReference Include="DocumentFormat.OpenXml" Version="3.1.0" />
<PackageReference Include="DynamicExpresso.Core" Version="2.16.1" />
<PackageReference Include="GitVersion.MsBuild" Version="5.12.0">
<PrivateAssets>all</PrivateAssets>
5 changes: 0 additions & 5 deletions Todo.txt

This file was deleted.

0 comments on commit 37225a1

Please sign in to comment.