如何通过脚本向现有的Google表单项添加验证? [英] How to add validation to existing google form items via script?

查看:135
本文介绍了如何通过脚本向现有的Google表单项添加验证?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为我的Google表单文本项添加验证,特别是文本验证。

I am trying to add validation, specifically text validation, for my google form text items.

然而,在我看来,像'setValidation()'函数仅适用于已知类型的项目 TextItem

However, it looks to me like the 'setValidation()' function only works with items with known type like TextItem.

据我所知,如果我通过'getItemById拉取表单项( )',我会得到一个通用项目。它仍然具有TEXT类型,但谷歌脚本只是没有将其视为TextItem,因此'setValidation()'函数不可用。

To my understanding, if I pull a form item via 'getItemById()', I would get a generic item. It still has 'TEXT' type but google script just doesn't see it as a TextItem and therefore the 'setValidation()' function is not available for it.

我尝试过像 .asTextItem()这样的事情,但没有运气。以下是由于错误而无法运行的示例脚本

I have tried doing thing like .asTextItem() with no luck. Here is an example script that fails to run because of an error


'TypeError:无法在对象Item中找到函数setValidation。 (行
10,文件代码)'第9行。

'TypeError: Cannot find function setValidation in object Item. (line 10, file "Code")' on line 9.



function validationTest() {
var form = FormApp.getActiveForm();
  var items = form.getItems();
  var textValidation = FormApp.createTextValidation()
    .requireNumberGreaterThanOrEqualTo(0)
    .requireWholeNumber();
  for (var i = 0; i<items.length; i++) {
    items[i].asTextItem();
    items[i].setValidation(textValidation);
  };
}

那么,这个问题是否有已知的解决方案或解决方法?提前谢谢。

So, is there a known solution or workaround for this issue? Thank you in advance.

SC

推荐答案

你应该添加<验证构建器末尾的code> .build(),如图所示这里

You should add .build() at the end of your validation builder, as it's shown here.

此外, asTextItem 应该是与同时调用setValidation

function validationTest() {
var form = FormApp.getActiveForm();
  var items = form.getItems();
  var textValidation = FormApp.createTextValidation()
    .requireNumberGreaterThanOrEqualTo(0)
    .requireWholeNumber()
    .build();
  for (var i = 0; i<items.length; i++) {
    items[i].asTextItem().setValidation(textValidation);
  };
}

这篇关于如何通过脚本向现有的Google表单项添加验证?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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