I am trying to build my first Google Form from a Google Sheets, but I am stuck with something. What I am trying to do is add choices to a MultipleChoiceItem but dynamically because I can’t know the number of choices before hand.
I have try this code and works:
//This Works
var preguntaQuienEres = form.addMultipleChoiceItem();
preguntaQuienEres.setTitle('¿Quién eres?').isRequired();
preguntaQuienEres.setChoices([
preguntaQuienEres.createChoice(nombresAlumnos[0], form.getItemById(saltosDeSeccion[0]).asPageBreakItem()),
preguntaQuienEres.createChoice(nombresAlumnos[1], form.getItemById(saltosDeSeccion[0]).asPageBreakItem()),
])
But, if I try to add single options, It just delete everything and set the last choice.
//This doesn't work
preguntaQuienEres.setChoices([
preguntaQuienEres.createChoice(nombresAlumnos[0], form.getItemById(saltosDeSeccion[0]).asPageBreakItem()),
])
preguntaQuienEres.setChoices([
preguntaQuienEres.createChoice(nombresAlumnos[1], form.getItemById(saltosDeSeccion[0]).asPageBreakItem()),
])
Which kind of make sense, but I not able to control it.
So, it looks like I have to build a list of choices, and then setChoices(list), but It doesn’t work. I have tried many many things with examples in the forum, but still not working.
My last approach was this:
nombresAlumnos: has names of students.
saltosDeSeccion: has IDs for PageBreaks.
var preguntaQuienEres = form.addMultipleChoiceItem();
preguntaQuienEres.setTitle('¿Quién eres?').isRequired();
var choices = [];
for (var i = 0; i < nombresAlumnos.length; i++) {
choices.push(preguntaQuienEres.createChoice(nombresAlumnos[i], form.getItemById(saltosDeSeccion[i]).asPageBreakItem()));
}
preguntaQuienEres.setChoiceValues(choices);
For example, I have tried this post but I can’t find asMultipleChoiceItem in the docs anymore.
I do not understand this one.
Thanks so much in advance for the help.
Alberto Gonzalez Berrio is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.