Changing a Textbox to a Button in Google Script - google-apps-script

I'm working on a todo list program in google script. I have it so that people can type in their task in a textbox, but I want it so that once they hit the enter key, the textbox turns into a button that, that says what the task is, and they will later press when they finished that task and it will return to a textbox. I can't figure out how to make any changes to the app from inside another function.
What I have so far is:
function doGet(e) {
var app = UiApp.createApplication();
var panel = app.createAbsolutePanel().setHeight("750px").setWidth("1600px");
var button = app.createButton("swag");
var enterHandler = app.createServerKeyHandler('enterPress');
enterHandler.addCallbackElement(panel);
var box = app.createTextBox().addKeyUpHandler(enterHandler);
panel.add(box,0,0);
app.add(panel);
return app;
}
function enterPress(e){
var app = UiApp.getActiveApplication();
if (e.parameter.keyCode==13){
var button2 =app.createButton('swag');
var panel = app.createAbsolutePanel().setHeight('750px').setWidth('1600px');
panel.add(button2,0,0);
app.add(panel);
}
return app;
}
It recognizes the enter press, but the changes won't return to the main app.

You can do it like this :
note : I didn't "place" the button, I guess you have an idea on how you want them to show up...
You could also use a grid to place them... as you want.
function doGet(e) {
var app = UiApp.createApplication();
var panel = app.createAbsolutePanel().setHeight("750px").setWidth("1600px");
var enterHandler = app.createServerKeyHandler('enterPress').addCallbackElement(panel);
var button = app.createButton("confirm",enterHandler).setId('swag');
var tBox = app.createTextBox().setName('tBox').setId('tBox').setWidth(300).setStyleAttributes({'textAlign':'center'});
var bBox = app.createButton("no Text",enterHandler).setVisible(false).setId('bBox').setWidth(300);
panel.add(tBox,20,20).add(bBox,20,20).add(button,140,50);
app.add(panel);
return app;
}
function enterPress(e){
var app = UiApp.getActiveApplication();
if (e.parameter.source=='bBox'){
app.getElementById('bBox').setVisible(false);
app.getElementById('tBox').setVisible(true);
}
if (e.parameter.source=='swag'){
app.getElementById('bBox').setVisible(true).setHTML(e.parameter.tBox);
app.getElementById('tBox').setVisible(false);
}
return app;
}
Test here
Edit: code modified with placement .
EDIT2 : You could also hide the enter button, I find it nicer.
function enterPress(e){
var app = UiApp.getActiveApplication();
if (e.parameter.source=='bBox'){
app.getElementById('bBox').setVisible(false);
app.getElementById('tBox').setVisible(true);
app.getElementById('swag').setVisible(true);
}
if (e.parameter.source=='swag'){
app.getElementById('bBox').setVisible(true).setHTML(e.parameter.tBox);
app.getElementById('tBox').setVisible(false);
app.getElementById('swag').setVisible(false);
}
return app;
}

Related

Message Box in popup form

I have a doubt,
I make a Button for Submit form and when I press the first button appear to me a message of confirmation to continue or not, but when I press "no" and the message disappear and return to form,but the first button doesn't work more.
You known that I make wrong.
thanks for all.
function doGet(){
var app = UiApp.createApplication().setTitle('Request Form');
var flow = app.createFlowPanel().setStyleAttribute("textAlign", "center").setWidth("900px");
var buttons = app.createButton('Print and Save').setId('buttons');
var handlers = app.createServerClickHandler('question').addCallbackElement(flow);
buttons.addClickHandler(handlers);
flow.add(buttons);
app.add(flow);
return app;
}
function question(e){
var app = UiApp.getActiveApplication();
var dialog = app.createPopupPanel().setModal(true).setSize(700, 100).setPopupPosition(10, 400);
var closeHandler = app.createClientHandler().forTargets(dialog).setVisible(false);
var handlersend = app.createServerClickHandler('Send');
var handleract =app.createServerClickHandler('activateAgain');
var labelp = app.createLabel('Are you sure that this information is correct?')
var buttonp1= app.createButton('yes, continue').addClickHandler(closeHandler).addClickHandler(handlersend);
var buttonp2= app.createButton('No, I want correct').addClickHandler(closeHandler).addClickHandler(handleract);
app.getElementById('buttons').setEnabled(false);
var gridp = app.createGrid(1,5);
gridp.setWidget(0, 0, labelp)
.setWidget(0,1,buttonp1)
.setWidget(0,2,buttonp2);
dialog.add(gridp)
dialog.show();
return app;
}
function activateAgain(e){
var app = UiApp.getActiveApplication();
app.getElementById('buttons').setEnabled(true);
return app;
}
function Send(e){
}
You are trying to make it happen in clientHandler which is basically a good idea but the problem is that some methods are not available in clientHandlers, for example hide() is not !
So you used setVisible(false) to hide the dialog but then you can't get it visible again easily...
All in all, I found it was easier with serverHandler and a few modifications.
Since you seem to be quite comfortable with UiApp I don't need to explain further, the code is self explanatory enough (I hope :-)
Code below : (and test HERE)
function doGet(){
var app = UiApp.createApplication().setTitle('Request Form');
var flow = app.createFlowPanel().setStyleAttribute("textAlign", "center").setWidth("900px");
var buttons = app.createButton('Print and Save').setId('buttons');
var handlers = app.createServerClickHandler('question').addCallbackElement(flow);
buttons.addClickHandler(handlers);
flow.add(buttons);
app.add(flow);
return app;
}
function question(e){
var app = UiApp.getActiveApplication();
var dialog = app.createPopupPanel().setModal(true).setSize(700, 100).setPopupPosition(10, 400).setId('dialog');
var button = app.getElementById('buttons').setEnabled(true);
var closeHandler = app.createClientHandler().forTargets(button).setEnabled(true);
var correctHandler = app.createServerHandler('hideDialog');
var handlersend = app.createServerClickHandler('Send');
var labelp = app.createLabel('Are you sure that this information is correct?')
var buttonp1= app.createButton('yes, continue').addClickHandler(handlersend);
var buttonp2= app.createButton('No, I want correct').addClickHandler(closeHandler).addClickHandler(correctHandler);
app.getElementById('buttons').setEnabled(false);
var gridp = app.createGrid(1,5);
gridp.setWidget(0, 0, labelp)
.setWidget(0,1,buttonp1)
.setWidget(0,2,buttonp2);
dialog.add(gridp)
dialog.show();
return app;
}
function hideDialog(){
var app = UiApp.getActiveApplication();
var dialog = app.getElementById('dialog');
dialog.hide();
return app;
}
function Send(e){
var app = UiApp.getActiveApplication();
var dialog = app.getElementById('dialog');
dialog.hide();
app.getElementById('buttons').setEnabled(false);
app.add(app.createLabel('send'));
return app;
}

Open new UI without closing the actual opened Ui

What I want is simply that when I press a button in the Actual Ui, opens a new Ui without closing the Actual Ui.
Here is a simple code that I did:
function shows(){
var app = UiApp.createApplication().setTitle("Actual Ui")
.setHeight(3000).setWidth(1350);;
var button = app.createButton('Button');
var pan = app.createHorizontalPanel();
pan.add(button)
var clickHandler = app.createServerClickHandler("ps");
button.addClickHandler(clickHandler);
clickHandler.addCallbackElement(pan);
app.add(pan);
var doc = SpreadsheetApp.getActive();
// show the app
doc.show(app)
}
function ps(e){
var apps = UiApp.createApplication().setHeight(50).setWidth(200);
apps.setTitle("New UI");
var docsss = SpreadsheetApp.getActive();
docsss.show(apps);
return apps
}
The code works perfectly but the fact that when I press the button it closes the Actual Ui and opens the new one.
You can't do it like that, here is how it works, use the same app instance and change its content.
function shows(){
var app = UiApp.createApplication().setTitle("common Ui")
.setHeight(400).setWidth(1350);;
var button = app.createButton('Button');
var pan = app.createHorizontalPanel().setId('panelUi1');
pan.add(app.createLabel(' first Ui'));
pan.add(button)
var clickHandler = app.createServerClickHandler("ps");
button.addClickHandler(clickHandler);
clickHandler.addCallbackElement(pan);
app.add(pan);
var doc = SpreadsheetApp.getActive();
// show the app
doc.show(app)
}
function ps(e){
var app = UiApp.getActiveApplication();
app.getElementById('panelUi1').setVisible(false);
app.add(app.createLabel('new Ui in the same app instance'));
return app;
}

use return app; without ending current function

Is there another way to update the gui than return app;?
I want to set the text on a label before doing an url fetch, like started downloading, and after it completes turn the label into download complete.
function EventHandler(e) {
var app = UiApp.getActiveApplication();
var url = e.parameter.URLInput;
var label = app.getElementById("label");
label.setText("Download started");
try{
var file = UrlFetchApp.fetch(url).getBlob();
} catch(err){
label.setText(err);
}
label.setText("Download finished");
return app;
}
The label stays empty until UrlFetchApp is completed, and then the label's content is 'Download finished'. Adding return app; before the fetch ends the function.
You have to use a clientHandler to set Text to your label in the doGet function, the clientHandler executes immediately when you click the button.
Here is a test app that shows how it works: (online test available here with simulated download)
function doGet(){
var app = UiApp.createApplication();
var label = app.createLabel('---empty---').setId('label');
app.add(label)
var handler = app.createServerHandler('EventHandler');
var cHandler = app.createClientHandler().forTargets(label).setText('starting download');
var btn = app.createButton('start',handler).addClickHandler(cHandler);
app.add(btn);
return app;
}
function EventHandler(e) {
var app = UiApp.getActiveApplication();
var url = e.parameter.URLInput;
var ulabel = app.getElementById("label");
ulabel.setText("Download started");
try{
//var file = UrlFetchApp.fetch(url).getBlob();
} catch(err){
label.setText(err);
}
ulabel.setText("Download finished");
return app;
}
note : you can use the same client handler to do lots of other usefull things : disable the button, show a spinner... whatever you like that must happen in the doGet function without delay.
EDIT following your comment
Have you tried using 2 server handlers in parallel ? in the displayHandler you could setup any condition you want, I left it simple in the following example :
function doGet(){
var app = UiApp.createApplication();
var label = app.createLabel('---empty---').setId('label');
app.add(label)
var handler = app.createServerHandler('EventHandler');
var displayHandler = app.createServerHandler('displayHandler');
var btn = app.createButton('start',handler).addClickHandler(displayHandler);
// you can add other handlers (keypress, hover... whatever) they will all execute at the same time
app.add(btn);
return app;
}
function displayHandler(e) {
var app = UiApp.getActiveApplication();
var ulabel = app.getElementById("label");
ulabel.setText("Download started");
return app;
}
function EventHandler(e) {
var app = UiApp.getActiveApplication();
var url = e.parameter.URLInput;
var ulabel = app.getElementById("label");
try{
Utilities.sleep(2000);// simulating download
} catch(err){
label.setText(err);
}
ulabel.setText("Download finished");
return app;
}

doPost Twice in Google Apps Script?

I have a web-deployed form written in Google Apps Script with doGet and doPost. In the doPost, the code checks if the user has filled in the form correctly (e.g. not leaving certain things blank). If not, it highlights the things that need to be fixed and adds a warning label. If everything is all right, it writes the form data to a spreadsheet.
The problem is that it doesn't seem like doPost can be called again if the user fixes the problems.
Any thoughts? Thanks!
EDIT: I am using UiService
EDIT: Here is a very simplified version of the app:
function doGet(e) {
var app = UiApp.createApplication();
var mainForm = app.createFormPanel().setId('mainForm');
var formContent = app.createVerticalPanel().setId('formContent');
var userName = app.createTextBox().setId('userName').setName('userName');
var passport = app.createFileUpload().setName('passport');
var submitButton = app.createSubmitButton('submit here');
var submitButtonWarning = app.createLabel('Something is wrong.').setId('submitButtonWarning')
.setVisible(false);
formContent
.add(userName)
.add(passport)
.add(submitButton)
.add(submitButtonWarning);
mainForm.add(formContent);
app.add(mainForm);
return app;
}
function doPost(e) {
var app = UiApp.getActiveApplication();
var userName = e.parameter.userName;
var passport = e.parameter.passport;
if (userName == 'no') {
app.getElementById('submitButtonWarning').setVisible(true);
app.add(app.getElementById('formContent'));
return app;
} else {
app.getElementById('submitButtonWarning').setVisible(false);
app.add(app.getElementById('formContent'));
return app;
}
return app;
}
I think you are just adding the wrong UI element to the app in your doPost.
Instead of
app.add(app.getElementById('formContent'));
use
app.add(app.getElementById('mainForm'));

How can I hide a label after few seconds using google apps script?

I have a simple application where the user press a button and a welcome message is shown. I need this message(label) to be hidden after few seconds, say 5 secs.
I couldn't find a function like setTimeout() in google apps script.
Can someone give an idea how I could implement this?
(as you can see, not an experienced programmer).
Thanks!!
A possible solution. The logic is to have two the button click handlers. the 1st one makes the label visible and the 2nd one sleeps 5 seconds and after hides the label.
function doGet(e) {
var app = UiApp.createApplication();
var panel = app.createVerticalPanel();
var btn = app.createButton().setText('Test');
var lblVisible = app.createLabel('Visible Test').setVisible(false).setId('lblVisible');
panel.add(btn);
panel.add(lblVisible);
var handler = app.createServerHandler('onBtnClick');
var handlerWait = app.createServerHandler('onWaitEvent');
handler.addCallbackElement(panel);
handlerWait.addCallbackElement(panel);
btn.addClickHandler(handler);
btn.addClickHandler(handlerWait);
app.add(panel);
return app;
}
function onWaitEvent(e) {
Utilities.sleep(5 * 1000);
var app = UiApp.getActiveApplication();
var lblVisible = app.getElementById('lblVisible');
lblVisible.setVisible(false);
return app;
}
function onBtnClick(e) {
var app = UiApp.getActiveApplication();
var lblVisible = app.getElementById('lblVisible');
lblVisible.setVisible(true);
return app;
}
Progress indicator solution etc, this works. It lets you chain events, I have used it several times.
Updating a widget value on the go. productforums.google.com/d/topic/apps-script/lABoP-cJcGQ/…