I am having trouble using Apps Script to help me make a Google Forms test using Google Sheets data

55 views
Skip to first unread message

Robert Mcleod

unread,
Jun 2, 2025, 7:06:05 AMJun 2
to Google Apps Script Community
Hello...I can't figure out why the following APPS Script won't run. It's a combination of research from studying several YouTube videos, Google inquiries and ChatGPT:

function createQuizFromSheet() {
  var ss = SpreadsheetApp.getActive();
  var sheet = ss.getSheetByName("Favorite Food Items Test"); // Replace "YourSheetName"
  var data = sheet.getDataRange().getValues();

  var form = FormApp.create("Favorite Food Items Test"); // Replace "Your Quiz Title"
  form.setIsQuiz(true);

  for (var i = 1; i < data.length; i++) {
    var question = data[i][0];
    var correctAnswer = data[i][1];
    var choices = [data[i][2], data[i][3], data[i][4], data[i][5]]; // Adjust based on your sheet

    var item = form.addMultipleChoiceItem();
    item.setTitle(question);
    item.setChoices([
      item.createChoice(choices[0]),
      item.createChoice(choices[1]),
      item.createChoice(choices[2]),
      item.createChoice(choices[3])
    ]);

      // Assuming correct answer is in the first column, but it should be
    // in the correct answer column in your spreadsheet.
    var correctChoice = null;
    for (var j = 0; j < choices.length; j++) {
       if (choices[j] == correctAnswer) {
          correctChoice = choices[j];
          break;
       }
    }
    if (correctChoice) {
      // If we find the correct answer in the list of choices,
      // set feedback and points (customize this).
     item.setFeedbackForCorrect(FormApp.QuizFeedback.builder().setText("Correct!").build());
      item.setPoints(25);
    } else {
        // Handle cases where correct answer isn't in the list.
        Logger.log("Correct answer not found for question: " + question);
    }


  }

  Logger.log("Quiz created: " + form.getPublishedUrl());
}

Michael O'Shaughnessy

unread,
Jun 2, 2025, 10:34:53 PMJun 2
to google-apps-sc...@googlegroups.com
Hello Robert,

You made a great effort!! I do believe your issue is with the "FormApp.QuizFeedback.builder()".

I took your idea and "refactored" your code and rolled my own.  Here is a PUBLIC view only sheet that you can make a copy of:

Look at the scripts and you will see there are 2.  The first, "quizCreator" basically just works.  However it is a little "brute force" coding and prone to errors if column headers change, columns rearranged, etc.  Also there is a "preview" function to see what data is actually going to be used.

The second one, "quizCreatorTake2", trys to do a little more error trapping and checking.

Take a look, have fun with it and let me know if you have any questions.

Thanks,
Michael



--
You received this message because you are subscribed to the Google Groups "Google Apps Script Community" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-apps-script-c...@googlegroups.com.
To view this discussion visit https://groups.google.com/d/msgid/google-apps-script-community/1c750ec3-3286-4ffb-bcbe-1a9ddb85931dn%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages