I have a 2D array (context: JavaScript, Google Chrome extension), each index has text consisting all kind of characters, I can convert this 2D-array into a csv
file and download it using below code:
function Download(){
//https://stackoverflow.com/a/14966131/11974735
var array = JSON.parse(sessionStorage.getItem("array"));
let csvContent = "data:text/csv;charset=utf-8,"
+ array.map(e => e.join(",")).join("\n");
var encodedUri = encodeURI(csvContent);
var link = document.createElement("a");
link.setAttribute("href", encodedUri);
link.setAttribute("download", "my.csv");
document.body.appendChild(link); // Required for FF
link.click();
}
// This will download the data file named "my_data.csv".
But how can I upload and use it as 2D-array again (in another machine, in case of hard drive crash)? I searched on internet, but the solution presented have conditions like the file can not consist a specific character (delimiter issue?), and other issues I could not get.
So can anyone help?
This basic solution falls apart if your cells contain quotes, commas or other escaped characters. To address more complex CSV strings, you'd have to implement a RegEx solution (see accepted answer to How can I parse a CSV string with Javascript?); and to support multiple common formats, you'd be better off just using a library.