2014-01-28 1518 views
1

我有觸發器的工作,我可以添加項目的形式。但是,如果我嘗試以任何方式更改項目的選項,我會在.setChoices行收到「無法編輯表單」錯誤。如何在表單提交後修改Google表單?我收到「無法編輯表單」。

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

我確信使用Google表單無法做到這一點。

它仍然可以使用Google App腳本來創建自己的用戶界面並與您自己的數據存儲庫進行交互。

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