问题描述
我检查过的所有有关导出为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();
}
推荐答案
如上所述,我在 $ b中将其改写为从对象更改为多维数组时的$ b。现在我意识到我应该已经编辑了这个问题,但是由于使用对象时可能会有不同的解决方案,因此我认为应该是一个不同的问题。
As mentioned in my above comment, I rephrased this in Exporting an array of arrays to CSVas 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
这篇关于将对象导出并附加到csv的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!