0

Everything I've looked up in regards to exporting to CSV seems to be related to exporting a predefined object list, or exporting an array.

I'm using an object constructor and grabbing user inputs from a form. I'd like to then append that info to an existing CSV file, but I can't seem to get this to work. I created a variable with the output of each input field, separated by commas, so I'm just trying to find a way to export/append this.

This is a school project and I'm quite new to javascript, so any help is appreciated.

function newProperty() {
    var number = document.frmPropData.txtNumber.value;
    var street = document.frmPropData.txtStreet.value;
    var suburb = document.frmPropData.txtSuburb.value;
    var postcode = document.frmPropData.txtPostcode.value;
    var status = document.frmPropData.drpStatus.value;
    var propertyid = i;


    if (number != "" && street != "" && suburb != "" && postcode != "" && status != "NA") {
        var x = new Property(number, street, suburb, postcode, status, propertyid);
        var result = "Added Property " + x.propertyid +"\n" + x.number + " " + x.street + "\n" + x.suburb + " " + x.postcode + "\n" + x.status;
        alert(result);

        createCSV({ filename: "properties2.csv" });

        x++ ;
        i++ ;
}
}
function createCSV(args) {
    var data, link
    var csv = document.frmPropData.txtNumber.value + "," + document.frmPropData.txtStreet.value + "," + document.frmPropData.txtSuburb.value
     + "," + document.frmPropData.txtPostcode.value + "," + document.frmPropData.drpStatus.value + "," + document.frmPropData.txtPID.value
    alert(csv);

    csv = 'data:text/csv;charset=utf-8,' + csv;
    datatype = encodeURI(csv);

    link = document.createElement('a');
    link.setAttribute('href', data);
    link.click();
}
Ronnie
  • 205
  • 3
  • 12
  • Which part of your code writes to a file? – PM 77-1 Aug 16 '19 at 12:42
  • Also, when `x` is an object what do you expect `x++` to accomplish? – PM 77-1 Aug 16 '19 at 12:43
  • 1
    You can not append data to a locally existing csv file over the internet (without using activeX) by javascript alone. You can merely read it, append data and download it again as whole (=new). – Lain Aug 16 '19 at 12:46
  • The files will be local, not online. Will it still need activeX? – Ronnie Aug 16 '19 at 13:23
  • I actually just realized that I declared x twice. Once as an object and globally as a variable with the value of 1. The idea was to create a new name for the variable that was holding the object properties, as it kept retaining information from the previous form submission. I must have overlooked this. I guess I can remove the global declaration and the x++, as I have the fields clearing after each submission (not shown). – Ronnie Aug 16 '19 at 13:23
  • I couldn't figure out writing to a file, so this code was to hopefully create and download a csv, but I'm not getting the download prompt. – Ronnie Aug 16 '19 at 13:45
  • You have to add the download attribute to the anchor. Chrome blockes downloads via top level urls since late and in IE you require their own implementation. – Lain Aug 17 '19 at 15:37
  • Thanks. I've rephrased the question in https://stackoverflow.com/questions/57542975/exporting-an-array-of-arrays-to-csv as I am now using multidimensional arrays instead of objects. – Ronnie Aug 18 '19 at 08:42

1 Answers1

0

As mentioned in my above comment, I rephrased this in Exporting an array of arrays to CSV as I changed from objects to mutlidimensional arrays. I realize now that I should have just edited this question, but as there may be a different solution when working with objects, I thought it should be a different question.

In either case, I've resolved this from multidimensional arrays; the solution can be found at Exporting an array of arrays to CSV

Ronnie
  • 205
  • 3
  • 12