在全局范围内调用变量,并在谷歌应用程序脚本中进行比较

Calling variables at global scope and comparing it in Google apps script

本文关键字:脚本 比较 应用程序 范围内 全局 调用 变量 谷歌      更新时间:2023-09-26

多亏了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,因为条件"answerswer[0]==choix1"似乎不起作用。我已经考虑了其他一些问题,我的问题可能来自变量的定义,使用ScriptDb可能会有所帮助,但我对这里的使用方式感到困惑。

我会继续调查,但现在我陷入了这个问题。

只能将常量分配给执行代码之外的全局变量。代替:

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"、"选项2"。。。所以我仍然可以知道选择了哪个选项。

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;
}