Skip to content

Commit

Permalink
add download as csv (with semicolon seperator)
Browse files Browse the repository at this point in the history
  • Loading branch information
IRooc committed Sep 26, 2023
1 parent 1544c60 commit df5953e
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 3 deletions.
10 changes: 9 additions & 1 deletion Pages/Index.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,14 @@
if (ids?.Any() == true)
{
var users = ViewData["Users"] as List<Microsoft.Graph.Models.User>;
<table>
<table id="usertable">
<tr>
<th>id</th>
<th>email</th>
<th>name</th>
<th>department</th>
<th>jobtitle</th>
</tr>
@foreach(var id in ids)
{
var u = users.FirstOrDefault(u => u.Id == id.ToLowerInvariant());
Expand All @@ -42,6 +49,7 @@
</tr>
}
</table>
<a href="#" onclick="download_table_as_csv('usertable');">Download as CSV</a>
}
}
</>
2 changes: 1 addition & 1 deletion Pages/Index.cshtml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public async Task OnPost()

GraphServiceClient client = GetGraphClient();

var idlist = ids.Split('\n', ',', ';');
var idlist = ids.Split('\n', ',', ';').Where(l => !string.IsNullOrWhiteSpace(l));

var userIds = idlist.Select(i => i.Trim()).ToArray();
string[] props = new string[] { "id", "mail", "accountEnabled", "givenName", "surname", "department", "jobTitle" };
Expand Down
32 changes: 31 additions & 1 deletion wwwroot/js/site.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,34 @@
// Please see documentation at https://docs.microsoft.com/aspnet/core/client-side/bundling-and-minification
// Please see documentation at https://docs.microsoft.com/;aspnet/core/client-side/bundling-and-minification
// for details on configuring this project to bundle and minify static web assets.

// Write your JavaScript code.
// Quick and simple export target #table_id into a csv
function download_table_as_csv(table_id, separator = ';') {
// Select rows from table_id
var rows = document.querySelectorAll('table#' + table_id + ' tr');
// Construct csv
var csv = [];
for (var i = 0; i < rows.length; i++) {
var row = [], cols = rows[i].querySelectorAll('td, th');
for (var j = 0; j < cols.length; j++) {
// Clean innertext to remove multiple spaces and jumpline (break csv)
var data = cols[j].innerText.replace(/(\r\n|\n|\r)/gm, '').replace(/(\s\s)/gm, ' ')
// Escape double-quote with double-double-quote (see https://stackoverflow.com/questions/17808511/properly-escape-a-double-quote-in-csv)
data = data.replace(/"/g, '""');
// Push escaped string
row.push('"' + data + '"');
}
csv.push(row.join(separator));
}
var csv_string = csv.join('\n');
// Download it
var filename = 'export_' + table_id + '_' + new Date().toLocaleDateString() + '.csv';
var link = document.createElement('a');
link.style.display = 'none';
link.setAttribute('target', '_blank');
link.setAttribute('href', 'data:text/csv;charset=utf-8,' + encodeURIComponent(csv_string));
link.setAttribute('download', filename);
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
}

0 comments on commit df5953e

Please sign in to comment.