0
votes

When I add a list item to a Google Form I can set and get choices.

If I select a list item from an existing form I cannot.

items = form.getItems();

for (i=0;i<items.length;i++){
 Logger.log(items[i].getType());
 if (items[i].getTitle() == 'A List'){
    choices = items.getChoices();
 }
}

I can see the items are of type "List" in the log but getChoices throws an error

TypeError: Cannot find function getChoices in object item....

Is this a bug in Apps Script? Is there something I can do to cast the item and make sure it is of the right type?

Thanks

Jeremy

1

1 Answers

1
votes

Please reffer to the documentation here. As you can see an item does not have a .getChoices() method. This method is available in specific types of items, like ListItem. So you need to specifically do something like

choices = items[i].asListItem().getChoices()

Remember that items is an array and you need to specify which item you are getting choices for. You then need to specify what type of item it is (i.e. a list item) and only then you can get the choices. If this is something you need to do for any type of item, then you will need to figure out how to check what kind of item it is and then get it as that item type.