【发布时间】:2014-12-04 20:15:52
【问题描述】:
为雇主从事一个相当简单的项目。我需要一份调查表,提交后会将答案转储到电子表格中。我在独立应用程序上使用 Google Apps 脚本工作。项目管理层决定不使用 Google 表单。
我遇到的问题是检索单选按钮值。有许多单选按钮问题,我需要能够遍历它们以获得它们的值。
UI 是通过 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;
}
我的 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;
}
为了测试检索值的前提,我将其文本包含值的标签添加到应用程序。函数的相关部分:
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;
}
真正让我感到困惑的是,上面的代码吐出 24 次“你选择了未定义”,下面的代码完全有效:
function doPost(e) {
var app = UiApp.getActiveApplication();
app.add(app.createLabel().setText('You selected ' + e.parameter.item0
return app;
}
似乎只要我不尝试任何循环并且我手动编码整个事情,一切都很好。
对此有何见解?
【问题讨论】:
-
任何试图重现这一点的人都需要编写一个 UI,猜测你在其中有什么。您可以通过提供重现问题所需的所有(最少)代码来帮助解决问题。
标签: javascript forms for-loop google-apps-script