为什么 '.getChoices() https://developers.google.com/apps-script/reference/forms/list-item#getChoices()' 不适用于现有列表项?
我有以下代码,它通过 ID 获取表单中的项目,并且我打算更新表单项目的值。但是,当使用 .getChoices() 方法时,它会失败并出现错误 '类型错误:无法在对象 Item 中找到函数 getChoices。'
我要获取的项目是所需的列表项,并且是在创建列表项时then获取后,它可以正常工作,如列出的示例代码所示here https://developers.google.com/apps-script/reference/forms/choice#getValue().
我的代码如下:
function getWeekNumberFormItem() {
var form = FormApp.getActiveForm();
var item = form.getItemById(12345);//redacted for privacy, but the ID in here is correct.
var title = item.getTitle();
var itemType = item.getType();
Logger.log('Item Type: ' + itemType);
Logger.log('Item Title: ' + title);
var choices = item.getChoices();
Logger.log(choices);
}
为了证明它是一个列表项,我的日志输出是:
我是否错误地使用了它,或者只能在 Apps 脚本也创建该项目时使用它?相反,我如何获取此列表项中的选项并使用新选项更新它们?我看到其他用户已经成功做到了这一点,所以我相信这是可能的。
Item
is an interface class that provides a few methods applicable to all form items. "Interface objects are rarely useful on their own; instead, you usually want to call a method like Element.asParagraph() to cast the object back to a precise class." ref https://developers.google.com/apps-script/guides/services/#dealing_with_interfaces
Since .getChoices()
是一个属于ListItem https://developers.google.com/apps-script/reference/forms/list-item.html类,并且没有出现在Item
,你需要投射你的Item
to ListItem
using Item.asListItem() https://developers.google.com/apps-script/reference/forms/item#asListItem().
...
var itemType = item.getType();
if (itemType == FormApp.ItemType.LIST) {
var choices = item.asListItem().getChoices();
// ^^^^^^^^^^^^
}
else throw new Error( "Item is not a List." );
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)