0

I have looking into this issue for a while, but have yet to find a suitable answer (most involve switching to setChoiceValues() rather than addressing the "Cannot convert Array to Choice[]" issue with setChoices([])).

While attempting to generate form sections and questions via Google Script, I ran into the issue of not getting my answer selections to go to specific pages based on the user's answer. This appears to be the difference between setChoiceValues() and setChoices([]), with the latter allowing for page navigation as best as I can tell.

However, when attempting to put my array of new choices into setChoices([]), I get the error message "Cannot convert Array to Choice[]". My code works fine otherwise, but I need to use setChoices([]) (it seems) in order to get the page navigation that I want.

How can I loop values into an array or other container and be able to make them appear as a Choices[] object? How can I make something like this work? It seems like it should be much easier than it is, but I cannot see the solution.

Below is a segment of my code that is causing the issue:

//Form - globally accessible 
var f = FormApp.openById(f_id);
//Date Iterator
var curr_date = 0;
//Time Iterator
var curr_time = 0;
//Array of Times
var Tchoices = [];
//Setting Time choices per date
while(curr_date < dates.length)
{
    Tchoices = [];
    curr_time = 0;
    //dates is an array of objects with both d's (single date) and t's 
    //    (array of times for that date)
    var d = dates[curr_date].d;
    var end_break = f.addPageBreakItem().setTitle("Times for " + d);
    var f_time = f.addMultipleChoiceItem().setTitle(d);
    while(curr_time < dates[curr_date].t.length)
    {               
        end_break = end_break.setGoToPage(FormApp.PageNavigationType.SUBMIT);
        Tchoices.push(f_time.createChoice(dates[curr_date].t[curr_time], end_break).getValue());
        curr_time++;
    } 
    f_time.setChoices([Tchoices]);
}

1 Answer 1

0

There was some minor issues with the building of your MultipleChoise object:

  //Form - globally accessible 
  var f = FormApp.openById('someID');
  //Date Iterator
  var curr_date = 0;
  //Time Iterator
  var curr_time = 0;
  //Array of Times
  var Tchoices = [];
  //Setting Time choices per date
  while(curr_date < dates.length)
  {
      Tchoices = [];
      curr_time = 0;
      //dates is an array of objects with both d's (single date) and t's 
      //    (array of times for that date)
      var d = dates[curr_date].d;
      var end_break = f.addPageBreakItem().setTitle("Times for " + d);
      var f_time = f.addMultipleChoiceItem();
      f.addMultipleChoiceItem().setTitle(d);
      while(curr_time < dates[curr_date].t.length)   //verify this while not sure what you have inside your dates array
      {               
          end_break = end_break.setGoToPage(FormApp.PageNavigationType.SUBMIT);
          Tchoices.push(f_time.createChoice(dates[curr_date].t[curr_time])); //You cannot add a pagebreak inside the elements of a choiseItem array
          curr_time++;
      } 
      Logger.log(Tchoices);
      f_time.setChoices(Tchoices);
  }

Check the values for dates[curr_date].t.length inside the Loop I'm not sure how you constructed the array. You cannot add a pagebreak inside the elements of a choiseItem array

1
  • For clarification: dates is an array of objects. dates[0] has elements d (single date as a string, ex: "3/1/17") and t (array of times as strings, ex: ["6:30 AM - 7:00 AM", etc...]). The nested while loop is to generate the list of times per date, which is why the times while loop is inside of the date while loop. I didn't realize that I couldn't use "end_break" as my go-to-page since it was adding a new Page Break item. After creating the Page Break item first, then assigning it to a variable as a Page Break item, it started working. Thank you very much for your help! Commented Mar 1, 2017 at 13:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.