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.

7
  • 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. Commented Mar 20, 2013 at 20:18
  • ....which can then be downloaded by the user. Commented Mar 20, 2013 at 20:19
  • 1
    possible duplicate of Javascript: Download data to file from content within the page Commented Mar 20, 2013 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. Commented Mar 20, 2013 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. Commented Mar 20, 2013 at 20:57

1 Answer 1

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");
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.