0

Basically I have a string variable that is composed of two other variables separated by a comma:

item = "Pencil";
amount = 5;
entry = item + "," + amount;
*export entry to csv

So the "entry" variable should be in the correct format to save as a comma separated file. Is there some command that will take this variable and save it as a csv, or any other format easily opened in a spreadsheet for later use? The entry variable will change and the new information will need to be appended to the csv file if it already exists. So let's say if we also had:

item = "Paper";
amount = 25;
entry = item + "," + amount;
*export entry to csv

The resulting csv file should be:

Pencil,5
Paper,25

I've done a bit of searching through other questions, but most folks seem to be trying to do more complex things (e.g., dealing with server vs. client-side issues) while I'm just trying to figure out how to get data I'm working on my own computer in javascript to a saveable file. Seems like that isn't the case for many questions being asked though. I'm sure there's a simple answer out there and hopefully just a single command or two. However, I've been wading through a lot of semi-related posts that aren't too clear, so I figured this would be quicker.

Tony H
  • 13
  • 4
  • 2
    Javascript does'nt really have access to the file system, so serverside code is usually the preferred way to save files, but that only saves them on the server. – adeneo Mar 20 '13 at 20:18
  • ....which can then be downloaded by the user. – Diodeus - James MacFarlane Mar 20 '13 at 20:19
  • 1
    possible duplicate of [Javascript: Download data to file from content within the page](http://stackoverflow.com/questions/4184944/javascript-download-data-to-file-from-content-within-the-page) – Felix Kling Mar 20 '13 at 20:19
  • Felix, that question is similar, but there didn't seem to be a clear answer, and I wasn't sure how up to date the answers were either. The main issue though is appending the file, which I haven't really seen addressed. – Tony H Mar 20 '13 at 20:38
  • It's still the same. Either you have to use flash or create data URLs. You cannot append to an existing file downloaded on the hard drive. You can only append to your own data on the page and create a new file. – Felix Kling Mar 20 '13 at 20:57
  • adeno, this script is written for Greasemonkey in Firefox, so would that help anything at all since the browser can be used to save files? It's definitely a stretch, but just a thought that could maybe be used as a connecting point. – Tony H Mar 20 '13 at 21:00
  • I'll look into the flash side of things then. Thanks Felix. – Tony H Mar 20 '13 at 21:06

1 Answers1

0

It is perfectly possible, using some FileSaver implementation. I believe it exists natively on IE10, but still needs a shim on other browsers.

I've written a light-weight client-side CSV generator library that might come in handy. Check it out on http://atornblad.se/github/ (scroll down to the headline saying Client-side CSV file generator)

As I said, it requires a functioning FileSaver implementation handling calls to window.saveAs(). Check out Eli Grey's solution on http://eligrey.com/blog/post/saving-generated-files-on-the-client-side

When in place, you simply generate and save CSV files on the fly like this:

var propertyOrder = ["name", "age", "height"];

var csv = new Csv(propertyOrder);

csv.add({ name : "Anders",
          age : 38,
          height : "178cm" });

csv.add({ name : "John Doe",
          age : 50,
          height : "184cm" });

csv.saveAs("people.csv");
Anders Marzi Tornblad
  • 18,896
  • 9
  • 51
  • 66