全局变量值在多个函数中不可用 [英] Global Variable value not usable in multiple functions

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

问题描述

使用Google Apps脚本我正在尝试创建一个可用于多种功能的全局变量(例如一个数组),但我似乎无法在任何地方找到答案,并且我需要它使Google电子表格能够正常工作。



代码:

  var infoSheetArray = null; 

function onOpen(){
var ui = SpreadsheetApp.getUi();
ui.createMenu('Custom Menu')
.addItem('Fetch Info','grabInfo')
.addItem('Run Program','itemSetup')
。 addToUi();
}

函数grabInfo(){
var infoSheet = SpreadsheetApp.openByUrl('....');
infoSheetArray = infoSheet.getSheetValues(1,1,infoSheet.getLastRow(),10);


解决方案

c $ c> infoSheetArray 是一个全局范围的变量,这意味着它可以被程序中的所有代码访问。不那么明显的是,你的程序运行多次&在不同的机器中,这就是为什么看起来 infoSheetArray 正在重置。



一个脚本在新的执行实例中完成,每个脚本都有自己的一组全局变量。例如,在您的示例代码片段中,当在Web编辑器中打开电子表格时, onOpen()是一个独立调用的自动触发器函数。您还有两个独立调用的独立函数 grabInfo() itemSetup()(未显示) 通过用户菜单扩展。



当这三个函数中的任何一个独立调用时,为它创建一个执行实例,加载程序,然后执行开始。首先评估所有全局语句(例如 var infoSheetArray = null; ),然后是特定的触发函数。对全局变量所做的任何更改只会在发生更改的执行实例内持续存在,并且只会在需要该实例的情况下才会更改。如果您需要在执行实例之间保留值,则需要使用一些存储方法,例如属性服务外部数据库


Using Google Apps Script I'm trying to create a global variable (e.g. an array) that can be used in multiple functions, but I can't seem to find the answer anywhere and I need it for my Google Spreadsheet to work.

Code:

var infoSheetArray = null;

function onOpen() {
  var ui = SpreadsheetApp.getUi();
  ui.createMenu('Custom Menu')
      .addItem('Fetch Info', 'grabInfo')
      .addItem('Run Program', 'itemSetup')
      .addToUi();
}

function grabInfo() {
  var infoSheet = SpreadsheetApp.openByUrl('....');
  infoSheetArray = infoSheet.getSheetValues(1, 1, infoSheet.getLastRow(), 10);
}    

解决方案

In your code, infoSheetArray is a variable with global scope, meaning that it is accessible by all code in your program. What isn't so obvious is that your program is running multiple times & in different "machines", and that's why it may appear that infoSheetArray is getting reset.

Each separate execution of a script is done in a new execution instance, and each of those have their own set of "globals". In your example snippet, for example, onOpen() is an automatic trigger function invoked independently when the spreadsheet is opened in the web editor. You have two other functions, grabInfo() and itemSetup() (not shown) that get invoked independently via user menu extensions.

When any of these three functions are invoked independently, an execution instance is created for it, your program is loaded, and then execution begins. All global statements (e.g. var infoSheetArray = null;) are evaluated first, followed by the specifically triggered function. Any changes made to global variables will persist only within the execution instance the change happened in, and only for as long as that instance is required. Once the function completes, the instance is blown away.

If you need to persist values between execution instances, you need to use some storage method such as the Properties Service or an External Database.

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

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