We prepared following script to download data into CSV file:
// Variable to store the final csv data
var csv = [];
// Get each row data
var rows = document.querySelectorAll("table tr");
// Get each column data
for(var i = 1; i < rows.length; i++){
// Stores each csv row data
var row = [], cols = rows[i].querySelectorAll("td,th");
for(var j = 0; j < cols.length; j++){
// Get the text data of each cell
// of a row and push it to csvrow
row.push('"' + cols[j].innerText.trim() + '"');
}
// Combine each column value with comma
csv.push(row.join(",") + "n");
}
The problem is that we have a comma at the beginning of each line. Could you please advise how can we fix our code to remove this comma? Thank you! Sample output:
"Host","Version","Country","Brand","Store"
,"test1","5.2","MX","brand","1234"
,"test2","5.2","CA","brand","4321"
,"test3","5.2","US","brand","4444"
Desired output:
"Host","Version","Country","Brand","Store"
"test1","5.2","MX","brand","1234"
"test2","5.2","CA","brand","4321"
"test3","5.2","US","brand","4444"
3
Answers
The commas you see are probably the commas of the list.
you can use this to get it with no commas
The issue you’re facing is caused by the fact that you’re adding a comma before each column value in the
row.push
statement.To fix this, you can modify the code to handle the first column differently, making sure that a comma is not added before the first value in each row.
Try use
Instead of
Your push is adding the comma
This is a lot simpler