Copying item .getFeedbackForCorrect between Google quiz forms

主宰稳场 提交于 2020-07-30 07:02:53

问题


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);
}

回答1:


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);
}


来源:https://stackoverflow.com/questions/44094496/copying-item-getfeedbackforcorrect-between-google-quiz-forms

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!