Google 脚本(表单)- 无法将数组转换为选择[]
Google Script (Forms) - Cannot Convert Array to Choice[]
我已经研究了这个问题一段时间,但尚未找到合适的答案(大多数涉及切换到 setChoiceValues() 而不是解决 "Cannot convert Array to Choice[]" 问题与 setChoices([]))。
在尝试通过 Google 脚本生成表单部分和问题时,我 运行 遇到了无法让我的答案选择转到基于用户答案的特定页面的问题。这似乎是 setChoiceValues() 和 setChoices([]) 之间的区别,据我所知后者允许页面导航。
然而,当我尝试将我的新选择数组放入 setChoices([]) 时,我收到错误消息 "Cannot convert Array to Choice[]"。否则我的代码工作正常,但我需要使用 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]);
}
您的 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);
}
检查循环中日期[curr_date].t.length 的值 我不确定您是如何构建数组的。
您不能在 choiseItem 数组的元素内添加分页符
我已经研究了这个问题一段时间,但尚未找到合适的答案(大多数涉及切换到 setChoiceValues() 而不是解决 "Cannot convert Array to Choice[]" 问题与 setChoices([]))。
在尝试通过 Google 脚本生成表单部分和问题时,我 运行 遇到了无法让我的答案选择转到基于用户答案的特定页面的问题。这似乎是 setChoiceValues() 和 setChoices([]) 之间的区别,据我所知后者允许页面导航。
然而,当我尝试将我的新选择数组放入 setChoices([]) 时,我收到错误消息 "Cannot convert Array to Choice[]"。否则我的代码工作正常,但我需要使用 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]);
}
您的 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);
}
检查循环中日期[curr_date].t.length 的值 我不确定您是如何构建数组的。 您不能在 choiseItem 数组的元素内添加分页符