将对象导出并附加到 csv [英] Export and append object to csv

查看:26
本文介绍了将对象导出并附加到 csv的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我所查找的有关导出到 CSV 的所有内容似乎都与导出预定义的对象列表或导出数组有关.

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.

我正在使用对象构造函数并从表单中获取用户输入.我想然后将该信息附加到现有的 CSV 文件中,但我似乎无法让它工作.我用每个输入字段的输出创建了一个变量,用逗号分隔,所以我只是想找到一种方法来导出/附加它.

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.

这是一个学校项目,我对 javascript 还很陌生,因此非常感谢您的帮助.

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

推荐答案

正如我在上面的评论中提到的,我在 将数组数组导出为 CSV当我从对象更改为多维数组时.我现在意识到我应该刚刚编辑这个问题,但由于在处理对象时可能有不同的解决方案,我认为这应该是一个不同的问题.

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.

无论哪种情况,我都从多维数组中解决了这个问题;可以在将数组数组导出到 CSV

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

这篇关于将对象导出并附加到 csv的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆