通过 Apps 脚本转换 Google Form ItemType

Conversion of a Google Form ItemType via Apps Script

我在 Google 表格上有一个用于销售东西的表格。我知道这不是最好的解决方案,但我的新客户已经在使用它,所以现在不值得改变,我们正计划进一步这样做。

代码现在做什么?

我设置了一个 sheet,我在其中存储了有关产品的信息,例如名称、价格和可用数量。当有人在表单中提交回复时,脚本会接受那个人的命令并更新表单的 sheet 和 "front-end"。该表格由 ScaleItem 问题组成,从 0 到 6,除非可用数量低于 6。发生这种情况时,代码会更改比例范围,因此不可能超额预订。

问题是:

当可用库存为 1 或 2 时,我需要更改 ItemType,因为 ScaleItem 要求至少有 4 个数字(0 到 3)。

我查看了文档,google,甚至在这里,也没有找到切换 ItemType 的解决方案。

这是我目前的代码,原样。我正在尝试替换该项目,但这不是好的解决方案,因为它会在表单响应 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)
}

感谢任何帮助。

无法转换 FormApp.ItemType

如果有帮助,ScaleItem 所需的最少选项数是两个而不是四个(1 到 2)。

否则,为什么不首先将项目声明为不同类型,而不是进行转换?如果多项选择对于大数字来说太大了,也许你可以将 ItemType 设置为 List?