[英]Trouble iterating through radio button values in Google Apps Script
Working on a fairly straight-forward project for an employer. 为雇主开展一个相当直接的项目。 I need a survey form that when submitted will dump the answers into a spreadsheet.
我需要一份调查表,提交后会将答案转储到电子表格中。 I am working from Google Apps Script on a standalone app.
我正在使用独立应用程序上的Google Apps脚本。 Project management decided against Google Forms.
项目管理部门决定不使用Google Forms。
What I'm having a problem with is retrieving radio button values. 我遇到的问题是检索单选按钮值。 There are a multitude of radio button questions, and I need to be able to iterate through them to get their values.
有很多单选按钮问题,我需要能够遍历它们以获取其价值。
The UI is done through GAS's UiApp service: 用户界面是通过GAS的UiApp服务完成的:
function doGet(e){
var app = UiApp.createApplication();
// set up the form
var form = app.createFormPanel();
var flow = app.createFlowPanel();
form.add(flow);
app.add(form);
// create the radio button groups (they are all Likert scales).
// I've written a function that creates a single group (see below)
for (var i=0; i<24; i++){
likertScale(i, flow);
};
flow.add(app.createSubmitButton('Submit')
return app;
}
My likertScale() function used to create the radio button group: 我的likertScale()函数用于创建单选按钮组:
function likertScale(name, flow) {
// the 'name' argument allows me to pass in the var i from the
// for loop above to give each radio button group a unique name
// the 'flow' argument allows me to pass in the var flow that
// I created when setting up the form
var app = UiApp.getActiveApplication();
// create the N/A option, assign the group's name, give the button a label
// and a value
flow.add(app.createRadioButton('item'+name, 'N/A').setFormValue('NA'));
for (var j=1; j<6; j++) {
// create the 5-point Likert scale; assign the group's name to each button,
// use j to give each a label and a value
flow.add(app.createRadioButton('item'+name,j).setFormValue(j);
}
return app;
}
To test the premise of retrieving the values, I'm adding labels whose text contains the values to the app. 为了测试获取值的前提,我向应用程序添加了标签,其文本包含值。 The relevant parts of the function:
函数的相关部分:
function doPost(e) {
var app = UiApp.getActiveApplication();
for (var i=0; i<24; i++){
var radio = 'item'+i;
// I've previously set the name of each radio button group
// as 'item#', where # is a number 0-23.
app.add(app.createLabel().setText('You selected ' + e.parameter.radio));
// The idea is that each iteration recreates the name of a radio
// button, then uses that name to inform e.parameter.radio
};
return app;
}
What's really confusing for me is while the above code spits out 'You selected undefined' 24 times, the below code totally works: 真正让我感到困惑的是,以上代码吐出了“您选择了未定义的内容” 24次,下面的代码完全可以正常工作:
function doPost(e) {
var app = UiApp.getActiveApplication();
app.add(app.createLabel().setText('You selected ' + e.parameter.item0
return app;
}
It seems that as long as I don't try any looping and I hand-code the whole thing, everything is fine. 看来,只要我不尝试任何循环并手动编码整个内容,一切就很好。
Any insight on this? 有什么见解吗?
you can't reference the variable "radio" that way, it will be interpreted as a property named "radio" try: 您不能以这种方式引用变量“ radio”,它将被解释为名为“ radio”的属性try:
app.add(app.createLabel().setText('You selected ' + e.parameter[radio]));
See details on ways to access object properties here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Working_with_Objects#Objects_and_properties 在此处查看有关访问对象属性的方法的详细信息: https : //developer.mozilla.org/zh-CN/docs/Web/JavaScript/Guide/Working_with_Objects#Objects_and_properties
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.