Google apps script 提交表单后如何修改谷歌表单?我收到;未能编辑表单。”;
我让触发器工作,我可以向表单中添加项。但是,如果我试图以任何方式更改项目的选择,我会在.setChoices行收到“编辑表单失败”错误Google apps script 提交表单后如何修改谷歌表单?我收到;未能编辑表单。”;,google-apps-script,google-forms,Google Apps Script,Google Forms,我让触发器工作,我可以向表单中添加项。但是,如果我试图以任何方式更改项目的选择,我会在.setChoices行收到“编辑表单失败”错误 var form = FormApp.openById("<your form id>"); var mci = form.getItems(FormApp.ItemType.MULTIPLE_CHOICE); var item = mci[0].asMultipleChoiceItem(); Logger.log(item.getChoic
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')
]);
及
同样的结果
目标是:
如果受访者在多项选择项中输入了“其他”选项,请将该选项添加到下一位受访者的选项列表中。我确信,使用谷歌表单无法做到这一点
仍然可以使用Google应用程序脚本创建您自己的用户界面,并与您自己的数据存储库交互。我能够使以下代码正常工作,尽管有一个独立的脚本,而不是容器绑定的脚本。这也必须手动运行,而不是通过触发器 这样的黑客可以很容易地修改和触发,使其每分钟运行一次,将已知电子表格中的选项添加到已知表单中 此代码需要包含多项选择对象的上一个表单,其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;
}
函数AddNewChoiceBug(){
//选择变量在运行前需要输入。
试一试{
//通过ID获取表单
变量id=“”;//在此处插入表单id
var form=FormApp.openById(id);
Logger.log('期初表单:%s',id);
//获取给定形式的所有多项选择对象
var MultChoice=form.getItems(FormApp.ItemType.MULTIPLE\u-CHOICE);
//将选项输入到所有多项选择对象
var choice='new choice';//在此处插入新选项
Logger.log('新选项:%s',选项);
//以开始形式遍历多项选择对象
对于(i=0;i
var choices = [];
choices.push(item.createChoice('Cats'));
choices.push(item.createChoice('Dogs'));
item.setChoices(choices);
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;
}