I am working with a 2D array in JavaScript within a Google Chrome extension. Each index contains text with various characters. I have successfully converted this 2D array into a CSV file and provided code for downloading it:
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".
Now, my challenge is how to upload and utilize this as a 2D array again (on another machine or in case of hard drive crash)? Upon researching online, I found solutions where the file cannot contain specific characters due to delimiter issues, among other complications.
If anyone could assist me, I would greatly appreciate it.
This simple solution may encounter difficulties if the cells contain quotes, commas, or other escaped characters. To handle more complex CSV strings, a Regex solution would need to be implemented (refer to the accepted answer on "How can I parse a CSV string with Javascript?"). Alternatively, using a library would be more efficient to support multiple common formats.