Skip to content

Instantly share code, notes, and snippets.

@omas-public
Last active October 14, 2020 08:45
Show Gist options
  • Save omas-public/186798e6dfd0eab1b744e17fd464c9e3 to your computer and use it in GitHub Desktop.
Save omas-public/186798e6dfd0eab1b744e17fd464c9e3 to your computer and use it in GitHub Desktop.
/*
Google Form Generator
以下のようなSpreadを作ってmainを実行してください
- シート名がファイルの名前とタイトルになります
- 答えはいくつでもOKです
- 複数シートの場合該当シート上で関数を実行してください
- [sample](https://docs.google.com/spreadsheets/d/1lDIZ7cicQUhGod4sTqF8XH9rLYCUspy9F78GMGqLRqY/edit?usp=sharing)
細かい説明を書く, point(空白にすると1点,数値を入れるとその点数), 1, 2, 3, 4
問題文, , 正答のインデックス番号, 問1の 答え1, 問1の 答え2, 問1の 答え3, 問1の 答え4
問題文, 5, 正答のインデックス番号, 問2の 答え1, 問2の 答え2, 問2の 答え3, 問2の 答え4
問題文, , 正答のインデックス番号, 問3の 答え1, 問3の 答え2, 問3の 答え3, 問3の 答え4
問題文, 10, 正答のインデックス番号, 問4の 答え1, 問2の 答え2, 問4の 答え3, 問4の 答え4
*/
const createQuestions = form => matrix => {
const judgeType = (v, sep = ',') => {
switch (typeof v) {
case 'number':
return {typefn: 'addMultipleChoiceItem', predfn : (correct) => i => i + 1 === correct }
case 'string':
return {typefn: 'addCheckboxItem', predfn : (correct) => i => correct.toString().split(sep).map(Number).includes((i + 1))}
}
return {typefn: (v) => v, predfn: (v) => v}
}
const createQuestion = (form => ([question, point, correct_index, ...answers]) => {
const type = judgeType(correct_index)
const item = form[type.typefn]().setTitle(question).setPoints(point || 1)
item.setChoices(answers.filter(v => v).map(
(v, i) => item.createChoice(v, type.predfn(correct_index)(i))
))
})(form)
matrix.forEach(createQuestion)
}
function main() {
const sheet = SpreadsheetApp.getActiveSheet()
const title = sheet.getName()
const contents = sheet.getDataRange().getValues()
const [description, point, question, ...rest] = contents.shift()
const form = FormApp.create(title)
form.setTitle(title)
.setDescription(description)
.setRequireLogin(true)
.setCollectEmail(true)
.setShuffleQuestions(true)
.setIsQuiz(true)
const makeSelection = createQuestions(form)
makeSelection(contents)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment