我有触发器工作,我可以添加项目到表单。但是,如果我尝试以任何方式更改项目的选项,我会在.setChoices行收到“无法编辑表单”错误。
var form = FormApp.openById("<your form id>");
var mci = form.getItems(FormApp.ItemType.MULTIPLE_CHOICE);
var item = mci[0].asMultipleChoiceItem();
Logger.log(item.getChoices()[0].getValue());
item.setChoices([
item.createChoice('Cats'),
item.createChoice('Dogs')
]);
我已经检查过,多选项存在,我可以记录正确的当前选择值。我也试过
item.setChoiceValues(['Cats', 'Dogs']);
和
var choices = [];
choices.push(item.createChoice('Cats'));
choices.push(item.createChoice('Dogs'));
item.setChoices(choices);
结果相同。
目标: 如果受访者在多项选择项中输入“其他”选项,请将该选项添加到下一个受访者的选择列表中。
答案 0 :(得分:0)
我确信使用Google表单无法做到这一点。
仍然可以使用Google App Scripts创建您自己的用户界面并与您自己的数据存储库进行交互。
答案 1 :(得分:0)
我能够使下面的代码工作,虽然有一个独立的而不是容器绑定的脚本。这也必须手动运行而不是通过触发器运行。
像这样的黑客可以很容易地修改和触发,以便每分钟运行一次,从已知的电子表格中添加选择到已知的表单。
此代码需要包含多选对象的先前表单,并将其ID插入变量id。
function AddNewChoiceBug() {
//choice variable needs input before running.
try{
//get form via ID
var id = ''; //insert form ID here
var form = FormApp.openById(id);
Logger.log( 'Opening form: %s', id );
//get all multiple choice objects in given form
var MultChoice = form.getItems(FormApp.ItemType.MULTIPLE_CHOICE);
//input choice to all multiple choice objects
var choice = 'new choice'; //insert new choice here
Logger.log( 'New choice: %s', choice );
//Iterate through Multiple Choice objects in opening form
for( i=0; i< MultChoice.length; i++ ){
Logger.log( 'Multiple Choice Question titled, %s', MultChoice[i].getTitle() );
var knownChoices = MultChoice[i].asMultipleChoiceItem().getChoices();
//Iterate through Multiple Choice Options for each object
if( catchMultChoices( knownChoices, choice ) != null ){
knownChoices.push( MultChoice[i].asMultipleChoiceItem().createChoice(choice) );
//The following creates an error:
MultChoice[i].asMultipleChoiceItem().setChoices( knownChoices );
}
}
}
catch (e){
Logger.log( e );
}
}
function catchMultChoices(kc, c) {
for( j=0; j< kc.length; j++ ){
//catch choices already in MultChoice[i]
if( kc[j].getValue() === c ){
Logger.log( ' choice %s already a choice', c);
return null;
}
}
//choice not in MultChoice[i]!
Logger.log( ' choice %s not in question!', c);
return c;
}