I'm trying to control my GForms using AppScript and would like to delete and fill the dropdown fields using a script. I've written the following scripts for this and stored them in the GForms file. The scripts work without an error message, but I can see success in the form. I've made a GForms publicly available here: Link
Who has a tip for me where I made a mistake?
Thanks in advance.
For Delete
function deleteDropdownValues() {
var formId = "1zf8tNDLHQZwFQnLWTFYTrx_X4k_ESLrM5kqUdCqz6GA";
var itemId = "2015071815"; // ID des Dropdown-Feldes
var form = FormApp.openById(formId);
var item = form.getItemById(itemId);
if (item.getType() === FormApp.ItemType.MULTIPLE_CHOICE) {
item.asMultipleChoiceItem().setChoices([]);
Logger.log('Dropdown-Werte erfolgreich gelöscht.');
} else {
Logger.log('Das angegebene Item ist kein Dropdown-Element.');
}
}
To add the values to the DropDown-Field
function addOptionToDropdown() {
var formId = "1zf8tNDLHQZwFQnLWTFYTrx_X4k_ESLrM5kqUdCqz6GA";
var itemId = "2015071815"; // ID des Dropdown-Feldes
var newOption = "XXXX";
// Form and Item abrufen
var form = FormApp.openById(formId);
var item = form.getItemById(itemId);
// Add new values
item.asListItem().createChoice(newOption);
item.asListItem().createChoice("sssssss");
item.asListItem().createChoice("eeeeeeeee");
}
You may use this modified version of your script that should work with what you'd like to do.
For deleting drop-down values:
function deleteDropdownValues() {
var formId = "1zf8tNDLHQZwFQnLWTFYTrx_X4k_ESLrM5kqUdCqz6GA";
var itemId = "2015071815";
var form = FormApp.openById(formId);
var item = form.getItemById(itemId);
if (item.getType() === FormApp.ItemType.LIST) {
item.asListItem().setChoiceValues([""]);
Logger.log('Dropdown-Werte erfolgreich gelöscht.');
} else {
Logger.log('Das angegebene Item ist kein Dropdown-Element.');
}
}
I changed
FormApp.ItemType.MULTIPLE_CHOICE
toFormApp.ItemType.LIST
since it's item type is aLIST
andsetChoices([])
tosetChoiceValues([""])
since for some reason it's giving out errors such asException: Array is empty: choices
andException: The parameters (number[]) don't match the method signature for FormApp.ListItem.setChoices.
For adding values in the drop-down field:
function addOptionToDropdown() {
var formId = "1zf8tNDLHQZwFQnLWTFYTrx_X4k_ESLrM5kqUdCqz6GA";
var itemId = "2015071815"; // ID des Dropdown-Feldes
var newOption = ["Option 1", "sssssss", "eeeeeeeee"];
// Form and Item abrufen
var form = FormApp.openById(formId);
var item = form.getItemById(itemId);
// Add new values
item.asListItem().setChoiceValues(newOption);
}
I changed the
createChoice()
's tosetChoiceValues(newOption)
on this script and you may just put your options in thenewOption
array.