Export and append object to csv

眉间皱痕 提交于 2019-12-11 17:04:25

问题


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();
}

回答1:


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



来源:https://stackoverflow.com/questions/57524757/export-and-append-object-to-csv

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!