私有/公共变量Javascript

私有/公共变量Javascript,javascript,Javascript,我正在为谷歌电子表格编写一些脚本。其中一个函数需要获取用户输入。之后,我需要在其他几个函数中使用输入,我在下面展示了一个示例。问题是,每次需要使用输入时是否需要调用函数“Setup()”?这将意味着多次要求输入,我知道这是愚蠢的 我怎样才能解决这个问题 谢谢 var setupdone = false; function Setup(){ if(!setupdone){ numofTeams = Browser.inputBox('Confirm the number of Tea

我正在为谷歌电子表格编写一些脚本。其中一个函数需要获取用户输入。之后,我需要在其他几个函数中使用输入,我在下面展示了一个示例。问题是,每次需要使用输入时是否需要调用函数“Setup()”?这将意味着多次要求输入,我知道这是愚蠢的

我怎样才能解决这个问题

谢谢

var setupdone = false;

function Setup(){
  if(!setupdone){
    numofTeams = Browser.inputBox('Confirm the number of Teams');
    var ui = Browser.msgBox('Ensure that the teams are arranged according to lane allocations below', Browser.Buttons.OK_CANCEL);
    setupdone = true;
  }

  var semisraw = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Secondary Group 1');
  var range = semisraw.getDataRange();
  values = range.getValues();
};

function numofTeamsprint(){
  Setup();
  Browser.msgBox('Setup is Complete!');
};

您可以将
numofTeams
设置为等于
-1
,然后仅在
numofTeams
<0
时请求输入,如下所示:

var numofTeams = -1;

function Setup(){
  if (numofTeams < 0) {
    numofTeams = Browser.inputBox('Confirm the number of Teams');
  }
  var ui = Browser.msgBox('Ensure that the teams are arranged according to lane allocations below', Browser.Buttons.OK_CANCEL);
  var semisraw = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Secondary Group 1');
  var range = semisraw.getDataRange();
  values = range.getValues();
};

function numofTeamsprint(){
  Setup();
  Logger.log(values[0][0]);
};
var numofTeams=-1;
函数设置(){
如果(numofTeams<0){
numofTeams=Browser.inputBox('确认团队数量');
}
var ui=Browser.msgBox('确保根据下面的车道分配安排车队',Browser.button.OK\u CANCEL);
var semisraw=SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Secondary Group 1');
var range=semisraw.getDataRange();
values=range.getValues();
};
函数numofTeamsprint(){
设置();
Logger.log(值[0][0]);
};

如果出于某种原因再次需要该输入,只需将其设置回
-1
,然后运行
Setup()

如果您将变量定义为全局变量(函数外部),则可以测试它们是否已设置,唯一提示的是它们尚未设置

var setupDone = false;

function setup () {
  if (!setupDone) {
    // do whatever
    setupDone = true;
  }

}

如果变量附加到
窗口
对象,则可以将其称为
全局变量

以这个例子为例

var thisIsAGlobalVar = 4;

function method1() {
    var thisIsALocalVar;

    // My Code
    // 1. thisIsALocalVar is available in this function only
    // 2. Its scope would be destroyed as soon as the function itself goes out of scope
    // 3. thisIsAGlobalVar can be access here.

    //Suppose I introduce another variable, without the var keyword, like the one below, inside of a function, the JavaScript interpretter adds it to the window object

    thisAlsoIsAGlobalVar = "Global";

}

function method2 () {

    //My Code
    // 1. thisIsAGlobalVar can be accessed here also

    //Other Code

    //Now that thisAlsoIsAGlobalVar is declared in method1, it has become global and hence can be used here as well
    //Only Pre-condition being method1 should be called firsr

    console.log(thisAlsoIsAGlobalVar); // Would print "Global"

    console.log(thisIsALocalVar); // Would be "undefined"

}

希望这有帮助,干杯

很酷,所以,我仍然会再次调用该函数以获取值。但由于setupdone检查,将不会提示用户。我说的对吗?没错。您可以根据需要多次调用它,只要
numofTeams
小于零,就不会提示用户。另一个答案使用相同的概念,但使用布尔值。你选择哪一个取决于你的设计。好吧,我试过了,但似乎不起作用。每次运行安装程序时,它都会提示用户输入。编辑的代码在原始qnI中可能会指出,尽管我使用了
-1
,因为在Javascript中,
0
false
都将计算为
false
,如果您这样编写代码:
if(numofTeams){…}
。由于您可能希望
numofTeams
为零,因此布尔值可能会在以后引起麻烦。您意识到,每次刷新页面时,
numofTeams
都会被重置。:)如果您想要一个能够持续多次页面刷新的解决方案,那么您必须将其存储在更持久的位置,如GET或cookie。很酷,因此,我仍将再次调用该函数以获取值。但由于setupdone检查,将不会提示用户。我这样说对吗?非常感谢。好吧,我试过了,但似乎不起作用。每次运行安装程序时,它仍会提示用户输入