Google 表格差异

Google Forms discrepancy

当我将列表项添加到 Google 表单时,我可以设置和获取选项。

如果我 select 来自现有表单的列表项,我不能。

items = form.getItems();

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

我可以在日志中看到这些项目属于 "List" 类型,但 getChoices 抛出错误

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

这是 Apps 脚本 中的错误吗?我可以对物品 cast 做些什么并确保它是正确的类型吗?

谢谢

杰里米

请参考文档here. As you can see an item does not have a .getChoices() method. This method is available in specific types of items, like ListItem。所以你需要专门做一些像

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

请记住,items 是一个数组,您需要指定要选择的项目。然后您需要指定它是什么类型的项目(即列表项),然后您才能获得选择。如果这是您需要为 任何 类型的项目做的事情,那么您将需要弄清楚如何检查它是什么类型的项目,然后将其作为该项目类型获取。