Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
237 views
in Technique[技术] by (71.8m points)

javascript - Global variables in Google Script (spreadsheet)

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.

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.

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.

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.

Here is the code:

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

Output:

  • outside3
  • declaring global variable
  • outside3
  • declaring global variable
  • globalVarTestFunctionOne
  • Value of globalTestVar: 1
  • outside3
  • declaring global variable
  • globalVarTestFunctionTwo
  • Value of globalTestVar: 1

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

Thank you!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...