2016-09-11 68 views
0

我想創建一個選擇題測驗,這需要從一個問題銀行的問題,使用GOOGLE表格和結果存儲在電子表格中。我已經知道如何創建測驗並將數據存儲在電子表格中,但我不知道如何使用問題庫。誰能幫我這個?從谷歌表單創建一個多選題測驗

在此先感謝。

+0

你是什麼意思的「問題銀行」?電子表格? –

回答

0

我很感興趣這個話題太,但我是一個完整的新手。 從現有的代碼中獲得靈感,我編寫了一個簡單的腳本,令我非常驚訝,似乎工作。 我在下面描述它,但請記住,這篇文章更多是問題而不是答案。我希望得到更有經驗的程序員的意見,因爲我不確定我做的一切都是正確的。

因此,在我的簡單方案中,問題銀行看起來像like this(請製作您自己的副本)。它應該是不言而喻的,但是

列A:Id(迄今未使用,但可能在未來有用);

B欄:應包含問題的文本(迄今只有像Q1,Q2等的佔位符)

柱C:爲每個問題

列d多種選擇號碼:所述正確的選擇(它的訂單號碼在下面的列中)

E列和以下:C列中的多個選項的文本(同樣,目前只有佔位符A3Q2是問題2的答案3 );

腳本如下。我在裏面寫了很多評論。 基本上它會通過名稱「QBANK_1_form」創建一個表單,其中包含電子表格中列出的多項選擇題的子集。 表單在變量destinationFolder指定的文件夾中創建。該腳本確保沒有重複的文件(我在調試時得到了很多這些文件)。

再次,我真的很想從更嚴肅的程序員那裏獲得一些見解。這是我第一次嘗試,可能很笨拙。我甚至對它的工作感到驚訝。

function myFunction() { 
    // Id of the spreadsheet containing the question bank 
    var questionBankId = "1QCO-W2PxR9sLf2HtXreaEslyGTBdjswTgqxWDFycgKc"; 
    // name of the output form 
    var formName = "QBANK_1_form"; 
    // Id of the destination folder (not root to keep things tidy) 
    var destinationFolder = "ID_OF_YOUR_FOLDER_HERE"; // <-- change accordingly 
    // variable containg the total number of questions in the question bank (read from spreadsheet) 
    var question_num; 
    // number of questions in the output form (question_req <= question_num) 
    var question_req = 3; // chosen programmatically for the moment 
    // other variables 
    var r; 
    var c; 
    var ans; 
    var item; 
    var content; 
    var choices =[]; 
    var nchoices; 
    var correct; 
    var iscorrect; 

    // ====================================================================================== 
    // Delete possible files by the same name from target folder, to avoid duplicates 
    // ====================================================================================== 
    // get target folder by ID 
    var myFolder = DriveApp.getFolderById('0Bw56O_ircsfpSWM2N2FQbm1fUWM'); 
    // check if other files with the chosen name exist in the folder, 
    var thisFile = myFolder.getFilesByName(formName); 
    // if this is the case, iterate over them 
    while (thisFile.hasNext()) { 
    // get next file 
    var eachFile = thisFile.next(); 
    // get its Id 
    var idToDLET = DriveApp.getFileById(eachFile.getId()); 
    // delete the file 
    DriveApp.getFolderById(destinationFolder).removeFile(idToDLET); 
    } 
    // -------------------------------------------------------------------------------------- 
    // ======================================================================================  
    // CREATE AND POPULATE THE FORM  
    // ====================================================================================== 
    // -------------------------------------------------------------------------------------- 
    // create the form 
    var form = FormApp.create(formName); 
    // ======================================================================================  
    // move the form to the desired folder (just for the sake of "housekeeping")  
    // ====================================================================================== 
    // get Id of the file 
    var formFile = DriveApp.getFileById(form.getId()); 
    // add the to the desired folder 
    DriveApp.getFolderById(destinationFolder).addFile(formFile); 
    // delete the file from the roor folder 
    DriveApp.getRootFolder().removeFile(formFile); 
    // ======================================================================================  
    // start populating the form 
    // ====================================================================================== 
    // open spreadsheet containing the question bank 
    var ss = SpreadsheetApp.openById(questionBankId); 
    // get number of questions in the question bank (number of rows-1); 
    var sheet = ss.getSheets()[0]; 
    question_num = sheet.getLastRow() - 1; 
    // if question_req > question_num then set question_req=question_num, and the following is just a reshuffling of 
    // the questions in the question bank 
    if(question_req>question_num) { 
    question_req=question_num; 
    } 
    // get random indexes for the question subset 
    var index = questions(question_num,question_req); 

    // Make sure the form is a quiz. 
    form.setIsQuiz(true); 


    // iteration of reading spreadsheet and writing form accordingly 
    for(r = 0; r<question_req ; r++){ 
    // create new multiple choice question 
    item = form.addMultipleChoiceItem();  
    // set value if correct 
    item.setPoints(10); 
    // get question text from question bank and write it in form 
    content = sheet.getRange(index[r]+1,2).getValue(); 
    item.setTitle(content); 
    // get number of choices from question bank 
    nchoices = sheet.getRange(index[r]+1,3).getValue(); 
    // get position of correct choice from question bank 
    correct = sheet.getRange(index[r]+1,4).getValue(); 
    // create choice array 
    for(c = 1; c<nchoices+1;c++){ 
     // determine whether the choice is correct 
     iscorrect = (c==correct); 
     // read choice from question bank 
     content = sheet.getRange(index[r]+1,c+4).getValue(); 
     // add choice to choice array 
     choices[c-1] = item.createChoice(content,iscorrect);   
    } 
    // set choices 
    item.setChoices(choices); 

    } 
} 
// ------------------------------------------------------------------------------ 
// this function extracts question_req unique integers between 1 and question_num 
// ------------------------------------------------------------------------------ 
function questions(question_num,question_req){ 
    var index = []; 
    var ilist = []; 
    var i; 
    var n; 


    // create and populate index list 
    for (i = 0; i < question_num; i++) { 
     ilist[i]=i+1; 
    } 
    // create indexes of random questions 
    i = 0; 
    while(i<question_req){ 
     n = Math.floor(Math.random() * (question_num-1)); 
     if(ilist[n]==n+1){ 
     index[i]=n+1; 
     ilist[n]=-1; 
     i++; 
     } 
    } 
    return index; 
} 
相關問題