I am downloading a csv using the following code.
<a download="somedata.csv" id='x'>Download CSV</a>
<script>
var csv = '01'; //prints 1 in the cell, truncating leading zero
var csv = "'"+01+"'"; //prints '01' in the cell, with single quote
var csv = '"\""01"\""'; //prints "01" in the cell, with double quotes
var a = document.getElementById('x');
a.href='data:text/csv;base64,' + btoa(csv);
</script>
I need to print the numbers with leading zeroes in the csv file. The solution I have found so far is to make the number a text by wrapping it either with single or double quotes. But this does not solve the problem as it prints the quotes also in the cell.
Isn't there a way to preserve the leading zeroes and print the numbers without quotes in the cell? Any suggestions?
EDIT: I forgot to mention in the original post that I MUST need to open the csv in Excel.
var csv = "'01"; //prints '01 in the cell
var csv = '="01"'; //prints 01 in the cell, but the value is ="01"
Isn't there a way to get the cell value and display both as 01 (instead of different display and value)?
I know it is an old question, but I recently came across this issue and fixed it by adding Zero-Width-Space character at the end like this :
var csv = '01' + String.fromCharCode(8203);
I hope it will help others who come across this issue.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With