感谢 Serge insas 的帮助,我一直在用谷歌应用脚本创建一个表单。在我开始设置一些持久变量之前,它运行良好。首先,我从由文本组成的持久变量开始(例如 var choice1 = 'Hello'),它工作得很好。但是我使用函数来定义我的变量让它变得更复杂了。这是我的代码的简化版本:
var choix1 = Math.floor((Math.random() * (33-13)) + 13);
var choix2 = Math.floor((Math.random() * (66-33)) + 33);
function doGet() {
var app = UiApp.createApplication().setTitle('test Questionnaire');
var panel = app.createVerticalPanel();
var sHdlr = app.createServerHandler('react').addCallbackElement(panel);
var questions = ['<b>Question Numéro 1 :</b><br>Faites votre choix parmis les 4 possibilités suivantes','<b>Question 2</b><br>Encore un fois, faites votre choix','<b>Question 3</b><br>encore un effort...','<b>Question 4</b><br>vous y êtes presque...'];
var Qitems = [[choix1.toString(),choix2.toString()],['choix1 de Q2','choix2 de Q2','choix3 de Q2','choix4 de Q2'],
['choix1 de Q3','choix2 de Q3','choix3 de Q3','choix4 de Q3'],['choix1 de Q4','choix2 de Q4','choix3 de Q4','choix4 de Q4']];
var Qpanel = [];
for (var n=0 ; n<questions.length ; ++n){
var Qval = app.createTextBox().setId('Qvalue'+n).setName('Qvalue'+n).setVisible(false);
Qpanel[n] = app.createVerticalPanel().setId('QP'+n).setVisible(false).add(app.createHTML(questions[n])).add(Qval).setStyleAttribute('padding','10px');
panel.add(Qpanel[n]);
for(var q=0;q<Qitems[n].length;++q){
var name = Qitems[n][q]
var handler = app.createClientHandler().forTargets(Qval).setText(name);
Qpanel[n].add(app.createRadioButton('radioButtonQ'+n,name).addClickHandler(handler).setId(name).addClickHandler(sHdlr));
}
}
app.add(panel);
Qpanel[0].setVisible(true);
return app;
}
function react(e){
var app = UiApp.getActiveApplication();
var source = e.parameter.source;
var answer = [];
for(var n = 0; n < 4 ; ++n){
answer[n] = e.parameter['Qvalue'+n];
Logger.log('answer '+ (n+1) + ' = '+answer[n]+' source = '+source)
}
if(answer[0]==choix1||answer[0]==choix2){app.getElementById('QP'+1).setVisible(true)}
if(answer[1]=='choix1 de Q2'||answer[1]=='choix3 de Q2'){app.getElementById('QP'+2).setVisible(true)}
if(answer[2]=='choix1 de Q3'||answer[2]=='choix3 de Q3'){app.getElementById('QP'+3).setVisible(true)}
if(answer[3]=='choix1 de Q4'){
app.add(app.createHTML('YESSSSSSSSS ... !!<br>Vous avez réussi !<br> vos réponses sont les suivantes : '+answer.join(' + ')).setStyleAttribute('padding','20px'))
}
return app;
}
在更复杂的代码中:我的决赛变量是多个“小”变量的聚合,就像这里介绍的那样。我已经通过这种方式构建了复杂的场景。而且我希望这些场景针对表单的每个用户进行更改。(我清楚吗?)在这个例子中,我不能传递到第 2 个问题,因为条件“answer[0]==choix1”似乎不起作用。我查看了其他一些问题,我的问题可能来自我的变量的定义,使用 ScriptDb 可能会有所帮助,但我对在这里使用它的方式感到困惑。
我会继续调查,但此刻我被这个问题困住了。