Google Script 中的全局变量(电子表格) [英] Global variables in Google Script (spreadsheet)

查看:23
本文介绍了Google Script 中的全局变量(电子表格)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直试图在 Google 应用程序脚本(电子表格内)中获取一些函数来修改全局变量,但我似乎无法弄清楚.

I've been trying to get some functions in a Google apps script (inside a spreadsheet) to modify a global variable, but I can't seem to figure it out.

基本上我想声明一个变量(在本例中为globalTestVar"),并且每次两个函数(globalVarTestFunctionOne 和两个)中的一个启动该变量时,该变量应加一.

Basically I want to declare a variable (in this case "globalTestVar"), and every time one of the two functions (globalVarTestFunctionOne and two) launches this variable should increment by one.

问题是每次按下按钮时都会再次声明变量,即使 if(typeof(globalTestVar) == 'undefined') 语句应该处理这个问题.

The problem is that the variable is being declared again every time a button is pressed even though the if(typeof(globalTestVar) == 'undefined')-statement should take care of that.

我习惯了 Objective C 和 Java,我可以在开始时声明我的变量,并在代码中的任何地方修改这些变量.

I'm used to Objective C and Java where I can declare my variables in the beginning, and modify these variables anywhere in the code.

如果这是一个基本问题,我很抱歉,但我已经用谷歌搜索了几个小时,但我无法让它工作.

I'm sorry if this is a basic question but I've been googling for hours and I just can't get it to work.

代码如下:

logstuff("outside");


function logstuff(logInput){
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Sheet1');
var lastRow = sheet.getLastRow() + 1;
sheet.getRange("A"+lastRow).setValue(logInput);
return;
}


if (typeof(globalTestVar) == 'undefined') {
logstuff('declaring global variable');
globalTestVar = 0;

} else {
logstuff('global variable has been declared'); 
}



function globalVarTestUIFunction() {
var app = UiApp.createApplication().setTitle('Test UI');
var doc = SpreadsheetApp.getActive();
var formPanel = app.createVerticalPanel();


var buttonF1 = app.createButton('F1');
var buttonbuttonF1ClickHandler = app.createServerClickHandler("globalVarTestFunctionOne");
buttonF1.addClickHandler(buttonbuttonF1ClickHandler);
buttonbuttonF1ClickHandler.addCallbackElement(formPanel);

var buttonF2 = app.createButton('F2');
var buttonbuttonF2ClickHandler = app.createServerClickHandler("globalVarTestFunctionTwo");
buttonF2.addClickHandler(buttonbuttonF2ClickHandler);
buttonbuttonF2ClickHandler.addCallbackElement(formPanel);


app.add(formPanel);

formPanel.add(buttonF1);
formPanel.add(buttonF2);


doc.show(app);

return app;
}



function globalVarTestFunctionOne() {
logstuff('globalVarTestFunctionOne');
globalTestVar++;
logstuff('Value of globalTestVar: ' + globalTestVar);
}

function globalVarTestFunctionTwo() {
logstuff('globalVarTestFunctionTwo');
globalTestVar++;
logstuff('Value of globalTestVar: ' + globalTestVar);
}

输出:

  • outside3
  • 声明全局变量
  • outside3
  • 声明全局变量
  • globalVarTestFunctionOne
  • globalTestVar 的值:1
  • outside3
  • 声明全局变量
  • globalVarTestFunctionTwo
  • globalTestVar 的值:1

我编写了自己的函数logstuff"来打印消息,因为我不喜欢内置的 Logger.log 函数.

I've written my own function "logstuff" to print out messages because I don't like the built in Logger.log-function.

谢谢!

推荐答案

你不会喜欢这样的:GAS 中的全局变量是静态的——你不能更新它们并期望它们保留它们的值.我也用谷歌搜索了几个小时.

You won't like this: global variables in GAS are static - you can't update them and expect them to retain their values. I, too, googled this for hours.

您可以使用 CacheServiceScriptDB 作为此类问题的可能存储空间.CacheService 快速且易于使用,但由于缓存最终会过期而受到限制.我没试过 ScriptDB

You can use CacheService or ScriptDB as possible storage for this sort of problem. CacheService is quick and easy to use, but limited because the cache will expire eventually. I haven't tried ScriptDB

这篇关于Google Script 中的全局变量(电子表格)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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