export data in csv with rows and comments

This commit is contained in:
Baptiste Lemoine 2020-02-04 15:23:46 +01:00
parent 4bcd3e475f
commit e3e5cf6638
2 changed files with 58 additions and 7 deletions

View File

@ -31,7 +31,7 @@ export class PollDisplayComponent extends BaseComponent implements OnInit {
ngOnInit() {
this.config.exportCSV();
}

View File

@ -601,19 +601,70 @@ export class ConfigService extends PollConfig {
exportCSV() {
const rows = [
["name1", "city1", "some other info"],
["name2", "city2", "more info"]
let rows = [];
let now = new Date();
const headers = [
['export de sondage Framadate ', this.customUrl],
['le', now.toISOString()],
[this.currentPoll.pollId, this.currentPoll.title, this.customUrl, this.creationDate],
['pseudo']];
let listOfChoices = ['choices : '];
this.currentPoll.choices.map(choice => {
listOfChoices.push(choice.text)
});
listOfChoices.push('pseudo');
this.currentPoll.stacks.map(voteStack => {
let voteStackInArray = [voteStack.pseudo];
let keysVotes = Object.keys(voteStack.votes);
keysVotes.map(id => {
voteStackInArray.push(voteStack.votes[id].value ? voteStack.votes[id].value : "")
});
rows.push(
voteStackInArray
);
});
const headersComments = [
['comments'],
['pseudo', 'text', 'creation_date'],
];
const comments = [];
this.currentPoll.comments.map(item => {
comments.push(
[item.pseudo,
item.text,
item.date.date,
'\n']
)
});
headers.push(listOfChoices);
rows = [headers, listOfChoices, rows, headersComments, comments];
let convertedCsv = rows.map(elem => {
console.log('elem', elem)
return elem.map(item => {
console.log('item', item);
if (typeof item === typeof Array) {
return item.join('\n')
}
return item;
}).join('\n')
}).join('\n');
console.log('rows', rows);
console.log('convertedCsv', convertedCsv);
let csvContent = "data:text/csv;charset=utf-8,"
+ rows.map(e => e.join(",")).join("\n");
+ convertedCsv;
console.log('csvContent', csvContent)
var encodedUri = encodeURI(csvContent);
var link = document.createElement("a");
link.setAttribute("href", encodedUri);
link.setAttribute("download", this.makeSlug() + "_export_" + new Date() + ".csv");
let exportFileName = (this.urlPublic ? this.urlPublic : this.makeSlug()) + "_export_" + new Date() + ".csv";
link.setAttribute("download", exportFileName);
document.body.appendChild(link); // Required for FF
this.todo();
link.click(); // This will download the data file named "my_data.csv".
}