Google apps script 处理程序中是否可能有一个静态变量?
我想制作一个脚本,记录按下按钮的时间。众所周知,javascript缺乏对静态变量的支持,但这通常是在目标函数之外定义变量的解决方法 尽管如此,在一个简单的google脚本web应用程序上,这并不适用于我。代码如下,它是模板web应用程序的简单扩展 有人知道如何才能做到这一点吗 这是google应用程序的代码:Google apps script 处理程序中是否可能有一个静态变量?,google-apps-script,Google Apps Script,我想制作一个脚本,记录按下按钮的时间。众所周知,javascript缺乏对静态变量的支持,但这通常是在目标函数之外定义变量的解决方法 尽管如此,在一个简单的google脚本web应用程序上,这并不适用于我。代码如下,它是模板web应用程序的简单扩展 有人知道如何才能做到这一点吗 这是google应用程序的代码: // Script-as-app template. function doGet() { var app = UiApp.createApplication(); var b
// Script-as-app template.
function doGet() {
var app = UiApp.createApplication();
var button = app.createButton('Click Me');
app.add(button);
var label = app.createLabel('The button was clicked.')
.setId('statusLabel')
.setVisible(false);
app.add(label);
myClickHandler.counter = 0;
var handler = app.createServerHandler('myClickHandler');
handler.addCallbackElement(label);
button.addClickHandler(handler);
return app;
}
function myClickHandler(e) {
var app = UiApp.getActiveApplication();
var label = app.getElementById('statusLabel');
label.setVisible(true);
label.setText('Clicked ' + myClickHandler.counter + ' times.')
myClickHandler.counter++;
//app.close();
return app;
}
有几种可能的方法可以实现这一点,下面是其中一种使用隐藏小部件来保存值的方法
function doGet(){
var app = UiApp.createApplication();
var button = app.createButton('Click Me');
app.add(button);
var counterValue = 0;
var label = app.createLabel('The button was clicked.')
.setId('statusLabel')
.setVisible(false);
var counter = app.createHidden('counter').setId('counter').setValue(counterValue)
app.add(label).add(counter);
var handler = app.createServerHandler('myClickHandler');
handler.addCallbackElement(counter);
button.addClickHandler(handler);
return app;
}
function myClickHandler(e) {
var app = UiApp.getActiveApplication();
var label = app.getElementById('statusLabel');
label.setVisible(true);
var counterValue = Number(e.parameter.counter)
var counter = app.getElementById('counter')
counterValue++;
counter.setValue(counterValue.toString())
label.setText('Clicked ' + counterValue + ' times.')
return app;
}
我一直在寻找更严格的东西,但我不能否认这真的成功了。非常感谢。