首页 > 解决方案 > 如何在谷歌应用脚​​本中使用 setrequired()

问题描述

我对谷歌应用脚​​本很陌生,因为谷歌表单不能执行条件要求的功能,我需要自己修改表单。我在谷歌应用程序脚本网站中找到了这段代码,我尝试使用它来测试我是否能够实现我想要的。我想实现什么:

  1. 对于“你喜欢猫还是狗?” 问题,如果用户选择猫,“评价你的兴趣”问题将自动设置为必填。

我从我拥有的代码中得到了什么:

  1. if 语句没有按预期工作。无论 if 语句条件如何,“评价您的兴趣”问题都设置为必填。

以下是代码:

function myfunction(){
var form = FormApp.create('New Form');
var item = form.addCheckboxItem();
item.setTitle('What condiments would you like on your hot dog?');
item.setChoices([
    item.createChoice('Ketchup'),
    item.createChoice('Mustard'),
    item.createChoice('Relish')
]);
var a= form.addMultipleChoiceItem()
.setTitle('Do you prefer cats or dogs?')
.setChoiceValues(['Cats','Dogs'])
.showOtherOption(true);
form.addPageBreakItem()
.setTitle('Getting to know you');
form.addDateItem()
.setTitle('When were you born?');
var b= form.addGridItem()
.setTitle('Rate your interests')
.setRows(['Cars', 'Computers', 'Celebrities'])
.setColumns(['Boring', 'So-so', 'Interesting']);
if (a != null){
b= b.setRequired(true);
}

Logger.log('Published URL: ' + form.getPublishedUrl());
Logger.log('Editor URL: ' + form.getEditUrl());
}

标签: google-apps-scriptgoogle-forms

解决方案


关于您的 if 语句:变量“a”不会为空,因为您刚刚为变量 A 设置了一个值。

你必须记住你正在创建一个表单,把它想象成一个一次性的过程。当用户向您的表单提交回复时,此脚本将不会运行。

我可以在这里看到您要实现的目标,遗憾的是,上面的代码无法实现。

我可以建议将您喜欢猫还是狗设置为必填问题,然后根据答案转到不同的部分。

EG 我喜欢猫,请转到表格的第 2 部分。我喜欢狗,请转到表格的第 3 部分。(例如,请参见图片。)在此处输入图像描述


推荐阅读