I want to make an script that keeps track of the times a button is pressed. It is well known that javascript lacks of support for static vars but this can normally be workaround defining the variable outside of the target function.
Nevertheless this does not work for me on a simple google scripting web application. The code is the following one and it is a simple extension of the template web aplication.
Anyone knows how can this be achieved?
This is the code of the google application:
// 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;
}
There are a few possible ways to achieve that, here is one of them using a hidden widget to hold values.
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;
}
Related
I'm trying to understand how handlers work with validation in Google's UI Service.
If I have a text box with a button and I only want to contact the server if the text box is 1) Not Empty and 2) Contains a specific value, for e.g. 'Fred' how do I create a validation function that checks if the value is 'Fred' before allowing the serverhandler to fire?
Some example code:
function myValid() {
//create the app
var app = UiApp.createApplication();
//set out UI in table
var flex = app.createFlexTable()
.setWidget(0, 0, app.createTextBox().setName('textbox').setId('textbox'))
.setWidget(0, 1, app.createButton('Submit').setId('submit'))
.setWidget(0, 2, app.createLabel().setId('status'));
//server handler - fires only if textbox isn't empty
var serverHandler = app.createServerHandler('submit')
.validateLength(app.getElementById('textbox'), 1, null)
.addCallbackElement(flex);
//my custom handler to check the value of the textbox
var myserverHandler = app.createServerHandler('myCheck')
.addCallbackElement(flex);
//client handler to display a message if textbox is empty
var clientHandler = app.createClientHandler()
.validateNotLength(app.getElementById('textbox'), 1, null)
.forTargets(app.getElementById('status'))
.setText('Cannot be empty');
//add the handlers to the submit button
app.getElementById('submit')
.addClickHandler(serverHandler)
.addClickHandler(myserverHandler)
.addClickHandler(clientHandler);
//add table to UI
app.add(flex);
//show app in the current spreadsheet
var spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
spreadsheet.show(app);
}
//check to see if textbox value is 'Fred'. If not display error
function myCheck(e) {
var app = UiApp.getActiveApplication();
if (e.parameter.textbox != "Fred")
var errorLabel = app.createLabel("Incorrect Value");
app.add(errorLabel);
return app;
}
//If all validation passes then display message
function submit(e) {
var app = UiApp.getActiveApplication();
app.getElementById('status').setText('Server handler fired');
return app;
}
I have attempted to use a serverhandler to check the textbox value but a clienthandler would be better. How do I write a custom client handler to check the textbox value for 'Fred'? Also, how do I prevent the serverhandler firing before all my validation conditions are met?
Thanks
The solution is simpler than your code, one server handler is enough with a single validation on "Fred" and a ClientHandler using validateNotMatches.
Code below.
function myValid() {
var app = UiApp.createApplication();
var textBox = app.createTextBox().setName('textbox').setId('textbox');
var btn = app.createButton('Submit');
var label = app.createLabel().setId('status');
var flex = app.createFlexTable()
.setWidget(0, 0, textBox)
.setWidget(0, 1, btn)
.setWidget(0, 2, label);
var serverHandler = app.createServerHandler('submit')
.validateMatches(textBox, 'Fred')
.addCallbackElement(flex);
var clientHandler = app.createClientHandler()
.validateNotMatches(textBox, 'Fred').forTargets(label).setText('Missing or invalid value');
btn.addClickHandler(serverHandler).addClickHandler(clientHandler);
app.add(flex);
var spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
spreadsheet.show(app);
}
function submit(e) {
var app = UiApp.getActiveApplication();
app.getElementById('status').setText('Server handler fired');
return app;
}
This code accepts any answer "containing" Fred, if you want it to reject anything but "Fred" alone just add a validation on the textBox length == 4 (validateLength,textBox,4,4), I wasn't sure what you really wanted...
Edit : Thanks for accepting, this edit just to mention there is also another approach that might interest you using btn.setEnabled() and a KeyUpHandler, code below... the choice is yours ;-)
function myValid() {
var app = UiApp.createApplication();
var textBox = app.createTextBox().setName('textbox').setId('textbox');
var btn = app.createButton('Submit').setEnabled(false);
var label = app.createLabel().setId('status');
var flex = app.createFlexTable()
.setWidget(0, 0, textBox)
.setWidget(0, 1, btn)
.setWidget(0, 2, label);
var serverHandler = app.createServerHandler('submit')
.validateMatches(textBox, 'Fred')
.addCallbackElement(flex);
btn.addClickHandler(serverHandler);
var btnHandler = app.createClientHandler().validateMatches(textBox,'Fred').forTargets(btn).setEnabled(true);
textBox.addKeyUpHandler(btnHandler);
app.add(flex);
var spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
spreadsheet.show(app);
}
function submit(e) {
var app = UiApp.getActiveApplication();
app.getElementById('status').setText('Server handler fired');
return app;
}
I'm using the code below to run a a function that takes a while to load. My question is, without a lot of modification, is there an easy way of adding something like a clienthandler so that in the midst of running the function it says something along the lines of "Loading...?"
function doGet(e) {
var app = UiApp.createApplication().setTitle('New app');
var grid = app.createGrid(1, 2);
grid.setWidget(0, 0, app.createLabel('First Name + Last Name (Case Sensitive):'));
grid.setWidget(0, 1, app.createTextBox().setName('userName').setId('userName'));
var panel = app.createVerticalPanel();
panel.add(grid);
var buttonPanel = app.createHorizontalPanel();
var button = app.createButton('Submit');
var submitHandler = app.createServerClickHandler('submit')
submitHandler.addCallbackElement(grid);
button.addClickHandler(submitHandler);
buttonPanel.add(button);
var clearButton = app.createButton('Clear');
var clearHandler = app.createServerClickHandler('clear');
clearButton.addClickHandler(clearHandler);
buttonPanel.add(clearButton);
var statusLabel = app.createHTML("").setId('status').setVisible(false);
panel.add(statusLabel);
panel.add(buttonPanel);
app.add(panel);
return app;
};
function clear() {
var app = UiApp.getActiveApplication();
app.getElementById('userName').setValue('');
app.getElementById('status').setVisible(false)
return app;
};
Yes, indeed. That is what client handlers are for.
function doGet(e) {
var app = UiApp.createApplication().setTitle('New app');
var grid = app.createGrid(1, 2);
grid.setWidget(0, 0, app.createLabel('First Name + Last Name (Case Sensitive):'));
grid.setWidget(0, 1, app.createTextBox().setName('userName').setId('userName'));
/* Have the widget ready */
var statusLabel = app.createHTML("").setId('status').setVisible(false);
var panel = app.createVerticalPanel();
panel.add(grid);
var buttonPanel = app.createHorizontalPanel();
var button = app.createButton('Submit');
var submitHandler = app.createServerClickHandler('submit')
submitHandler.addCallbackElement(grid);
button.addClickHandler(submitHandler);
buttonPanel.add(button);
var clearButton = app.createButton('Clear');
var clearHandler = app.createServerClickHandler('clear');
/* Create the client handler */
var plswait = app.createClientHandler().forTargets(statusLabel).setText('Loading...');
clearButton.addClickHandler(clearHandler).addClickHandler(plswait);
buttonPanel.add(clearButton);
panel.add(statusLabel);
panel.add(buttonPanel);
app.add(panel);
return app;
};
function clear() {
var app = UiApp.getActiveApplication();
app.getElementById('userName').setValue('');
app.getElementById('status').setVisible(false).setText('');
return app;
};
It can be more pleasant if you add an image to your status label, here is an example using a transparent animated gif : (simply replace in Srik's code)
var statusLabel = ui.createHorizontalPanel().add(ui.createImage('https://dl.dropboxusercontent.com/u/211279/loading3T.gif'))
.add(ui.createHTML("Loading...")).setId('status').setVisible(false);// Label + animated gif, the text in the HTML widget can be there from the start.
Edit : just noticed something was missing in Srik's code... the clientHandler should go like this :
var plswait = app.createClientHandler().forTargets(statusLabel).setVisible(true);// and eventually setText(' blah blah...') if you didn't define/change it before/elsewhere
Note : now that you have a clientHandler you could also make use of it to setEnabled(false) the button that triggered it to avoid multiple clicks... simply add .forEventSource().setEnabled(false) to your clientHandler.
My code is below. It will accept data from the input text; then it will fetch the recent newest feed from stackoverflow(based in the input) in google spreadsheet.
I have(wanted) set an external gif image to be shown as a progress indicator while fetching of rss feed and its processing is going on.
Problem is: When i remove the commented part; that gif image will never be visible to the user and when that is remained as a comment; that gif image is being loaded by the app after the completion of the process and remained then after.
I want it to be visible only when the process(fetching of rss and processing of it) is going on. So is there any way to make it possible?
function stackoverflow(){
var app =UiApp.createApplication().setHeight(380).setWidth(800);
app.setTitle("Enter tag name");
var mydoc=SpreadsheetApp.getActiveSpreadsheet();
var txtBox = app.createTextBox().setFocus(true).setWidth("150").setId("ticker").setName("ticker");
var mainpanel = app.createHorizontalPanel().setId('mainpanel');
mainpanel.add(txtBox);
var subbtn = app.createButton("Get posts!").setId("tick").setWidth("80");
mainpanel.add(subbtn);
var myloadingimg=app.createImage("http://schedule.msu.edu/img/InProgress.gif").setHeight("26").setWidth("26").setId("loadingimg");
mainpanel.add(myloadingimg.setVisible(false));
app.add(mainpanel);
var panel = app.createVerticalPanel().setId('panel');
var submitHandler = app.createServerHandler("clicker").addCallbackElement(mainpanel);
subbtn.addClickHandler(submitHandler);
app.add(panel);
mydoc.show(app);
}
function clicker(e){
var app = UiApp.getActiveApplication();
var txtBox = app.getElementById("ticker");
app.getElementById("loadingimg").setVisible(true);
var panel=app.getElementById("panel");
app.remove(1);
var panel = app.createVerticalPanel().setId('panel');
var stackurl = "http://stackoverflow.com/feeds/tag?tagnames="+e.parameter.ticker+"&sort=newest";
var stackXML = UrlFetchApp.fetch(stackurl).getContentText();
var stackDOC = Xml.parse(stackXML, false);
var stackentry = stackDOC.getElement().getElements('entry');
for (var i = 0; i < stackentry.length; i++) {
var result = stackentry[i];
panel.add(app.createHTML("<br><b>Post Title: </b>"+ result.getElement('title').getText()+"<br>"));
panel.add(app.createAnchor('', '') .setText("Post URL").setHref(result.getElement('link').getAttribute('href').getValue()));
panel.add(app.createHTML("<br>"));
}
var scroll = app.createScrollPanel().setPixelSize(760, 280);
scroll.add(panel);
app.add(scroll);
//app.getElementById("loadingimg").setVisible(false);
return app;
}
function onOpen()
{
var ss = SpreadsheetApp.getActiveSpreadsheet();
var menuEntries = [];
menuEntries.push({name:'stackoverflow',functionName:'stackoverflow'});
ss.addMenu("f2", menuEntries);
}
You can make use of clientHandlers which are meant for precisely such cases.
function stackoverflow(){
...
var clientHandler = app.createClientHandler().forTargets(myloadingimg).setVisible(true);
var submitHandler = app.createServerHandler("clicker").addCallbackElement(mainpanel);
subbtn.addClickHandler(submitHandler).addClickHandler(clientHandler);
...
}
function clicker(e){
var app = UiApp.getActiveApplication();
....
app.getElementById("loadingimg").setVisible(false);
return app;
}
Does the app.close() not do anything? it does not appear to work....
// Script-as-app template.
Sample code
function doGet() {
var app = UiApp.createApplication();
var button = app.createButton('Click Me');
var closeButton = app.createButton('Close It');
app.add(closeButton);
app.add(button);
var label = app.createLabel('The button was clicked.')
.setId('statusLabel')
.setVisible(false);
var label2 = app.createLabel('The Close button was clicked.')
.setId('statusLabel2')
.setVisible(false);
app.add(label);
app.add(label2);
var closeHandler = app.createServerHandler('close');
closeHandler.addCallbackElement(label);
var handler = app.createServerHandler('myClickHandler');
handler.addCallbackElement(label);
closeButton.addClickHandler(closeHandler);
button.addClickHandler(handler);
return app;
}
function myClickHandler(e) {
var app = UiApp.getActiveApplication();
var label = app.getElementById('statusLabel');
label.setVisible(true);
app.close();
return app;
}
function close(e)
{
Logger.log("Here");
var app = UiApp.getActiveApplication();
var label2 = app.getElementById('statusLabel2');
label2.setVisible(true);
return app.close();
}
Link to sample code
https://script.google.com/a/macros/scotts.com/s/AKfycbz-avIT3bZNJ68_EpRZYXmh66XLJzYI--F0n7DGNn8jL_wlG1k/exec
As documented here:
"This will only work if the app is being shown as a dialog, such as in
a Google Spreadsheet. It will have no effect when called on an app
that is published as a standalone service."
That's a fundamental limit of browsers, by the way - a page can't close itself.
I'am very new to google-apps-script. I encounter this error "TypeError: Cannot find function getValue in object Generic" that i can't figure out why.
Here is the code :
function doGet(e) {
var app = UiApp.createApplication();
app.setTitle("My first application");
var vPanel = app.createVerticalPanel();
var hPanel3 = app.createHorizontalPanel();
hPanel3.add(app.createLabel("Text"));
var textArea = app.createTextArea().setId('theText').setName('theText');
//textArea.setName("textArea");
hPanel3.add(textArea);
vPanel.add(hPanel3);
var hPanel4 = app.createHorizontalPanel();
var button = app.createButton("Save");
var handler = app.createServerHandler('onClick');
handler.addCallbackElement(textArea);
button.addClickHandler(handler);
hPanel4.add(button);
vPanel.add(hPanel4);
var label = app.createLabel().setId('label').setText('tata');
//label.setName("label");
var hPanel5 = app.createHorizontalPanel();
hPanel5.add(label);
vPanel.add(hPanel5);
app.add(vPanel);
return app;
}
function onClick(e) {
var app = UiApp.getActiveApplication();
// ERROR occurs here when the button is clicked
var text = app.getElementById('theText').getValue();
Logger.log(text);
app.getElementById('label').setValue(e.parameter.textArea + ' toto');
//label.setText(textArea.getValue());
return app;
}
I've already change the name and id of the textArea but nothing works.
Thanks for your help!
Trung
The TextArea class has no the getValue method. Its value is passed to a handler as a parameter. Here is a sample demonstrating how it works.
function doGet(e) {
var app = UiApp.createApplication();
var panel = app.createFlexTable();
panel.setWidth('100%');
var btn = app.createButton().setId('btn').setText('Click Me');
var textArea = app.createTextArea().setName('textArea').setValue('Text');
var handler = app.createServerHandler('onBtnClick');
handler.addCallbackElement(panel);
btn.addClickHandler(handler);
panel.setWidget(0, 0, btn);
panel.setWidget(1, 0, textArea);
app.add(panel);
return app;
}
function onBtnClick(e) {
var app = UiApp.getActiveApplication();
var btn = app.getElementById('btn');
var textAreaValue = e.parameter.textArea;
btn.setText(textAreaValue);
return app;
}
Change
var text = app.getElementById('theText').getValue();
by
var text = e.parameter.theText;
e is your form value
you use the name fixed with setName to choose which parameter of e you want to get in the variable
If you refer to the documentation for textArea, there is no such method as getValue() for textArea. As Cartman answered you can get the value through the callbackElement which uses the name of the textArea as parameter (the ID is not necessary unless you want to setValue() with something else)