Last active
August 27, 2021 08:49
-
-
Save omas/f536e72fa8f61e00535ce7121fd94f3c to your computer and use it in GitHub Desktop.
Google Spread Sheet を用いた QuizGenerator
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Google Form Generator(択一問題) | |
グループ範囲を指定してランダムに出題するフォームを作成するプログラムです | |
以下のようなSpreadを作りこのスクリプトをコピーして貼り付けてmainを実行してください | |
タイトルを書く, 説明文を書く, 出題数, 出題範囲(groupidをカンマ区切りで) | |
groupid, 問題文, 問1の正答, 問1の誤答1, 問1の誤答2, 問1の誤答3 | |
groupid, 問題文, 問2の正答, 問2の誤答1, 問2の誤答2, 問2の誤答3 | |
groupid, 問題文, 問3の正答, 問3の誤答1, 問3の誤答2, 問3の誤答3 | |
- シート名はdataにしてください | |
- 答えはいくつでもOKですが,正答を最初にしてください(出題時にはシャッフルされます) | |
*/ | |
const utils_ = { | |
getToday: d => [ | |
d.getMonth() + 1, d.getDate(), d.getHours(),d.getMinutes()] | |
.map(v => v.toString()) | |
.map(v => v.padStart(2, '0')).join(''), | |
shuffle: ([...array]) => { | |
const randInt = n => Math.trunc(Math.random() * n) | |
for (let i = array.length - 1; i >= 0; i--) { | |
const j = randInt(i + 1) | |
;[array[i], array[j]] = [array[j], array[i]] | |
} | |
return array | |
} | |
} | |
const createForm_ = (title, description) => { | |
const filename = `${title}.${utils_.getToday(new Date())}` | |
const form = FormApp.create(filename) | |
form.setTitle(title) | |
.setDescription(description) | |
.setPublishingSummary(true) | |
.setAllowResponseEdits(true) | |
// .canEditResponse(true) | |
.setRequireLogin(true) | |
.setCollectEmail(true) | |
.setShuffleQuestions(true) | |
.setIsQuiz(true) | |
return form | |
} | |
const createQuestions_ = form => (matrix) => { | |
const createMultipleChoice_ = form => (point, question, ...answers) => { | |
const item = form.addMultipleChoiceItem() | |
.setTitle(question).setPoints(point) | |
item.setChoices(answers.map( | |
(v, i) => item.createChoice(v, i === 0) | |
)) | |
} | |
const point = 100 / matrix.length | |
for (const cols of matrix) { | |
createMultipleChoice_(form)(point, ...cols) | |
} | |
} | |
const main = () => { | |
// const fileId = 'xxxxxxxxxxxxxxxxxxxxxxxxx' | |
// const file = SpreadsheetApp.openById(fileId) | |
const file = SpreadsheetApp.getActiveSpreadsheet() | |
const sheet = file.getSheetByName('data') | |
const data = sheet.getDataRange().getValues() | |
const [title, description, questionNum, questionRange] = data.shift() | |
const form = createForm_(title, description) | |
const questions = utils_.shuffle(data.filter(cols => | |
questionRange.toString().trim().split(',') | |
.includes(cols[0].toString())).map(v => v.slice(1))) | |
.slice(0, questionNum) | |
createQuestions_(form)(questions) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment