Skip to content Skip to sidebar Skip to footer

Calling Variables At Global Scope And Comparing It In Google Apps Script

Thanks to the help of Serge insas, I've been creating a form with google apps script. It was working well until I started to set some persistant variables. First I started with per

Solution 1:

You can only assign constants to global variables outside of the executed code. Instead of:

var choix1 = Math.floor((Math.random() * (33-13)) + 13);
var choix2 = Math.floor((Math.random() * (66-33)) + 33);

functiondoGet() {
  var app = UiApp.createApplication().setTitle('test Questionnaire');
...

write

var choix1;
var choix2;

functiondoGet() {
choix1 = Math.floor((Math.random() * (33-13)) + 13);
choix2 = Math.floor((Math.random() * (66-33)) + 33);
  var app = UiApp.createApplication().setTitle('test Questionnaire');
...

in fact you can drop the lines

var choix1;
var choix2;

as well, but it's a good idea to list all global variables in one place.

However, you have to be aware that the variables choix1 and choix2 are not persistent at all - only visible at global scope. The values will be recomputed for each call of the webapp code.

More explicit:

if (answer[0]==choix1||answer[0]==choix2) ...

is simply nonsense in a server handler as choix1 and choix2 will have other random values than whatever was set in the doGet.

If you need session variables you have to store them in the UiInstance. Use Hidden elements, that's why they were invented.

Solution 2:

Well I found a way to do what I want to. I store the different options in a spreadsheet and add directly the propositions into the question. Answers are just 'option1', 'option2'... so I still can know which option was chosen.

function doGet() {
  varchoix1= Math.floor((Math.random() * (33-13)) + 13);
  varchoix2= Math.floor((Math.random() * (66-33)) + 33);
  varss= SpreadsheetApp.openById('0ApJ8EJRunZt-dEtBU2dkQ0xrRzdTeVJhZXdaQnR3VEE');
  varsh= ss.getActiveSheet();
  varfirstEmptyRow= sh.getLastRow() + 1;
  varapp= UiApp.createApplication().setTitle('test Questionnaire');
  varpanel= app.createVerticalPanel();
  varsHdlr= app.createServerHandler('react').addCallbackElement(panel);
  varquestions= ['<b>Question Numéro 1 :</b><br>Faites votre choix parmis les 4 possibilités suivantes :<br>'+ choix1 + '<br>' +choix2,'<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...'];
  varQitems= [['option1','option2'],['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']];
  varQpanel= [];
  for (var n=0 ; n<questions.length ; ++n){
    varQval= 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){
      varname= Qitems[n][q]
      varhandler= app.createClientHandler().forTargets(Qval).setText(name);
      Qpanel[n].add(app.createRadioButton('radioButtonQ'+n,name).addClickHandler(handler).setId(name).addClickHandler(sHdlr));
    }
    }
    app.add(panel);
    sh.getRange(firstEmptyRow,1,1,1).setValue(choix1);
    sh.getRange(firstEmptyRow,2,1,1).setValue(choix2);
    Qpanel[0].setVisible(true);
    return app;
}

function react(e){
  varapp= UiApp.getActiveApplication();
  varsource= e.parameter.source;
  varanswer= [];
  
  
  for(varn=0; n < 4 ; ++n){
    answer[n] = e.parameter['Qvalue'+n];
    Logger.log('answer '+ (n+1) + ' = '+answer[n]+' source = '+source)
    }
      if(answer[0]=='option1'||answer[0]=='option2'){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;
}

Post a Comment for "Calling Variables At Global Scope And Comparing It In Google Apps Script"