diff --git a/src/app/pages/poll-display/poll-display.component.ts b/src/app/pages/poll-display/poll-display.component.ts index c46ac19c..a41cb023 100644 --- a/src/app/pages/poll-display/poll-display.component.ts +++ b/src/app/pages/poll-display/poll-display.component.ts @@ -31,7 +31,7 @@ export class PollDisplayComponent extends BaseComponent implements OnInit { ngOnInit() { - + this.config.exportCSV(); } diff --git a/src/app/services/config.service.ts b/src/app/services/config.service.ts index a33aaec6..3d5a0e6e 100644 --- a/src/app/services/config.service.ts +++ b/src/app/services/config.service.ts @@ -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". }