修改现有的表单值-GetChoices()无法正常工作 [英] Modify existing form Values - GetChoices() not working

查看:46
本文介绍了修改现有的表单值-GetChoices()无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么' .getChoices()'不适用于现有列表项吗?

Why does '.getChoices()' not work for existing list items?

我有以下代码,通过它的ID来获取表单中的项,并且我打算更新表单项的值.但是,使用.getChoices()方法时,它将失败,并显示错误" TypeError:在对象Item中找不到函数getChoices."

I have the following code which gets an item in a form by it's ID, and I intend to update the values of the form item. However, when using the .getChoices() method, it fails with the error 'TypeError: Cannot find function getChoices in object Item.'

我要提取的项目是所需的列表项目,并且创建列表项目时,先提取然后,它就可以正常工作,如

The item I'm fetching is a list item as required, and when a list item is created then fetched, it works normally as in the sample code listed here.

我的代码如下:

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);
}

并证明它是一个列表项,我的日志记录输出是:

and to prove it is a list item, my logging output is:

我使用不正确吗?还是只能在Apps脚本也创建项目时使用?相反,我将如何在此列表项中获得选择并使用新选项更新它们?我看到其他用户设法做到了这一点,所以我相信这是有可能的.

Am I using this incorrectly, or can this only be used when Apps script creates the item also? How instead would I get the choices in this list item and update them with new options? I see other users have managed to do this, so I believe it's possible.

推荐答案

Item接口类,它提供了一些适用于所有表单项的方法. 接口对象本身很少有用;相反,您通常希望调用诸如Element.asParagraph()之类的方法来将对象强制转换为精确的类." 参考

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

由于.getChoices()是属于 ListItem 类,并且没有出现在Item中,您需要使用

Since .getChoices() is a method that belongs to the ListItem class, and does not appear in Item, you need to cast your Item to ListItem using 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." );

这篇关于修改现有的表单值-GetChoices()无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆