const GEMINI_API_KEY = '你的API密鑰';
function generateAndSaveQuiz() {
const apiUrl = 'https://generativelanguage.googleapis.com/v1/models/gemini-pro:generateContent?key=' + GEMINI_API_KEY;
const payload = {
"prompt": "請生成一個關於基礎數學的選擇題,包括問題、四個選項及正確答案。"
};
const options = {
"method": "post",
"contentType": "application/json",
"payload": JSON.stringify(payload)
};
const response = UrlFetchApp.fetch(apiUrl, options);
const data = JSON.parse(response.getContentText());
const question = JSON.parse(data.candidates[0].content);
const sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
sheet.appendRow([
question.question,
...question.options,
question.answer
]);
Logger.log("題目已生成並儲存到試算表");
}
步驟:
function displayQuizzes() {
const sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
const data = sheet.getDataRange().getValues();
let quizzes = [];
for (let i = 1; i < data.length; i++) {
quizzes.push({
question: data[i][0],
options: data[i].slice(1, 5),
answer: data[i][5]
});
}
Logger.log(JSON.stringify(quizzes, null, 2));
return quizzes;
}
步驟: