This repository has been archived by the owner on Sep 4, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #15 from Typiqally/4-adding-file-export-types
- Loading branch information
Showing
4 changed files
with
103 additions
and
0 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
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
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,96 @@ | ||
using Epsilon.Abstractions.Export; | ||
using Epsilon.Canvas.Abstractions.Data; | ||
using ExcelLibrary.SpreadSheet; | ||
using Microsoft.Extensions.Options; | ||
|
||
namespace Epsilon.Export.Exporters; | ||
|
||
public class ExcelModuleExporter : ICanvasModuleExporter | ||
{ | ||
private readonly ExportOptions _options; | ||
|
||
public ExcelModuleExporter(IOptions<ExportOptions> options) | ||
{ | ||
_options = options.Value; | ||
} | ||
|
||
public IEnumerable<string> Formats { get; } = new[] { "xls" }; | ||
|
||
public List<Outcome> GetAllOutcomesTypes(Module module) | ||
{ | ||
List<Outcome> addedOutcomes = new List<Outcome>(); | ||
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<Outcome> 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<Module> modules, string format) | ||
{ | ||
Workbook workbook = new Workbook(); | ||
|
||
foreach (var module in modules) | ||
{ | ||
if (module.HasAssignments()) | ||
{ | ||
List<Outcome> 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($"{_options.FormattedOutputName}.{format}"); | ||
} | ||
} |
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