function doGet() {
const htmlOutput = HtmlService.createHtmlOutputFromFile('Index')
.setTitle('AI 命題系統')
.setFaviconUrl('https://www.google.com/favicon.ico');
return htmlOutput;
}
步驟:
以下是一些性能與功能優化的建議:
function optimizedGenerateQuiz() {
const sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
const lastRow = sheet.getLastRow();
const lastQuestion = sheet.getRange(lastRow, 1).getValue();
if (lastQuestion) {
Logger.log("使用緩存的問題:" + lastQuestion);
return;
}
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);
sheet.appendRow([question.question, ...question.options, question.answer]);
Logger.log("題目已生成並儲存到試算表");
}
步驟: