Newer
Older
Game / csvload.js
@Yuto Togashi Yuto Togashi on 23 Sep 2022 4 KB add 5 quiz
//GASのAPIのURL
var endpoint = "https://script.google.com/macros/s/AKfycbwGy_ohKubQtJyGjzpJ_ggpIGaN6n1VtM3KS1d7RiSomG0icZkqxCDb9Pbnf2RADe1Z/exec";

//APIを使って非同期データを取得する
fetch(endpoint)
.then(response => response.json())
/*成功した処理*/
.then(data => {
    console.log(JSON.stringify(data, null, 2));
    console.log(json2csv(data));
});

(() => {
    var quiz = [],	// 読み取ったCSV全てが入る配列
    nQuiz,		// データの個数(quiz.lengthのまま使ってもよい)
    pos=0;		// 現在の配列の添字位置
    var csvfile = "data.csv";	// CSVデータファイル名
    function putValues(row) {
    // CSVの1行分のJSONがrowに入る
    for (let key of Object.keys(row)) {
        // Object.keys(JSON)でキーを1個ずつ取り出す(Rubyのkeys)
        let id = "__" + key + "__"; // HTML要素のID
        let elem = document.getElementById(id);
        if (elem) {  // もし id="__カラム名__" の要素が見つかったら
        elem.innerText = row[key]; // その要素の内部テキストを更新
        }
    }
    }
    function slide(n) {	// nで指定した数だけ現在位置(pos)をずらす
        pos = (nQuiz+pos+n) % nQuiz;	// 0以上 nQUIZ以下 になるよう調整
        putValues(quiz[pos]);		// ずらした添字位置で文書書き換え
    }
    function next() {
        slide(1);   // 1つ先に進むボタンの処理
        document.getElementById("result").innerHTML = " ";
        document.getElementById("next").setAttribute("class", "none");
    }	
    function init() {			// HTML文書を読み終わったら呼ばれる
        fetch(csvfile).			// csvfileをサーバから読み込んで
        then((resp) => {		// 応答がrespに入るので
        if (resp.ok) return resp.text();	// テキストを返す
        }).then((txt) => {		// テキストが txt に入るので
        quiz = new CSV(txt, {header: true}).parse(); // CSV解析
        nQuiz = quiz.length;	// 行数を記憶
        putValues(quiz[pos]);	// 最初の行のデータを表示
        });;
        document.getElementById("next").addEventListener("click", next);
    }
    document.addEventListener("DOMContentLoaded", ()=>{
        init();	// ブラウザの文書読み込みが完了したらここに来る
    }, false);
})();

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

function judgeA() {
    const choiceA = document.getElementById("__a__").value;
    var answer = document.getElementById("__answer__");
    if (choiceA == answer.textContent) {
        document.getElementById("result").innerHTML = "正解!!";
        document.getElementById("result").setAttribute("class", "quiz-correct");
        document.getElementById("next").removeAttribute("class");
    } else {
        document.getElementById("result").innerHTML = "不正解…";
        document.getElementById("result").setAttribute("class", "quiz-incorrect");
    }
}

function judgeB() {
    const choiceB = document.getElementById("__b__").value;
    var answer = document.getElementById("__answer__");
    if (choiceB == answer.textContent) {
        document.getElementById("result").innerHTML = "正解!!";
        document.getElementById("result").setAttribute("class", "quiz-correct");
        document.getElementById("next").removeAttribute("class");
    } else {
        document.getElementById("result").innerHTML = "不正解…";
        document.getElementById("result").setAttribute("class", "quiz-incorrect");
    }
}

function judgeC() {
    const choiceC = document.getElementById("__c__").value;
    var answer = document.getElementById("__answer__");
    if (choiceC == answer.textContent) {
        document.getElementById("result").innerHTML = "正解!!";
        document.getElementById("result").setAttribute("class", "quiz-correct");
        document.getElementById("next").removeAttribute("class");
    } else {
        document.getElementById("result").innerHTML = "不正解…";
        document.getElementById("result").setAttribute("class", "quiz-incorrect");
    }
}