将复选框转换为 Google 表单上的下拉列表
Convert Checkboxes into dropdown list on Googleform
美好的一天,
我有一个 Google 表单问卷,上面有 120 多个问题,它的所有选项都是相同的并且是复选框,有没有办法将复选框转换为下拉列表?这样使用选项卡进行选择会更清晰、更容易。
我目前正在手动进行,但还有很多其他问卷也有同样的问题。
非常感谢,祝你有美好的一天!
是的。这很容易。你看过documentation了吗?您只需使用 FormApp
并使用 FormAp.getItems(FormApp.ItemType.LIST)
即可获取列表中的所有项目。然后您需要做的就是遍历该列表并执行以下操作:
- 获取当前项目作为
LIST
项目(所有项目将在 ITEM
类型)
- 创建一个新的
CHECKBOX
项目
- 将旧项目的标题复制到新项目
- 将帮助文本从旧项目复制到新项目
- 需要从旧项目复制到新项目
- 将选项从旧项目复制到新项目
- 获取新旧项目的索引
- 将新项目移至旧项目索引
- 删除旧项目
虽然我反对给代码,希望你自己写,但太琐碎了,我还是直接给你吧:
编辑:我误读了问题并认为目标是将下拉列表更改为复选框,而不是将复选框更改为下拉列表。编辑代码以反映反向目标
要自己调整代码,真的只需要改几行。 var itemList = form.getItems(FormApp.ItemType.LIST)
根据 enum ItemType 更改,并将 oldItem
和 newItem
更改为 as
和 add
各自的类型。
function myFunction() {
var form = FormApp.getActiveForm()
var itemList = form.getItems(FormApp.ItemType.CHECKBOX)
var oldItem, newItem, oldIndex, i
for (i = 0; i < itemList.length; i++) {
oldItem = itemList[i].asCheckboxItem() //get the item as a checkbox item to gain access to the choices
newItem = form.addListItem() //create a new list item (you can use any other type)
newItem.setTitle(oldItem.getTitle()) //copy the title
newItem.setHelpText(oldItem.getHelpText()) //copy the help text
newItem.setRequired(oldItem.isRequired()) //copy if it is required
newItem.setChoices(oldItem.getChoices()) //copy the choices
oldIndex = oldItem.getIndex() //get where the current item is
form.moveItem(form.getItemById(newItem.getId()), oldIndex) //move the new item to the old items position
// -----Another optional way to move the item----
// var newIndex = newItem.getIndex()
// form.moveItem(form.getItemById(newIndex, oldIndex)
// -----------------------------------------------
form.deleteItem(itemList[i]) //delete the old item
}
}
美好的一天,
我有一个 Google 表单问卷,上面有 120 多个问题,它的所有选项都是相同的并且是复选框,有没有办法将复选框转换为下拉列表?这样使用选项卡进行选择会更清晰、更容易。
我目前正在手动进行,但还有很多其他问卷也有同样的问题。
非常感谢,祝你有美好的一天!
是的。这很容易。你看过documentation了吗?您只需使用 FormApp
并使用 FormAp.getItems(FormApp.ItemType.LIST)
即可获取列表中的所有项目。然后您需要做的就是遍历该列表并执行以下操作:
- 获取当前项目作为
LIST
项目(所有项目将在ITEM
类型) - 创建一个新的
CHECKBOX
项目 - 将旧项目的标题复制到新项目
- 将帮助文本从旧项目复制到新项目
- 需要从旧项目复制到新项目
- 将选项从旧项目复制到新项目
- 获取新旧项目的索引
- 将新项目移至旧项目索引
- 删除旧项目
虽然我反对给代码,希望你自己写,但太琐碎了,我还是直接给你吧:
编辑:我误读了问题并认为目标是将下拉列表更改为复选框,而不是将复选框更改为下拉列表。编辑代码以反映反向目标
要自己调整代码,真的只需要改几行。 var itemList = form.getItems(FormApp.ItemType.LIST)
根据 enum ItemType 更改,并将 oldItem
和 newItem
更改为 as
和 add
各自的类型。
function myFunction() {
var form = FormApp.getActiveForm()
var itemList = form.getItems(FormApp.ItemType.CHECKBOX)
var oldItem, newItem, oldIndex, i
for (i = 0; i < itemList.length; i++) {
oldItem = itemList[i].asCheckboxItem() //get the item as a checkbox item to gain access to the choices
newItem = form.addListItem() //create a new list item (you can use any other type)
newItem.setTitle(oldItem.getTitle()) //copy the title
newItem.setHelpText(oldItem.getHelpText()) //copy the help text
newItem.setRequired(oldItem.isRequired()) //copy if it is required
newItem.setChoices(oldItem.getChoices()) //copy the choices
oldIndex = oldItem.getIndex() //get where the current item is
form.moveItem(form.getItemById(newItem.getId()), oldIndex) //move the new item to the old items position
// -----Another optional way to move the item----
// var newIndex = newItem.getIndex()
// form.moveItem(form.getItemById(newIndex, oldIndex)
// -----------------------------------------------
form.deleteItem(itemList[i]) //delete the old item
}
}