Reputation: 1420
I am trying to export data into CSV using following function
function exportHtml(monthNo){
var data = [];
data.push('Person;Salary;Workover');
let query = Meteor.users.find().fetch().map(function(u){
var userFirst = u.profile.name_first;
var userLast = u.profile.name_last;
var currentId = u._id;
try {
var sum = Salaries.findOne({"userId": {$eq: currentId}, "month": {$eq: monthNo}, "year": {$eq: parseFloat(earningFilterYear.get())} } , {sort:{createdAt: -1}}).cost;
var hours = Salaries.findOne({"userId": {$eq: currentId}, "month": {$eq: monthNo}, "year": {$eq: parseFloat(earningFilterYear.get())} } , {sort:{createdAt: -1}}).over;
}catch(e){
}
if(!sum)
sum = 0;
data.push('\r\n');
data.push(userFirst+' '+userLast+';'+sum+';'+hours);
});
var link = document.createElement('a');
link.setAttribute('download', "Export_"+monthNo+"_"+earningFilterYear.get()+".csv");
link.setAttribute('href', 'data:text/html;charset=utf-8;' + data);
link.click();
Bert.alert('Data exported successfully', 'success' );
};
Expected behavior
Person;Salary;Workover
Name1;5;15
Name2;12;125
Reality (one line, ignore undefined)
,Admin Account;295.5;5,,Sergo Sarkjan;0;undefined,,Anton Lidat;0;undefined,,Aleksei Dmijev;0;undefined
It seems that new line isn't added even tho I have the \r\n, I have also tried without success, in addition for some reason there are these random commas all over the document. Why?
Upvotes: 0
Views: 101
Reputation: 10076
Change data:text/html
to data:text/plain
and replace in the same line data
with data.join('')
.
It should look like this:
link.setAttribute('href', 'data:text/plain;charset=utf-8,' + data.join(''));
Upvotes: 2