diff --git a/csvload.js b/csvload.js index 0935333..9774393 100644 --- a/csvload.js +++ b/csvload.js @@ -1,3 +1,15 @@ +//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のまま使ってもよい) @@ -39,16 +51,28 @@ }, 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", "correct"); + document.getElementById("result").setAttribute("class", "quiz-correct"); document.getElementById("next").removeAttribute("class"); } else { document.getElementById("result").innerHTML = "不正解…"; - document.getElementById("result").setAttribute("class", "incorrect"); + document.getElementById("result").setAttribute("class", "quiz-incorrect"); } } @@ -57,11 +81,11 @@ var answer = document.getElementById("__answer__"); if (choiceB == answer.textContent) { document.getElementById("result").innerHTML = "正解!!"; - document.getElementById("result").setAttribute("class", "correct"); + document.getElementById("result").setAttribute("class", "quiz-correct"); document.getElementById("next").removeAttribute("class"); } else { document.getElementById("result").innerHTML = "不正解…"; - document.getElementById("result").setAttribute("class", "incorrect"); + document.getElementById("result").setAttribute("class", "quiz-incorrect"); } } @@ -70,10 +94,10 @@ var answer = document.getElementById("__answer__"); if (choiceC == answer.textContent) { document.getElementById("result").innerHTML = "正解!!"; - document.getElementById("result").setAttribute("class", "correct"); + document.getElementById("result").setAttribute("class", "quiz-correct"); document.getElementById("next").removeAttribute("class"); } else { document.getElementById("result").innerHTML = "不正解…"; - document.getElementById("result").setAttribute("class", "incorrect"); + document.getElementById("result").setAttribute("class", "quiz-incorrect"); } } \ No newline at end of file diff --git a/webapi.js b/webapi.js index 042b729..fa5e3a4 100644 --- a/webapi.js +++ b/webapi.js @@ -1,3 +1,27 @@ +//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)); +}); + +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; +} + let quizData = {}; let currentQuizNo = 0; let correctCount = 0; @@ -9,9 +33,8 @@ // 問題開始のイベント設定 register_start_event(); -/** +/* * 問題のデータを取得する - * */ function get_quiz_data() { let xhr = new XMLHttpRequest(); @@ -23,9 +46,8 @@ xhr.send(); } -/** +/* * 問題開始のイベントを設定する - * */ function register_start_event() { document.querySelector('.js-quiz-start').addEventListener('click', function() { @@ -36,9 +58,8 @@ }, false); } -/** +/* * 問題の選択肢を選択したときのイベントを設定する - * */ function register_choice_event() { for (var i = 0; i < document.querySelectorAll('.js-quiz-choice').length; i++) { @@ -58,9 +79,8 @@ } } -/** +/* * 次の問題へ遷移するときのイベントを設定する - * */ function register_nextquiz_event() { document.querySelector('.js-quiz-next').addEventListener('click', function() { @@ -72,9 +92,8 @@ }, false); } -/** +/* * 結果画面へ遷移するときのイベントを設定する - * */ function register_result_event() { document.querySelector('.js-quiz-result').addEventListener('click', function() { @@ -85,9 +104,8 @@ }, false); } -/** +/* * トップへ遷移するときのイベントを設定する - * */ function register_top_event() { document.querySelector('.js-quiz-top').addEventListener('click', function() { @@ -101,11 +119,9 @@ }, false); } -/** +/* * トップ画面を生成する - * */ -// function generate_top_content() { var ins = '
'; ins += ''; @@ -114,9 +130,8 @@ document.querySelector('.js-quiz-content').innerHTML = ins; } -/** +/* * 問題画面を生成する - * */ function generate_quiz_content() { var ins = '

' + quizData['quiz'][currentQuizNo]['q'] + '

'; @@ -131,7 +146,7 @@ document.querySelector('.js-quiz-content').innerHTML = ins; } -/** +/* * 回答・解説画面を生成する * @param {number} choice - 選択した回答番号 */ @@ -139,11 +154,11 @@ var ins = '

' + quizData['quiz'][currentQuizNo]['q'] + '

'; // 正解の場合 if(quizData['quiz'][currentQuizNo]['correct'] === choice) { - ins += '

正解!!

'; + ins += '

正解!!

'; correctCount++; // 不正解の場合 } else { - ins += '

不正解…

'; + ins += '

不正解…

'; } ins += '

' + quizData['quiz'][currentQuizNo]['commentary'] + '

'; // 未回答の問題がある場合 @@ -161,9 +176,8 @@ document.querySelector('.js-quiz-content').innerHTML = ins; } -/** +/* * 結果画面を生成する - * */ function generate_result_content() { var ins = '

' + (currentQuizNo+1) + '問中' + correctCount + '問正解

';