Copying Images between Google Form quizzes

2019-09-16 13:49发布

问题:

I am trying to copy Google quiz items from one Form to another, which appeared to have been completely successful. However, I forgot that some of these items have images attached to them.

Here's a link to an example quiz.

There's nothing documented to suggest how to access these, but I have tried getImage both from the Item and the Item.asMultipleChoiceItem but neither is recognised. It's just the last 2 lines of code that aren't working.

I get

TypeError: Cannot find function getImage in object Item

or

TypeError: Cannot find function getImage in object MultipleChoiceItem

function copyMultipleChoiceItem(item1, item2) {
//  copies MC question item1 to item2 - tested PDW 17/05/20
//  copy of feedback now working - tested PDW 17/05/30
//
    var item1MC = item1.asMultipleChoiceItem();
//  basic question items
    item2.setTitle(item1.getTitle());
    item2.setHelpText(item1.getHelpText());
    item2.setPoints(item1MC.getPoints());
    item2.setRequired(item1MC.isRequired());

//  the choices
    var choices = item1MC.getChoices();
    for (var i = 0; i < choices.length; i++) {
        item2.createChoice(choices[i].getValue(), choices[i].isCorrectAnswer());
    }
    item2.setChoices(choices);
//  the feedback
    var feedback1 = item1MC.getFeedbackForCorrect();
    item2.setFeedbackForCorrect(feedback1);
    var feedback1 = item1MC.getFeedbackForIncorrect();
    item2.setFeedbackForIncorrect(feedback1);
//  the image
    var image1 = item1.getImage();
    item2.setImage(image1);
}

Here's a picture of the image which is attached to the MultipleChoiceItem, not an ImageItem in its own right:

回答1:

Make sure your item1 is an ImageItem, I've made a test script base on your code. Here is the snippet:

function myFunction() {
   // Create and open a form.
 var newForm = FormApp.create('Form Name');
 var img = UrlFetchApp.fetch('https://www.google.com/images/srpr/logo4w.png');

 var pageTwo = newForm.addPageBreakItem().setTitle('Page Two');
 var newImage = newForm.addImageItem()
     .setTitle('Google')
     .setHelpText('Google Logo') // The help text is the image description
     .setImage(img);
 var pageThree = newForm.addPageBreakItem().setTitle('Page Three');
  var img2 = newForm.addImageItem();
  ImageCopy(newImage,img2);
}

function ImageCopy(itemImage,img2){
  var image1 = itemImage.getImage();
  img2.setImage(image1);
}

Here is the result:

Hope this helps.



回答2:

here is the result of the code

if(questionType=='MULTIPLE_CHOICE'){
var img = UrlFetchApp.fetch(www.myWebSite.com/tomato.png);
form.addImageItem()
 .setTitle('Quizz') 
 .setHelpText('Any number off answer')
 .setImage(img);
var item = form.addMultipleChoiceItem();    
item.setChoices([
    item.createChoice('tomato'),
    item.createChoice('pomodoro'),
    item.createChoice('tomate'),
    item.createChoice('tomata'),

]);

It is a bit odd but this is the only way I have found to make the quizz appear the same way as if I had add the image in the quizz(using the form editor without script). Now you have a problem with the spreadsheet with the response, sorry