Skip to content

Commit

Permalink
Merge pull request #7 from aldobrynin/a.dobrynin/CommentsAndDataValid…
Browse files Browse the repository at this point in the history
…ationsByReferenceOrderFix

Append LegacyDrawing element after PageMargins
  • Loading branch information
Umqra authored Aug 5, 2020
2 parents 5989cd3 + 1a2fb52 commit a1bc7af
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 10 deletions.
3 changes: 3 additions & 0 deletions Excel.TemplateEngine.Tests/Excel.TemplateEngine.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,9 @@
<None Update="ObjectPrintingTests\Files\сheckBoxes_template.xlsx">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="ObjectPrintingTests\Files\commentsAndOtherSheetDataValidations.xlsx">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
</ItemGroup>

</Project>
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
using System.Diagnostics;
using System.IO;
using System.Linq;

using NUnit.Framework;

using SkbKontur.Excel.TemplateEngine.FileGenerating;
using SkbKontur.Excel.TemplateEngine.FileGenerating.Primitives;
using SkbKontur.Excel.TemplateEngine.ObjectPrinting.ExcelDocumentPrimitives.Implementations;
using SkbKontur.Excel.TemplateEngine.ObjectPrinting.NavigationPrimitives.Implementations;
using SkbKontur.Excel.TemplateEngine.ObjectPrinting.TableBuilder;
Expand Down Expand Up @@ -162,15 +164,7 @@ public void TestDataValidationsFromTheOtherWorksheet()
{
targetDocument.CopyVbaInfoFrom(templateDocument);

foreach (var index in Enumerable.Range(1, templateDocument.GetWorksheetCount() - 1))
{
var worksheet = templateDocument.GetWorksheet(index);
var name = templateDocument.GetWorksheetName(index);
var innerTemplateEngine = new SkbKontur.Excel.TemplateEngine.TemplateEngine(new ExcelTable(worksheet), logger);
var targetWorksheet = targetDocument.AddWorksheet(name);
var innerTableBuilder = new TableBuilder(new ExcelTable(targetWorksheet), new TableNavigator(new CellPosition("A1"), logger));
innerTemplateEngine.Render(innerTableBuilder, new {});
}
CopySecondaryWorksheets(templateDocument, targetDocument);

var template = new ExcelTable(templateDocument.GetWorksheet(0));
var templateEngine = new SkbKontur.Excel.TemplateEngine.TemplateEngine(template, logger);
Expand Down Expand Up @@ -236,6 +230,50 @@ public void TestPrintingCommentsWithSeveralAuthors()
Assert.Fail($"Please manually open file:\n{path}\nand check that cells has same comments as in\n{templatePath}\n");
}
}

/// <summary>
/// output file must contain data validations AND rendered comment
/// </summary>
[Test]
public void TestPrintingCommentsWithEnabledDataValidationByOtherSheetData()
{
using (var templateDocument = ExcelDocumentFactory.CreateFromTemplate(File.ReadAllBytes(GetFilePath("commentsAndOtherSheetDataValidations.xlsx")), logger))
using (var targetDocument = ExcelDocumentFactory.CreateFromTemplate(File.ReadAllBytes(GetFilePath("empty.xlsx")), logger))
{
CopySecondaryWorksheets(templateDocument, targetDocument);

var template = new ExcelTable(templateDocument.GetWorksheet(0));
var templateEngine = new TemplateEngine(template, logger);

var target = new ExcelTable(targetDocument.GetWorksheet(0));
var tableNavigator = new TableNavigator(new CellPosition("A1"), logger);
var tableBuilder = new TableBuilder(target, tableNavigator, new Style(template.GetCell(new CellPosition("A1"))));
templateEngine.Render(tableBuilder, new {});

var filename = "output.xlsx";
File.WriteAllBytes(filename, targetDocument.CloseAndGetDocumentBytes());

var path = "file:///" + Path.GetFullPath(filename);
Process.Start(filename);
Assert.Fail($@"Please check that opened file has:
{path}
- comment/annotation
- D4-D7 has data validation with values from the second worksheet and G4-G7 has data validation with values from K1:K6");
}
}

private void CopySecondaryWorksheets(IExcelDocument templateDocument, IExcelDocument targetDocument)
{
foreach (var index in Enumerable.Range(1, templateDocument.GetWorksheetCount() - 1))
{
var worksheet = templateDocument.GetWorksheet(index);
var name = templateDocument.GetWorksheetName(index);
var innerTemplateEngine = new TemplateEngine(new ExcelTable(worksheet), logger);
var targetWorksheet = targetDocument.AddWorksheet(name);
var innerTableBuilder = new TableBuilder(new ExcelTable(targetWorksheet), new TableNavigator(new CellPosition("A1"), logger));
innerTemplateEngine.Render(innerTableBuilder, new {});
}
}

private readonly ConsoleLog logger = new ConsoleLog();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@ private void CopyVmlDrawingPartAndGetId([NotNull] WorksheetPart templateWorkshee
var vmlDrawingPartId = vmlDrawingPart == null ? null : templateWorksheetPart.GetIdOfPart(vmlDrawingPart);
SafelyAddPart(targetWorksheet.WorksheetPart, vmlDrawingPart, vmlDrawingPartId);
targetWorksheet.RemoveAllChildren<LegacyDrawing>();
targetWorksheet.Append(new LegacyDrawing {Id = vmlDrawingPartId});
targetWorksheet.InsertAfter(new LegacyDrawing {Id = vmlDrawingPartId}, worksheet.GetFirstChild<PageMargins>());
}

[CanBeNull]
Expand Down

0 comments on commit a1bc7af

Please sign in to comment.