mirror of
https://framagit.org/framasoft/framadate/funky-framadate-front.git
synced 2023-08-25 13:53:14 +02:00
export data in csv with rows and comments
This commit is contained in:
parent
4bcd3e475f
commit
e3e5cf6638
@ -31,7 +31,7 @@ export class PollDisplayComponent extends BaseComponent implements OnInit {
|
||||
|
||||
ngOnInit() {
|
||||
|
||||
|
||||
this.config.exportCSV();
|
||||
}
|
||||
|
||||
|
||||
|
@ -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".
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user