【问题标题】:Google Script (Forms) - Cannot Convert Array to Choice[]Google 脚本(表单)- 无法将数组转换为选项 []
【发布时间】:2017-02-28 21:22:57
【问题描述】:

我已经研究了一段时间,但还没有找到合适的答案(大多数涉及切换到 setChoiceValues(),而不是使用 setChoices([]) 解决“无法将数组转换为 Choice[]”问题) .

在尝试通过 Google 脚本生成表单部分和问题时,我遇到了一个问题,即我的答案选择无法根据用户的答案转到特定页面。这似乎是 setChoiceValues() 和 setChoices([]) 之间的区别,后者尽可能地允许页面导航。

但是,当尝试将我的新选项数组放入 setChoices([]) 时,我收到错误消息“无法将数组转换为选项 []”。否则我的代码工作正常,但我需要使用 setChoices([]) (似乎)以获得我想要的页面导航。

如何将值循环到数组或其他容器中,并使它们显示为 Choices[] 对象?我怎样才能使这样的工作?看起来它应该比现在容易得多,但我看不到解决方案。

下面是导致问题的一段代码:

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

【问题讨论】:

    标签: javascript arrays google-apps-script google-forms


    【解决方案1】:

    在构建 MultipleChoise 对象时存在一些小问题:

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

    检查循环内 dates[curr_date].t.length 的值我不确定你是如何构造数组的。 您不能在 choiseItem 数组的元素中添加分页符

    【讨论】:

    • 为了澄清:日期是一个对象数组。 dates[0] 具有元素 d(单个日期作为字符串,例如:“3/1/17”)和 t(时间数组作为字符串,例如:[“6:30 AM - 7:00 AM”等。 ..])。嵌套的 while 循环用于生成每个日期的时间列表,这就是为什么时间 while 循环位于日期 while 循环内的原因。我没有意识到我不能使用“end_break”作为我的跳转页面,因为它正在添加一个新的分页符项目。首先创建分页项,然后将其作为分页项分配给变量后,它开始工作。非常感谢您的帮助!
    猜你喜欢
    • 1970-01-01
    • 2021-06-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多