From 70f70c3d9862a8ace9b7e2b714d027d2d061111b Mon Sep 17 00:00:00 2001 From: Neal Geilen Date: Sat, 4 Jun 2022 13:22:56 +0200 Subject: [PATCH 1/2] Adding Xls support --- Epsilon.Canvas.Abstractions/Data/Module.cs | 5 ++ Epsilon/Epsilon.csproj | 1 + .../Export/ExcelCanvasModuleFileExporter.cs | 88 +++++++++++++++++++ .../CoreServiceCollectionExtensions.cs | 1 + 4 files changed, 95 insertions(+) create mode 100644 Epsilon/Export/ExcelCanvasModuleFileExporter.cs diff --git a/Epsilon.Canvas.Abstractions/Data/Module.cs b/Epsilon.Canvas.Abstractions/Data/Module.cs index 874dcafb..5ac2780b 100644 --- a/Epsilon.Canvas.Abstractions/Data/Module.cs +++ b/Epsilon.Canvas.Abstractions/Data/Module.cs @@ -10,4 +10,9 @@ public record Module( { [JsonIgnore] public IList Assignments { get; set; } = new Collection(); + + public bool HasAssignments() + { + return this.Assignments.Count > 0; + } } \ No newline at end of file diff --git a/Epsilon/Epsilon.csproj b/Epsilon/Epsilon.csproj index e6e27c1b..2030f0c7 100644 --- a/Epsilon/Epsilon.csproj +++ b/Epsilon/Epsilon.csproj @@ -12,6 +12,7 @@ + diff --git a/Epsilon/Export/ExcelCanvasModuleFileExporter.cs b/Epsilon/Export/ExcelCanvasModuleFileExporter.cs new file mode 100644 index 00000000..fff98cd3 --- /dev/null +++ b/Epsilon/Export/ExcelCanvasModuleFileExporter.cs @@ -0,0 +1,88 @@ +using Epsilon.Abstractions.Export; +using Epsilon.Canvas.Abstractions.Data; +using ExcelLibrary.SpreadSheet; + +namespace Epsilon.Export; + +public class ExcelCanvasModuleFileExporter : ICanvasModuleFileExporter +{ + public bool CanExport(string format) => format.ToLower() == "xls"; + + public List GetAllOutcomesTypes(Module module) + { + List addedOutcomes = new List(); + if (module.HasAssignments()) + { + foreach (var assignment in module.Assignments) + { + foreach (var result in assignment.OutcomeResults) + { + if (result.Outcome != null && !addedOutcomes.Contains(result.Outcome)) + { + addedOutcomes.Add(result.Outcome); + } + } + } + } + + return addedOutcomes.OrderByDescending(o => o.Title.Length).ToList(); + } + + public int GetOutcomeRow(List outcomes, Outcome outcome) + { + var result = outcomes.Find(o => o.Title == outcome.Title); + if (result != null) + { + return outcomes.IndexOf(result); + } + + return 0; + } + + public void Export(IEnumerable modules, string path) + { + Workbook workbook = new Workbook(); + + foreach (var module in modules) + { + if (module.HasAssignments()) + { + List outcomes = GetAllOutcomesTypes(module); + Worksheet worksheet = new Worksheet(module.Name); + //Because reasons @source https://stackoverflow.com/a/8127642 + for(int i = 0;i < 100; i++) + worksheet.Cells[i,0] = new Cell(""); + + //Adding all the outcomes. + for (int index = 0; index < outcomes.Count; index++) + { + worksheet.Cells[index, 0] = new Cell(outcomes[index].Title); + } + + foreach (var assignment in module.Assignments) + { + foreach (var outcomeResult in assignment.OutcomeResults) + { + if (outcomeResult.Outcome != null) + { + int row = GetOutcomeRow(outcomes, outcomeResult.Outcome); + + //Adding assignments to the outcomes + string cellValue = worksheet.Cells[row, 1].StringValue; + cellValue += (cellValue != "" ? "\n": "") + assignment.Name + " " + assignment.Url ; + + worksheet.Cells[row, 1] = new Cell(cellValue); + } + + } + } + + worksheet.Cells.ColumnWidth[0, 0] = 5000; + worksheet.Cells.ColumnWidth[0, 1] = 8000; + workbook.Worksheets.Add(worksheet); + + } + } + workbook.Save(path + ".xls"); + } +} \ No newline at end of file diff --git a/Epsilon/Extensions/CoreServiceCollectionExtensions.cs b/Epsilon/Extensions/CoreServiceCollectionExtensions.cs index 9143e3a1..16cbe451 100644 --- a/Epsilon/Extensions/CoreServiceCollectionExtensions.cs +++ b/Epsilon/Extensions/CoreServiceCollectionExtensions.cs @@ -21,6 +21,7 @@ private static IServiceCollection AddExport(this IServiceCollection services, IC services.Configure(config); services.AddScoped(); services.AddScoped(); + services.AddScoped(); services.AddScoped(); return services; From 0d4085590260f4c100b7466da38f68366d5f7084 Mon Sep 17 00:00:00 2001 From: Neal Geilen Date: Sat, 4 Jun 2022 13:36:33 +0200 Subject: [PATCH 2/2] Made changes to support new export architecture --- .../ExcelModuleExporter.cs} | 18 +++++++++++++----- .../CoreServiceCollectionExtensions.cs | 1 + 2 files changed, 14 insertions(+), 5 deletions(-) rename Epsilon/Export/{ExcelCanvasModuleFileExporter.cs => Exporters/ExcelModuleExporter.cs} (85%) diff --git a/Epsilon/Export/ExcelCanvasModuleFileExporter.cs b/Epsilon/Export/Exporters/ExcelModuleExporter.cs similarity index 85% rename from Epsilon/Export/ExcelCanvasModuleFileExporter.cs rename to Epsilon/Export/Exporters/ExcelModuleExporter.cs index fff98cd3..9f1e67f8 100644 --- a/Epsilon/Export/ExcelCanvasModuleFileExporter.cs +++ b/Epsilon/Export/Exporters/ExcelModuleExporter.cs @@ -1,12 +1,20 @@ using Epsilon.Abstractions.Export; using Epsilon.Canvas.Abstractions.Data; using ExcelLibrary.SpreadSheet; +using Microsoft.Extensions.Options; -namespace Epsilon.Export; +namespace Epsilon.Export.Exporters; -public class ExcelCanvasModuleFileExporter : ICanvasModuleFileExporter +public class ExcelModuleExporter : ICanvasModuleExporter { - public bool CanExport(string format) => format.ToLower() == "xls"; + private readonly ExportOptions _options; + + public ExcelModuleExporter(IOptions options) + { + _options = options.Value; + } + + public IEnumerable Formats { get; } = new[] { "xls" }; public List GetAllOutcomesTypes(Module module) { @@ -39,7 +47,7 @@ public int GetOutcomeRow(List outcomes, Outcome outcome) return 0; } - public void Export(IEnumerable modules, string path) + public void Export(IEnumerable modules, string format) { Workbook workbook = new Workbook(); @@ -83,6 +91,6 @@ public void Export(IEnumerable modules, string path) } } - workbook.Save(path + ".xls"); + workbook.Save($"{_options.FormattedOutputName}.{format}"); } } \ No newline at end of file diff --git a/Epsilon/Extensions/CoreServiceCollectionExtensions.cs b/Epsilon/Extensions/CoreServiceCollectionExtensions.cs index 1b5afb09..47e35cef 100644 --- a/Epsilon/Extensions/CoreServiceCollectionExtensions.cs +++ b/Epsilon/Extensions/CoreServiceCollectionExtensions.cs @@ -23,6 +23,7 @@ private static IServiceCollection AddExport(this IServiceCollection services, IC services.AddScoped(); services.AddScoped(); + services.AddScoped(); services.AddScoped();