在UI处理程序Google Spreadsheet Script中访问全局变量 [英] Accessing global variable in UI handler Google Spreadsheet Script

查看:95
本文介绍了在UI处理程序Google Spreadsheet Script中访问全局变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的,但是导入的问题。

我在脚本中声明了一个全局变量(Google Spreadsheet Script)。接下来,我正在从SS脚本调用一个UI应用程序。它有一个列表框和一个按钮。它显示罚款。



当我点击按钮时,它应该把选定的值放到全局变量中。问题出在处理函数中(单击(eventInfo)),它不能识别全局变量(SELNAME)。

  //全局变量
var SELNAME;

function show(){
var doc = SpreadsheetApp.getActiveSpreadsheet();
var app = UiApp.createApplication()。setTitle('My Application');
var panel = app.createVerticalPanel();
var lb = app.createListBox(true).setId('myId')。setName('myLbName');


//将项目添加到ListBox
lb.setVisibleItemCount(8);

lb.addItem(one);
lb.addItem(two);
lb.addItem(three);

panel.add(lb);
var button = app.createButton('press me');
var handler = app.createServerClickHandler('click')。addCallbackElement(panel);
button.addClickHandler(handler);
panel.add(button);
app.add(panel);
doc.show(app);



function click(eventInfo){
var app = UiApp.getActiveApplication();
//获取ListBox的值
var value = eventInfo.parameter.myLbName;
//多选框返回逗号分隔的字符串
var n = value.split(',');

var doc = SpreadsheetApp.getActiveSpreadsheet();

SELNAM = value;

返回应用;


解决方案

全局变量不仅与处理函数相关。

Google Apps脚本中的全局变量不能在函数中修改,它们只能读为常量。



坏消息?不是真的,因为有很多可能的解决方法...



获得预期行为的一种可能方式是使用 scriptProperties 来存储全局值(或者如果您希望该值保持专用于同一服务的useProperties你)



在你的函数之外,你可以像下面这样为你的键赋值:

  ScriptProperties.setProperty(key,value); 

然后在需要访问或修改的任何函数中使用它:

  var value = ScriptProperties.getProperty(key); //获得值
//用这个值做一些事情......
ScriptProperties.setProperty(key,value); //更新值

当然还有其他解决方法,比如用户界面中隐藏的窗口小部件,但这一个有很多优点,第一个恕我直言,你可以很容易地在调试时看到脚本编辑器( File / project properties / project properties )的值。


I have a simple, but import question.

I have declared a global variable in my script (Google Spreadsheet Script). Next I am calling a UI application from the SS Script. It has a listbox and a button. It shows fine.

When I click the button, it should put the selected value to the global variable. Problem is inside the handler function (click(eventInfo)), it does not recognise the global variable (SELNAME). How can I overcome this?

//global variable
var SELNAME; 

function show() {
var doc = SpreadsheetApp.getActiveSpreadsheet();
var app = UiApp.createApplication().setTitle('My Application');
var panel = app.createVerticalPanel();
var lb = app.createListBox(true).setId('myId').setName('myLbName');


// add items to ListBox
lb.setVisibleItemCount(8);

lb.addItem("one");
lb.addItem("two");
lb.addItem("three");

panel.add(lb);
var button = app.createButton('press me');
var handler = app.createServerClickHandler('click').addCallbackElement(panel);
button.addClickHandler(handler);
panel.add(button);
app.add(panel);
doc.show(app);
}


function click(eventInfo) {
var app = UiApp.getActiveApplication();
// get values of ListBox
var value = eventInfo.parameter.myLbName;
// multi select box returns a comma separated string
var n = value.split(',');

var doc = SpreadsheetApp.getActiveSpreadsheet();

SELNAM =value;

return app;
}

解决方案

The problem you have with this global variable is not (only) related to handler functions.

Global variables in Google Apps Script can not be modified from within a function, they can only be read as constants.

Bad news ? not really since there are a lot of possible workarounds...

One possible way to get the expected behavior is to use scriptProperties to store the "global" value (or useProperties from the same service if you want that this value remains private to you)

Outside of your function you can assign a value to your key like this :

ScriptProperties.setProperty(key, value);

and then in any function where you need to access or modify it simply use this :

var value = ScriptProperties.getProperty(key);// to get the value
// do something with that value...
ScriptProperties.setProperty(key, value);// to update the value

There are of course other workaround like hidden widgets in the Ui but this one has many advantages, the first one IMHO is that you can easily see the value from the script editor (File/project properties/project properties) while debuging.

这篇关于在UI处理程序Google Spreadsheet Script中访问全局变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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