1
votes

I have a form on Google Forms for selling stuff. I know it's not the best solution for this, but my new customer is using it already, so It's not worthy to change now, we are planning to do this further.

What the code does now?

I have set a sheet where I have stored info about the products, like name, price and available qty. When someone submits a response in the form, the script takes the order from that guy and updates the sheet AND the "front-end" of the form. The form is composed by ScaleItem questions, from 0 to 6, unless the available qty is below six. When this happens, the code changes the bounds of the scale so it's not possible to have overbooking.

The problem is:

When the available stock is 1 or 2, I need to change the ItemType, because the ScaleItem requires to have at least 4 numbers (0 to 3).

I've looked on documentation, google and even here and found no solution for switching the ItemType.

This is the code I have so far, AS IS. I'm trying to substitute the item, but it's not good solution, because it creates a new column in the form response sheet.

var form = FormApp.openById(form_id);
var item = form.getItemById(item_id);
var choices = [];
for (var p = 0; p <= stock; p++) {
  choices.push(p);
}
if (stock <= 2) {
  if (item.getType() === FormApp.ItemType.MULTIPLE_CHOICE) {  
    item.asMultipleChoiceItem().setChoiceValues(choices);
  } else if (item.getType() === FormApp.ItemType.SCALE) {
    //
    // THIS IS WHERE THE ITEM SHOULD GET CONVERTED TO MULTIPLE CHOICE
    // it is provisionally creating a new item
    //
    // item.asMultipleChoiceItem().setChoiceValues(choices); <= NOT VALID CONVERSION ERROR
    var ii = item.getIndex();
    var title = item.getTitle();
    form.deleteItem(item);
    var new_item = form.addMultipleChoiceItem()
      .setTitle(title)
      .setChoiceValues(choices);
    form.moveItem(new_item, ii); // this line gets error - still working on
  }
} else if (stock <= 6) {
  item.asScaleItem().setBounds(0, stock);
} else {
  item.asScaleItem().setBounds(0, 6)
}

Appreciate any help.

1

1 Answers

0
votes

It is not possible to convert a FormApp.ItemType

If it helps, the minimum required number of options for ScaleItem is two and not four (1 to 2).

Otherwise, instead of converting, why do you not declare the item as a different type in the first place? If multiple choice is too bulky for large numbers, maybe you can set the ItemType to List?