Tạo Google Form Quiz

Tạo một Form mới, Make Quiz, đặt “anyone can edit”

Tạo một file csv có các cột theo thứ tự: Question, Option 1, 2, 3, 4, Answer

Mở App script trên google form, copy đoạn mã sau:

function importCSVToGoogleForm() {
  var formUrl = 'YOUR_FORM_URL';  // Replace with your Google Form URL
  var form = FormApp.openByUrl(formUrl);
  
  var csvFile = DriveApp.getFilesByName('YOUR_CSV_FILE_NAME.csv');  // Replace with your CSV file name
  if (csvFile.hasNext()) {
    var file = csvFile.next();
    var content = file.getBlob().getDataAsString();
    var rows = Utilities.parseCsv(content);
    
    // Loop through each row in the CSV
    for (var i = 1; i < rows.length; i++) {  // Skip header row
      var question = rows[i][0];  // First column is the question
      var options = rows[i].slice(1, -1);  // All but the last column are options
      var correctAnswer = rows[i][rows[i].length - 1];  // Last column is the correct answer

      // Add the question to the form
      var item = form.addMultipleChoiceItem();
      item.setTitle(question)
          .setChoices(options.map(function(option) {
            return item.createChoice(option, option === correctAnswer);  // Mark correct answer
          }));
    }
  } else {
    Logger.log('File not found');
  }
}

Steps to Use the Updated Script

  1. Replace Placeholders:
    • Replace YOUR_FORM_URL with your Google Form edit URL.
    • Replace YOUR_CSV_FILE_NAME.csv with the name of your CSV file containing questions and options.
  2. Save and Run:
    • Save the script and run the importCSVToGoogleForm function.
    • Ensure the CSV file is uploaded to Google Drive.
  3. Authorize:
    • If prompted, authorize the script to access your Google Drive and Google Forms.

Important Notes

  • The correct answer should match exactly one of the options provided.
  • If there are any duplicates in options, they will still be treated as separate choices, but the correct answer will be marked correctly.

This setup should now work to import questions with the correct answer specified for each question. Let me know if you need any more help!