Download/Save CSV 文件 - PapaParse

Download/Save CSV file - PapaParse

问题:
下载的 CSV 文件是空白的(unparse() 方法将 array/JSON 转换为 CSV)。

详情:
将 CSV 文件解析为 JavaScript 数组时,Papaparse 工作正常。但是当我将该数组数据或 JSON 数据提供给 unparse() 方法时,它不起作用。

AngularJS方法:

$scope.downloadCSV = function(){
    var csv = Papa.unparse($scope.final_array);
    console.log($scope.final_array);
    console.log(csv);
    var csvData = new Blob([csv], {type: 'text/csv;charset=utf-8;'});
    var csvURL =  null;
    if (navigator.msSaveBlob) {
        csvURL = navigator.msSaveBlob(csvData, 'download.csv');
    } else {
        csvURL = window.URL.createObjectURL(csvData);
    }
    var tempLink = document.createElement('a');
    tempLink.href = csvURL;
    tempLink.setAttribute('download', 'download.csv');
    tempLink.click();
}

$scope.final_array 包含数据为:

在上面的代码中,console.log(csv); 在控制台上显示空白。

简而言之:var csv = Papa.unparse($scope.final_array); 不工作。

更新
发布的数组帮助我生成以下 table 按钮“保存生成的 CSV”不起作用,发布的代码用于此操作按钮。

您正在尝试 unparse 一个 数组数组 ,它只是没有列名的纯数据。

要使用列名,您可能需要使用 对象数组 版本。将您的 final_array 重写为:

$scope.final_array = [
    { question1: "A", question2: "A", question3: "mike" },
    { question1: "A B", question2: "B", question3: "dan" },
];

或者,您可以将列名称和单个对象中的数据分开,如下所示:

$scope.final_object = {
    fields: [ "question1", "question2", "question3" ],
    data: [
        [ "A", "A", "mike" ],
        [ "A B", "B", "dan", ],
    ],
};

如果您需要转换 $scope.final_array,也许以下代码片段会对您有所帮助:

function convertFinal(arrOfArr) {
    return arrOfArr.map(function(arr) {
        var obj = {};
        for(var key in arr) {
            if(arr.hasOwnProperty(key)) {
                obj[key] = arr[key];
            }
        }
        return obj;
    });
}

var csv = Papa.unparse(convertFinal($scope.final_array));

你的错误:如果我们查看 your console screenshot,我们会发现你的数组代码中有一些来自数组和对象的混杂。在你的代码中你有这样的代码:

var array =
[
    [question1: "A", question2: "A"],
    [question1: "A B", question2: "B"]
];

但它不正确,必须是这样的:

var array =
[
    {question1: "A", question2: "A"},
    {question1: "A B", question2: "B"}
];

你必须更正它。

工作示例

看到这个 Codepen demo 因为 Whosebug 片段在沙盒中,因此不起作用。

var array =
[
    {
        question1: "А",
        question2: "А",
        question3: "mike",
        question1_A: "TRUE",
        question1_B: "FALSE",
        question1_C: "FALSE",
        question1_D: "FALSE"
    },
    {
        question1: "A В",
        question2: "В",
        question3: "dan",
        question1_A: "TRUE",
        question1_B: "TRUE",
        question1_C: "FALSE",
        question1_D: "FALSE"
    },
    {
        question1: "B C D",
        question2: "А В С",
        question3: "tango",
        question1_A: "FALSE",
        question1_B: "TRUE",
        question1_C: "TRUE",
        question1_D: "TRUE"
    },
    {
        question1: "A D",
        question2: "С",
        question3: "charlie",
        question1_A: "TRUE",
        question1_B: "FALSE",
        question1_C: "FALSE",
        question1_D: "TRUE"
    },
    {
        question1: "В",
        question2: "А",
        question3: "bob",
        question1_A: "FALSE",
        question1_B: "TRUE",
        question1_C: "FALSE",
        question1_D: "FALSE"
    },
    {
        question1: "C D",
        question2: "А",
        question3: "john",
        question1_A: "FALSE",
        question1_B: "FALSE",
        question1_C: "FALSE",
        question1_D: "FALSE"
    }
];


function downloadCSV()
{
    var csv = Papa.unparse(array);

    var csvData = new Blob([csv], {type: 'text/csv;charset=utf-8;'});
    var csvURL =  null;
    if (navigator.msSaveBlob)
    {
        csvURL = navigator.msSaveBlob(csvData, 'download.csv');
    }
    else
    {
        csvURL = window.URL.createObjectURL(csvData);
    }

    var tempLink = document.createElement('a');
    tempLink.href = csvURL;
    tempLink.setAttribute('download', 'download.csv');
    tempLink.click();
}
<script src="https://www.papaparse.com/resources/js/papaparse.js"></script>
<input type="button" value="download CSV" onclick="downloadCSV()">

有关详细信息,请参阅 Papa's unparse 函数的文档。