在全球范围内调用变量并在Google应用脚本中进行比较

时间:2013-06-26 14:50:52

标签: forms google-apps-script

感谢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可能有所帮助,但我对这里使用它的方式感到困惑。

我会继续调查,但此刻我仍然坚持这个问题。

2 个答案:

答案 0 :(得分:1)

您只能将常量分配给已执行代码之外的全局变量。而不是:

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 choix1;
var choix2;

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

实际上你可以放弃行

var choix1;
var choix2;

同样,但在一个地方列出所有全局变量是个好主意。

但是,你必须要知道变量choix1和choix2根本不是持久的 - 只能在全局范围中看到。每次调用webapp代码时都会重新计算这些值。

更明确:

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

在服务器处理程序中只是无意义,因为choix1和choix2将具有除doGet中设置的任何其他随机值。

如果您需要会话变量,则必须将它们存储在UiInstance中。使用隐藏元素,这就是它们被发明的原因。

答案 1 :(得分:0)

我找到了一种方法来做我想做的事。我将不同的选项存储在电子表格中,并将命题直接添加到问题中。答案只是'option1','option2'......所以我仍然可以知道选择了哪个选项。

function doGet() {
  var choix1 = Math.floor((Math.random() * (33-13)) + 13);
  var choix2 = Math.floor((Math.random() * (66-33)) + 33);
  var ss = SpreadsheetApp.openById('0ApJ8EJRunZt-dEtBU2dkQ0xrRzdTeVJhZXdaQnR3VEE');
  var sh = ss.getActiveSheet();
  var firstEmptyRow = sh.getLastRow() + 1;
  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 :<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...'];
  var Qitems = [['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']];
  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);
    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){
  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]=='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;
}

但我的最后一个问题是:由于我会为更多的选项和问题而这样做,它会让脚本运行得更慢吗?

感谢Taras的帮助,你指出了我不知道的事情!