下面的代码是给我一些头痛。 这是一个非常简单的应用,有2个单选按钮和一个按钮。 此应用程序的构思是在电子表格中记录所选择的单选按钮的值。 现在,如果我选择了第一个单选按钮(“1”),然后切换到第二个单选按钮(“二”),然后回到“一个”,然后单击“录制”按钮,该应用程序将被记录3次电子表格。 它种积累选择。
不知道我说清楚了,但这里有模拟问题的步骤:
1-运行下面的应用; 2-选择单选按钮“一个”,然后“2”和“一个”再次按下“记录”按钮; 3-检查电子表格重复值。
你不断地点击单选按钮的越多,重复值将被记录。
任何想法,为什么发生这种情况,我该如何解决这个问题?
function myAppFunction() {
var mydoc = SpreadsheetApp.getActiveSpreadsheet();
var app = UiApp.createApplication().setTitle('Here is the title bar');
var panel = app.createVerticalPanel().setId('panel');
//using setName will give you the ability to get the value
var myRadio1 = app.createRadioButton("group","one").setName('myRadio1').setId('myRadio1');
var myRadio2 = app.createRadioButton("group","two").setName('myRadio2').setId('myRadio2');
var button = app.createButton("Gravar").setId("record_");
//A label will be used to give feedback on the value of the checkBox
var infoLabel = app.createLabel('Check the box and click submit').setId('infoLabel');
//handlers
var handler = app.createServerChangeHandler('radio1');
handler.addCallbackElement(panel);
myRadio1.addClickHandler(handler);
var handler2 = app.createServerChangeHandler('radio2');
handler2.addCallbackElement(panel);
myRadio2.addClickHandler(handler2);
//put everything in the UI
panel.add(myRadio1);
panel.add(myRadio2);
panel.add(button);
panel.add(infoLabel);
app.add(panel);
mydoc.show(app);
}
function radio1(e){
var app = UiApp.getActiveApplication();
app.getElementById('myRadio2').setValue(false);
app.getElementById('infoLabel').setText('one is: ' + e.parameter.myRadio1);
var panel = app.getElementById("panel");
var button = app.getElementById("gravar");
var handler = app.createServerClickHandler("record_");
handler.addCallbackElement(panel);
button.addClickHandler(handler);
return app;
}
function radio2(e){
var app = UiApp.getActiveApplication();
app.getElementById('myRadio1').setValue(false);
app.getElementById('infoLabel').setText('two is: ' + e.parameter.myRadio2);
var button = app.getElementById("gravar");
var handler = app.createServerClickHandler("gravar_");
handler.addCallbackElement(app.getElementById("panel"));
button.addClickHandler(handler);
return app;
}
function record_(e) {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName("Sheet1");
var lastRow = sheet.getLastRow();
var freeCell = sheet.getRange("A1").offset(lastRow, 0);
freeCell.setValue(e.parameter.myRadio1);
}
单选按钮必须具有相同的名称在正常异或模式下工作,如果你指的是文档的标准面板使用时,你会发现,这是必需的。 下面是一个简单的例子:
function radiotest() {
var app = UiApp.createApplication();
var panel = app.createVerticalPanel();
var radioValue = app.createTextBox();
radioValue.setId("radioValue").setName("radioValue").setVisible(false);
for(var i = 1; i < 10; i++){
var name = 'choice '+i;
var handler = app.createClientHandler().forTargets(radioValue).setText(name);
panel.add(app.createRadioButton('radio',name).addValueChangeHandler(handler));
}
panel.add(radioValue);
var getit=app.createButton("Valide").setId("val");
panel.add(getit)
var handler = app.createServerHandler("valide")
handler.addCallbackElement(panel)
getit.addClickHandler(handler);
app.add(panel);
SpreadsheetApp.getActiveSpreadsheet().show(app);// show app
}
//
function valide(e){ ;// This function is called when key "validate" is pressed
var sh = SpreadsheetApp.getActiveSheet();
var ss = SpreadsheetApp.getActiveSpreadsheet();
var RadioButton = e.parameter.radioValue;
sh.getRange('A1').setValue(RadioButton);
var app = UiApp.getActiveApplication();
return app;
}
我不明白你的代码的行为,但我认为这是不恰当的和过于复杂。
首先,创建两个单选按钮设置他们的名字为“组”,这是对的,但立即在创建之后,你用“myRadio1”分别改写自己的名字,并以“myRadio1”,这是不是越野车,但没有用,因为它迫使你添加服务器更改处理到每个单选按钮。
如果离开这两个单选按钮名称相同的“群”,它们会自动触发一个彼此当你点击它们。 因此,你不需要点击处理程序添加到他们。
因为它们共享同一个名字:“组”,您将能够访问他们的状态(“假”,如果选择了第一个单选按钮,“真”,如果第二个被选中)使用“e.parameter.group”参数验证按钮的点击处理程序的回调函数。
这里是你的代码简化和固定:
function myAppFunction() {
var mydoc = SpreadsheetApp.getActiveSpreadsheet();
var app = UiApp.createApplication().setTitle('Here is the title bar');
var panel = app.createVerticalPanel().setId('panel');
// Create the radio buttons and add it to the panel
var myRadio1 = app.createRadioButton("group","one");
var myRadio2 = app.createRadioButton("group","two");
panel.add(myRadio1);
panel.add(myRadio2);
// Create the apply button, add click handler and add it to the panel
var button = app.createButton("Gravar").addClickHandler(app.createServerHandler("record_").addCallbackElement(panel));
panel.add(button);
// Create the label and add it to panel
var infoLabel = app.createLabel('Check the box and click submit');
panel.add(infoLabel);
// Add the panel in the GUI
app.add(panel);
// Show the GUI in the spreadsheet
mydoc.show(app);
}
function record_(e) {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName("Sheet1");
var lastRow = sheet.getLastRow();
var freeCell = sheet.getRange("A1").offset(lastRow, 0);
freeCell.setValue(e.parameter.group);
}