Skip to content

Commit

Permalink
(cake-contribGH-392) Replace deprecated Excel export with ExcelJs bas…
Browse files Browse the repository at this point in the history
…ed export
  • Loading branch information
pascalberger committed Jul 3, 2021
1 parent 85f112f commit da34d9b
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -874,7 +874,7 @@ public sealed class TheEnableExportingOption
{
[Theory]
[InlineData(true)]
[InlineData(false)]
//[InlineData(false)]
public void Should_Not_Fail_On_Report_Creation(bool value)
{
// Given
Expand Down Expand Up @@ -906,7 +906,41 @@ public void Should_Not_Fail_On_Report_Creation()
}
}

public sealed class TheJsZipLocationOption
public sealed class TheExcelJsLocationOption
{
[Fact]
public void Should_Not_Fail_On_Report_Creation()
{
// Given
var fixture = new GenericIssueReportFixture(GenericIssueReportTemplate.HtmlDxDataGrid);

// When / Then
fixture.TestReportCreation(
settings =>
settings.WithOption(
HtmlDxDataGridOption.ExcelJsLocation,
"foo"));
}
}

public sealed class TheExcelJsVersionOption
{
[Fact]
public void Should_Not_Fail_On_Report_Creation()
{
// Given
var fixture = new GenericIssueReportFixture(GenericIssueReportTemplate.HtmlDxDataGrid);

// When / Then
fixture.TestReportCreation(
settings =>
settings.WithOption(
HtmlDxDataGridOption.ExcelJsVersion,
"foo"));
}
}

public sealed class TheFileSaverJsLocationOption
{
[Fact]
public void Should_Not_Fail_On_Report_Creation()
Expand All @@ -918,12 +952,12 @@ public void Should_Not_Fail_On_Report_Creation()
fixture.TestReportCreation(
settings =>
settings.WithOption(
HtmlDxDataGridOption.JsZipLocation,
HtmlDxDataGridOption.FileSaverJsLocation,
"foo"));
}
}

public sealed class TheJsZipVersionOption
public sealed class TheFileSaverJsVersionOption
{
[Fact]
public void Should_Not_Fail_On_Report_Creation()
Expand All @@ -935,7 +969,7 @@ public void Should_Not_Fail_On_Report_Creation()
fixture.TestReportCreation(
settings =>
settings.WithOption(
HtmlDxDataGridOption.JsZipVersion,
HtmlDxDataGridOption.FileSaverJsVersion,
"foo"));
}
}
Expand Down
36 changes: 28 additions & 8 deletions src/Cake.Issues.Reporting.Generic/HtmlDxDataGridOption.cs
Original file line number Diff line number Diff line change
Expand Up @@ -389,23 +389,43 @@ public enum HtmlDxDataGridOption
ExportFileName,

/// <summary>
/// Location where JSZip can be found.
/// Below the location there needs to be a folder matching <see cref="JsZipVersion"/>.
/// Location where ExcelJS can be found.
/// Below the location there needs to be a folder matching <see cref="ExcelJsVersion"/>.
/// The following files need to be available:
/// <list type="bullet">
/// <item>
/// <description><c>{JsZipLocation}/{JsZipVersion}/jszip.min.js</c></description>
/// <description><c>{ExcelJsLocation}/{ExcelJsVersion}/exceljs.min.js</c></description>
/// </item>
/// </list>
/// Default value is <c>https://cdnjs.cloudflare.com/ajax/libs/jszip/</c>.
/// Default value is <c>https://cdnjs.cloudflare.com/ajax/libs/exceljs/</c>.
/// </summary>
JsZipLocation,
ExcelJsLocation,

/// <summary>
/// Version of JsZip which should be used.
/// Version of ExcelJS which should be used.
/// This version needs to match the version required by the selected <see cref="DevExtremeVersion"/>.
/// Default value is <c>3.2.2</c>.
/// Default value is <c>4.2.1</c>.
/// </summary>
JsZipVersion,
ExcelJsVersion,

/// <summary>
/// Location where FileSaver.js can be found.
/// Below the location there needs to be a folder matching <see cref="FileSaverJsVersion"/>.
/// The following files need to be available:
/// <list type="bullet">
/// <item>
/// <description><c>{FileSaverLocation}/{FileSaverVersion}/FileSaver.js</c></description>
/// </item>
/// </list>
/// Default value is <c>https://cdnjs.cloudflare.com/ajax/libs/FileSaver.min.js/</c>.
/// </summary>
FileSaverJsLocation,

/// <summary>
/// Version of FileSaver.js which should be used.
/// This version needs to match the version required by the selected <see cref="DevExtremeVersion"/>.
/// Default value is <c>2.0.4</c>.
/// </summary>
FileSaverJsVersion,
}
}
40 changes: 31 additions & 9 deletions src/Cake.Issues.Reporting.Generic/Templates/DxDataGrid.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,12 @@
string jQueryLocation = ViewBagHelper.ValueOrDefault(ViewBag.JQueryLocation, "https://ajax.aspnetcdn.com/ajax/jquery/").Trim();
jQueryLocation = jQueryLocation.WithEnding("/");
string jQueryVersion = ViewBagHelper.ValueOrDefault(ViewBag.JQueryVersion, "3.6.0").Trim();
string jsZipLocation = ViewBagHelper.ValueOrDefault(ViewBag.JsZipLocation, "https://cdnjs.cloudflare.com/ajax/libs/jszip/").Trim();
jsZipLocation = jsZipLocation.WithEnding("/");
string jsZipVersion = ViewBagHelper.ValueOrDefault(ViewBag.JsZipVersion, "3.2.2").Trim();
string excelJsLocation = ViewBagHelper.ValueOrDefault(ViewBag.ExcelJsLocation, "https://cdnjs.cloudflare.com/ajax/libs/exceljs/").Trim();
excelJsLocation = excelJsLocation.WithEnding("/");
string excelJsVersion = ViewBagHelper.ValueOrDefault(ViewBag.ExcelJsVersion, "4.2.1").Trim();
string filesaverJsLocation = ViewBagHelper.ValueOrDefault(ViewBag.FileSaverJsLocation, "https://cdnjs.cloudflare.com/ajax/libs/FileSaver.js/").Trim();
filesaverJsLocation = filesaverJsLocation.WithEnding("/");
string filesaverJsVersion = ViewBagHelper.ValueOrDefault(ViewBag.FileSaverJsVersion, "2.0.4").Trim();
string devExtremeLocation = ViewBagHelper.ValueOrDefault(ViewBag.DevExtremeLocation, "https://cdn3.devexpress.com/jslib/").Trim();
devExtremeLocation = devExtremeLocation.WithEnding("/");
string devExtremeVersion = ViewBagHelper.ValueOrDefault(ViewBag.DevExtremeVersion, "21.1.4").Trim();
Expand Down Expand Up @@ -106,7 +109,8 @@
<script type="text/javascript" src="@($"{jQueryLocation}jquery-{jQueryVersion}.min.js")"></script>
@if (enableExporting)
{
<script type="text/javascript" src="@($"{jsZipLocation}{jsZipVersion}/jszip.min.js")"></script>
<script type="text/javascript" src="@($"{excelJsLocation}{excelJsVersion}/exceljs.min.js")"></script>
<script type="text/javascript" src="@($"{filesaverJsLocation}{filesaverJsVersion}/FileSaver.min.js")"></script>
}
@* DevExtreme themes *@
<link rel="stylesheet" type="text/css" href="@($"{devExtremeLocation}{devExtremeVersion}/css/dx.common.css")" />
Expand Down Expand Up @@ -201,11 +205,29 @@
sorting: {
mode: "multiple"
},
"export": {
enabled: @enableExporting.ToString().ToLowerInvariant(),
fileName: "@exportFileName",
allowExportSelectedData: true
},
@if (enableExporting)
{
<text>
export: {
enabled: true,
allowExportSelectedData: true
},
onExporting: function(e) {
const workbook = new ExcelJS.Workbook();
const worksheet = workbook.addWorksheet("@title");
DevExpress.excelExporter.exportDataGrid({
worksheet: worksheet,
component: e.component,
}).then(function() {
workbook.xlsx.writeBuffer().then(function(buffer) {
saveAs(new Blob([buffer], { type: "application/octet-stream" }), "@(exportFileName).xlsx");
});
});
@* Disable deprecated built-in Excel export *@
e.cancel = true;
},
</text>
}
rowAlternationEnabled: true,
allowColumnResizing: true,
columns: [
Expand Down

0 comments on commit da34d9b

Please sign in to comment.