函数中定义的全局变量没有定义? [英] Global variable defined in function appears not defined?

查看:148
本文介绍了函数中定义的全局变量没有定义?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为Google Spreadsheets编写一个脚本,我希望在整个脚本中全局使用我的标题索引。 相关所有关于在Google Apps脚本中定义全局变量的帖子以及这个答案原来你不能更新处理函数内的全局变量,例如GAS中的全局变量是静态的。



现在,它取决于你的特定用例,但是我假设你已经定义了一个函数 getHeaders(),你可能会想多次调用它。如果不是这样,那么你可以简单地声明和初始化那些在所有函数范围之外的变量作为全局变量,并在任何其他函数中读取它们。



可能想在你的情况下尝试这样的事情(未测试以下代码):

  var sheet = SpreadsheetApp.getActiveSheet (); 
var data = sheet.getDataRange()。getValues();
var header = data [0]

headerIdIndex = headers.indexOf(ID)
headerNameIndex = headers.indexOf(First-Last)

....

函数tellMeNames(){
var sheet = SpreadsheetApp.getActiveSheet();
var data = sheet.getDataRange()。getValues();
for(var i = 1; i< data.length; i ++){
Logger.log(Name:+ data [i] [headerNameIndex])
}
}

如果想要在处理函数中存储和更新全局变量,您可能需要尝试脚本数据库。希望这能让你从正确的方向开始。


I'm writing a script for Google Spreadsheets, I want to have my headers index available globally throughout the script.

According to the theory, I should be able to define global variables from within a function.

function testFunc() {
testVar = 1;       // `testVar` is a Global variable now
}

In my code it looks more or less like this:

function getHeaders() {
  var sheet = SpreadsheetApp.getActiveSheet();
  var data = sheet.getDataRange().getValues();
  var headers = data[0]

  headerIdIndex = headers.indexOf("ID")
  headerNameIndex = headers.indexOf("First-Last")
}

However, later on in my code, when I call up the variable headerNameIndex, it appears undefined:

function tellMeNames() {
  var sheet = SpreadsheetApp.getActiveSheet();
  var data = sheet.getDataRange().getValues();
  for (var i = 1; i < data.length; i++) {
    Logger.log("Name: " + data[i][headerNameIndex])
  }
}

So what am I doing wrong? Thanks.

解决方案

Well, I was going through some relevant SO posts on defining a global variable in Google Apps Script and from this answer it turns out you can not update global variables inside a handler function i.e. Global Variables in GAS are static.

Now, it depends on your specific use case but I am assuming since you have defined a function getHeaders(), you would want to make a call to it, more than once. If however that's not the case, then you can simply declare and initialize those variables outside the scope of all functions as global variables and read them in any other functions.

So you might want to try something like this in your case (haven't tested the following code):

 var sheet = SpreadsheetApp.getActiveSheet();
 var data = sheet.getDataRange().getValues();
 var headers = data[0]

 headerIdIndex = headers.indexOf("ID")
 headerNameIndex = headers.indexOf("First-Last")

 ....

 function tellMeNames() {
  var sheet = SpreadsheetApp.getActiveSheet();
  var data = sheet.getDataRange().getValues();
 for (var i = 1; i < data.length; i++) {
  Logger.log("Name: " + data[i][headerNameIndex])
 }
}

If however you want to store and update global variables in handler functions, you might want to give Script-DB a try. Hope this gets you started in the right direction.

这篇关于函数中定义的全局变量没有定义?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆