我正在向GoogleForm MultipleChoiceItem项目添加多项选择的问题。
发生的情况是,有时某些选择没有通过item.setChoices(choices)
api附加到项目。因此,我尝试删除了这些不良项,以避免在表单中显示出来,但没有成功。
这是主要功能
function createQuestions() {
var form = FormApp.getActiveForm();
form.setShuffleQuestions(false); // true to shuffle responses
form.setProgressBar(true); // set progress bar
form.setShowLinkToRespondAgain(true); // show respond again
form.setLimitOneResponsePerUser(false); // true to limit one response per respondent
form.setPublishingSummary(true); //show summary
form.setConfirmationMessage(App.Help.Thankyou);
//form.setAcceptingResponses(false); // wait until is form is ready
// clean up
var items = form.getItems();
for (var i=0; i<items.length; i++) {
try {
form.deleteItem(i);
} catch(ex) {}
}
// import lyrics snippet from dataset
var dataset=importFromDataset();
// populate random sample items
var questionItems=[], sampleItems=[];
var randomSample='';
while(sampleItems.length < App.Settings.NumSamples) {
var newSample='', newItem;
while(newSample==randomSample || newSample=='') { // get new sample
newItem = randomElement(dataset);
newSample = newItem[App.DS.GoldColumn]; //text
}
randomSample=newSample;
sampleItems.push(newItem);
}
// create question items
for(var i=0;i<sampleItems.length;i++) {
var sampleItem=sampleItems[i];
if( sampleItem[App.DS.GoldColumn] == '') continue;
if( sampleItem[App.DS.IndexColumn] == '') continue;
var item = FormApp.getActiveForm().addMultipleChoiceItem();
item.setRequired(true);
item.setTitle( sampleItem[App.DS.GoldColumn] );
item.setHelpText("["+sampleItem[App.DS.IndexColumn]+"]"); // [id] placeholder
// add label choices
var choices=App.DS.labels.map(function(label) {
return item.createChoice(label);
});
item.setChoices(choices);
Logger.log("item:%s choices:%s",item.getHelpText(),item.getChoices().length);
if( item.getChoices().length < 1 || item.getHelpText() == '') {
Logger.log("item:%s removed!",item.getId());
try {
form.deleteItem(item);
} catch(ex) {}
}
questionItems.push(item);
}
Logger.log("loaded samples:%s questions:%s",sampleItems.length, questionItems.length);
// form can accept responses
//form.setAcceptingResponses(true);
}//createQuestions
我用importFromDataset
填充CSV中的数据,并填充了从dataset
中选择随机值并最终跳过重复项的问题。由于某种原因,我不理解后一个日志给出的
[18-11-08 18:02:00:036 CET] item:[24232] choices:5.0
[18-11-08 18:02:01:778 CET] item: choices:5.0
[18-11-08 18:02:03:625 CET] item:[11967] choices:5.0
[18-11-08 18:02:05:225 CET] item:[1670] choices:5.0
[18-11-08 18:02:06:699 CET] item: choices:1.0
[18-11-08 18:02:08:273 CET] item:[4115] choices:5.0
[18-11-08 18:02:10:101 CET] item:[14980] choices:5.0
[18-11-08 18:02:11:873 CET] item: choices:5.0
[18-11-08 18:02:13:548 CET] item:[3420] choices:1.0
[18-11-08 18:02:15:550 CET] item:[5665] choices:5.0
[18-11-08 18:02:15:551 CET] loaded samples:10.0 questions:10.0
因此有些项只有1
选项,而数组映射应该在5
的字符串常量数组中添加App.DS.labels
,从而引起问题。