google-apps-scriptgoogle-formsgoogle-classroomgoogle-form-quiz

Copying item .getFeedbackForCorrect between Google quiz forms


The .duplicate() method does not work between forms (well not as far as I can see) so I have written a quick routine to copy multiple-choice items from one form to another.

(I'm doing this to set quizzes in Classroom. I have banks of questions on single topics in quizzes for formative assessment and want to combine random questions from these for summative assessment.)

The code I've written works fine in copying over the questions but not the feedback. This is my first GAS code so apologies!

TypeError: Cannot find function getFeedbackForCorrect in object Item. (line 32, file "Code")

function copyMultipleChoiceItem(item1, item2) {
//  copies MC question item1 to item2 - tested PDW 17/05/20
//  copy of feedback not working
//
//  basic question items
    item2.setTitle(item1.getTitle());
    item2.setPoints(item1.asMultipleChoiceItem().getPoints());
    item2.setRequired(item1.asMultipleChoiceItem().isRequired());
    item2.setHelpText(item1.getHelpText());
//  the choices
    var choices = item1.asMultipleChoiceItem().getChoices();
    for (var i = 0; i < choices.length; i++) {
        item2.createChoice(choices[i].getValue(),choices[i].isCorrectAnswer());
    }
    item2.setChoices(choices);
//  the feedback and links - cannot get this to work!
    var feedback1 = item1.getFeedbackForCorrect();
    var feedback2 = FormApp.createFeedback()
        .setDisplayText(feedback1.getText());
//  then do list of URLs
    item2.setFeedbackForCorrect(feedback2);
}

Solution

  • You need to get item1 as a multiple choice item. The getFeedbackForCorrect method is not available on an item. You could create a new variable for the item as a multiple choice item.

    var itemAsMultipleChoice;
    
    itemAsMultipleChoice = item1.asMultipleChoiceItem();//Get item as multiple choice item
    

    Then use the new itemAsMultipleChoice variable;

    var feedback1 = itemAsMultipleChoice.getFeedbackForCorrect();
    

    Here is the entire function:

    function copyMultipleChoiceItem(item1, item2) {
    //  copies MC question item1 to item2 - tested PDW 17/05/20
    //  copy of feedback not working
    //
    //  basic question items
        var itemAsMultipleChoice;
    
        Logger.log('item1: ' + item1)
    
        itemAsMultipleChoice = item1.asMultipleChoiceItem();
    
        item2.setTitle(item1.getTitle());
        item2.setPoints(item1.asMultipleChoiceItem().getPoints());
        item2.setRequired(item1.asMultipleChoiceItem().isRequired());
        item2.setHelpText(item1.getHelpText());
    //  the choices
    
        var choices = item1.asMultipleChoiceItem().getChoices();
        for (var i = 0; i < choices.length; i++) {
            item2.createChoice(choices[i].getValue(),choices[i].isCorrectAnswer());
        }
        item2.setChoices(choices);
    
        var feedback1 = itemAsMultipleChoice.getFeedbackForCorrect();
    
        var feedback2 = FormApp.createFeedback()
            .setDisplayText(feedback1.getText());
    //  then do list of URLs
        item2.setFeedbackForCorrect(feedback2);
    }