rica01
rica01

Reputation: 69

Google app script to update Form from Sheet

I am trying to fill a Google Form multiple choice with a row of data from a Google Sheet. I can get the row text into an Array, but somehow when I use the method .setChoiceValues(), I get a:

Exception: Invalid conversion for item type: TEXT. (line 36, file "PopulateBudgetCategories")

According to Google documentation, such method receives a String[], but fails, throwing the same expection, even with the following code:

studentNames = ['🚗⛽ Car', '🚑 Healthcare', '📱 Mobile phones'];
formExpenseTypes.asListItem().setChoiceValues(studentNames);

Has anyone got an idea of how to fix this?

Thanks.

Upvotes: 0

Views: 630

Answers (2)

rica01
rica01

Reputation: 69

sorry it was all a mistake by me. In one of hte edits I think the form control's ID's changed order and I was actually trying to add Choices to a Text question.

Upvotes: 0

ziganotschka
ziganotschka

Reputation: 26796

Your error implies that formExpenseTypes is an item of type TEXT

  • asListItem() means that you are retrieving an item as a dropdown question, however if your question was not a dropdown question when you created it
  • As of now, it is not possible to convert item types with Apps Script
  • There is a feature request for it on Google's Public Issue Tracker that you can "star" if you are interested
  • In the mean time - you need to create an item as the type of item you want it to be
  • If you desire to change the item type programmatically - you need to delete the existing item and create a new one
  • If you want to create choices for a multiple choice item (opposed to a dropdown item!), you need to retrieve it as asMultipleChoiceItem(), not as asListItem()

Sample:

function myFunction() {
  var form = FormApp.getActiveForm();
  form.addMultipleChoiceItem();
  var items = form.getItems();
  var formExpenseTypes = items[items.length-1];
  studentNames = ['🚗⛽ Car', '🚑 Healthcare', '📱 Mobile phones'];
  formExpenseTypes.asMultipleChoiceItem().setChoiceValues(studentNames);
}

Upvotes: 1

Related Questions