Newer
Older
Game / sample.js
@Yuto Togashi Yuto Togashi on 23 Sep 2022 959 bytes add json to csv function
//GASのAPIのURL
var endpoint = "https://script.google.com/macros/s/AKfycbwGy_ohKubQtJyGjzpJ_ggpIGaN6n1VtM3KS1d7RiSomG0icZkqxCDb9Pbnf2RADe1Z/exec";

//APIを使って非同期データを取得する
fetch(endpoint)
.then(response => response.json())
/*成功した処理*/
.then(data => {
    //JSONから配列に変換
/*
    var quizArray = data;

    for (var i = 0; i < quizArray.length; i++) { 
        var quizList = document.createElement('li'); 
        quizList.textContent = quizArray[i]; 
        document.getElementById('list').appendChild(quizList); 
    }

    console.table(quizArray);
*/
    console.log(JSON.stringify(data, null, 2));
    console.log(json2csv(data));
});

function json2csv(json) {
    var header = Object.keys(json[0]).join(',') + "\n";

    var body = json.map(function(d){
        return Object.keys(d).map(function(key) {
            return d[key];
        }).join(',');
    }).join("\n");

    return header + body;
}