Google Script - Modal dialog - screen freezes after closing the dialog - google-apps-script

I created the following code to create a modal dialog: it is works until it is closed. After closing - the page from where this was called stops responding. Unable to click inside any text box / click any button at all.
var app = UiApp.createApplication(); var dialog =
app.createDialogBox().setModal(true).setText('Record
Saved').setPopupPosition(200, 200).show(); var closeHandler =
app.createClientHandler().forTargets(dialog).setVisible(false); var
button=
dialog.setWidget(app.createButton('Ok').addClickHandler(closeHandler));
app.add(dialog); return app;
Is there something amiss with that code?
Edit:
I amended the code as follows:
Removed the following line:
var app = UiApp.createApplication();
Revised code looks as follows:
var dialog = app.createDialogBox().setModal(true).setText('Record
Saved').setPopupPosition(200, 200).show(); var closeHandler =
app.createClientHandler().forTargets(dialog).setVisible(false); var
button=
dialog.setWidget(app.createButton('Ok').addClickHandler(closeHandler));
app.add(dialog); return app;
The current situation is that the freezing issue is resolved now. The new issue is that the Modal design is gone. The dialog appears at the bottom. Functionality is fine - it is just that modality is gone.
Will continue the search and keep all of us posted. Thank you.
Update:
Improvement in code - hides only the button and not dialog
var dialog =
app.createDialogBox().setModal(true).setPopupPosition(200,
200).show(); var closeHandler = app.createClientHandler()
.forEventSource()
.setVisible(false); var savebutton = dialog.setWidget(app.createButton("Record
Saved").addClickHandler(closeHandler));

This did the trick. Thanks Team.
app.createDialogBox().setModal(true).setPopupPosition(200,
200).setAutoHideEnabled(true).setText("Record Saved").show();

Related

modal dialog with new spreadsheet api issue

I'm was trying to show html body of gmail message in modal dialog window but faced an error calling SpreadsheetApp.getUi() method.
...
var html = HtmlService.createHtmlOutput(threads[i].getMessages()[0].getBody());
SpreadsheetApp.getUi().showModalDialog(html, 'My add-on');
...
How I can call that dialog? Or may be there is better aproach to display message(some panel or sidebar)?
This prompt box is misleading, as the error must be elsewhere. The new version of Sheets not only does support the getUI method, I believe it's specifically designed for for the new version of sheets.
I've expanded on your previous question and tested this myself with the following code in the new version of sheets:
function getMail() {
var threads = GmailApp.getInboxThreads();
var messages = threads[0].getMessages()[0];
var raw = messages.getPlainBody();
return raw;
}
function dialogueBox(){
var raw = getMail();
var htmlOutput = HtmlService.createHtmlOutput(raw);
SpreadsheetApp.getUi().showModalDialog(htmlOutput, 'For mk_yo');
}
and it displays the prompt with no issues, as shown here. Try creating a new sheet with a new script and run the code above. Additionally, you can try ensuring that the sheet that you're adding this to is definitely using the new version of sheets.
In relation to if there's a better approach by displaying a side panel, yes you can use custom sidebars in the new version of Google sheets(and only the new version), but as this still uses the 'getUI' method, this won't resolve your current error, as this is not where the issue lies.

Using server handlers with modal dialogs

I am displaying a User Interface over a sheet using showModalDialog passing in the app I just created. I also setup a button with a server handler. When server handler function is called I try to get the app again using "UiApp.getActiveApplication()" to hide some elements and show some different elements, however, the changes are not reflected. At the end of the method I tried to close the app, and show a new modal dialog, I tried to return the app, I tried to do nothing, and nothing seems to work.
I can't post my whole code since it is very long, so I made a very simple version that gets the point across. When I put some logging statements in testHandler() it proves that the code is running.
function test() {
var app = UiApp.createApplication().setHeight(700).setWidth(1500);
var label = app.createLabel("Hi").setId("label");
var label2 = app.createLabel("GoodBye").setId("label2").setVisible(false);
var button = app.createButton("Press Me").setId("button");
app.add(label);
app.add(label2);
app.add(button);
var testHandler = app.createServerHandler('testHandler');
testHandler.addCallbackElement(label);
testHandler.addCallbackElement(label2);
button.addClickHandler(testHandler);
SpreadsheetApp.getUi().showModalDialog(app, 'Test');
}
function testHandler() {
var app = UiApp.getActiveApplication();
app.getElementById('label').setVisible(false);
app.getElementById('label2').setVisible(true);
// Not sure what to do now
}
Thank you in advance for your help
return app; //where you are not sure what do do

GUI apps script, returning "undefined " value

i've created simple GUI with a flow panel name and ID main panel, a label name Label1 a textbox name myTextBox and a button with ID getETA.
my aim is if i enter a value in text box and click submit den the value should write in spreadsheet.
my problem is the script is returning undefined in spreadsheet not the actual value i've entered.
var app = UiApp.createApplication();
app.setTitle("My Application");
app.add(app.loadComponent("MyGui"));
SpreadsheetApp.getActiveSpreadsheet().show(app);
var clickHandler = app.createServerHandler('clickGetETA');
clickHandler.addCallbackElement(app.getElementById('mainPanel'));
app.getElementById('getETA').addClickHandler(clickHandler);
var doc = SpreadsheetApp.getActive();
doc.show(app);
}
// this function responds to submit button
function clickGetETA(e) {
var app = UiApp.getActiveApplication();
var textBoxValue = e.parameter.myTextBox;
var sheet = SpreadsheetApp.getActiveSheet();
var lastRow = sheet.getLastRow()+1;
var lastCell = sheet.getRange("A"+lastRow);
lastCell.setValue(textBoxValue);
return app.close();
}
I'm new to stackoverflow dont have enough reputations to post image so posting links of images
image1
image2
image3
image4
image5
image6
image7
I see that you defined the clickHandler in the script AND in the GUI builder... that is one too much, that's probably the cause of the issue
Please try to remove the one in the GUI builder and test again, I don't see any other problem in your test ;-)
You could also of course remove the handler in the script but in that case you should add the callbackelement in there too by developing the smal '+' near the handler name and put your panel there.
EDIT : Sorry, there are 2 handlers in the GUI builder, so that is two too much ! you cannot use a single handler with a single name on 2 different handler type (click & key) if you want to have multiple handlers on a button you should define as many handlers you need, each of the type you want, eventually calling the same function but with different variable names.
Edit 2 the line SpreadsheetApp.getActiveSpreadsheet().show(app); in the begining of the main function souldn't be there either, just remove it since you call the spreadsheet later with var doc = SpreadsheetApp.getActive();
Working code with GUI here
EDIT 3 : issue solved by sharing the questioner sheet, the panel name was not correctly written : mainPanel in the script and mainpanel in the GUI ...
Aaaah, case sensitiveness ! ;-) (visible in image6)

google apps script - web app doesn't seem to do anything . .

I'm a Java developer but I did a small site for a non-profit group using Google Sites. I have a form I'd like to be somewhat dynamic and Google Apps Script seemed to be a viable option. As frequently happens when one is learning a new technology, I copied and pasted the code below from a tutorial/documentation. I then published it, and inserted the script widget into a page on the site and saved the page. When I reload the page, the "place holder" for the widget is there, but nothing happens - no buttons, no panel, nothing. Same results when I run it from the script editor. I'm sure I'm missing something obvious, but I haven't been able to get the UI to render at all. A little direction would be greatly appreciated.
thanks in advance!
function doGet(e) {
Logger.log("Executing the doGet() method . . .");
var app = UiApp.createApplication();
var aPanelRoot = app.createVerticalPanel();
var button = app.createButton('Click Me');
aPanelRoot.add(button);
var label = app.createLabel('The button was clicked.');
label.setId('statusLabel');
aPanelRoot.add(label);
var handler = app.createServerHandler('myClickHandler');
handler.addCallbackElement(label);
button.addClickHandler(handler);
aPanelRoot.setVisible(true);
return app;
}
function myClickHandler(e) {
var app = UiApp.getActiveApplication();
var label = app.getElementById('statusLabel');
label.setVisible(true);
//app.close();
return app;
}
It seems that you simply forgot to add aPanelRoot to the app in the doGet() function
app.add(aPanelRoot)
also : by default all widgets are visibles so you can remove all the setVisible(true) statements as they are only necessary if you set them to false somewhere else...
And if I may add a last comment, it's generally a good idea to choose the parent widget as callbackElement so you don't risk to forget to add elements when you begin to have lots of them (all children are automatically included in the parent) .

Replicate tab Panel in GUI Builder google apps

I think I've seen this answer, but I can't remember where for certain.
I'm trying to to create a tabbed panel interface using the GUI Builder, but don't see that option. The part I seem to recall is someone having an approach to replicate that in the GUI Builder. I just can't seem to find that information in my brain, the old google groups or here.
Can someone jog my memory?
Thank you...
Maybe the post you were referring to was this one ? Anyway, no matter how much panels you have, yo could design them in the GUI, one on top of the other or (more simply) one under each other in a 'parent' vertical panel and play with client handlers to show/hide the one you need.
I have an example here with 2 panels in an UI, the UI is designed with script but that is not important, look at the client handlers to see how it works.
If I have some free time tonight I'll make a demo script for 4 panels in GUI ;-).
EDIT : here is a test example (standalone) or embedded in a Google site + link to the script (make a copy to edit)
Note that in the GUI builder you'l have to 'play' with visibility of each panel to work on it, I used a main panel large enough to hold 2 panels together so you can have a better vision of "harmony" between panels (which is not the case in my test;-))
and the code (very simple basic example 4 panels with each of them a textBox & a Label, just to test the handlers on the buttons):
function doGet() {
var app = UiApp.createApplication();
var UI=app.loadComponent('multiUi')
var panel1 = app.getElementById('panel1')
var panel2 = app.getElementById('panel2')
var panel3 = app.getElementById('panel3')
var panel4 = app.getElementById('panel4')
var Button1 = app.getElementById('Button1')
var Button2 = app.getElementById('Button2')
var Button3 = app.getElementById('Button3')
var Button4 = app.getElementById('Button4')
var pHandler1 = app.createClientHandler()
.forTargets(panel1).setVisible(true).forTargets(panel2,panel3,panel4).setVisible(false)
Button1.addClickHandler(pHandler1)
var pHandler2 = app.createClientHandler()
.forTargets(panel2).setVisible(true).forTargets(panel1,panel3,panel4).setVisible(false)
Button2.addClickHandler(pHandler2)
var pHandler3 = app.createClientHandler()
.forTargets(panel3).setVisible(true).forTargets(panel2,panel1,panel4).setVisible(false)
Button3.addClickHandler(pHandler3)
var pHandler4 = app.createClientHandler()
.forTargets(panel4).setVisible(true).forTargets(panel2,panel3,panel1).setVisible(false)
Button4.addClickHandler(pHandler4)
app.add(UI)
return app;
}
The following code makes the tabs, based on the array you put in, dynamically:
function doGet() {
// create application
var app = UiApp.createApplication();
// set array
var aTabs = ['donald','katrijn','dagobert'];
// create tab panel
var pTab = app.createTabPanel();
// add tabs to panel
for(var k=0; k<pTabs.length; k++) {
pTab.add(app.createLabel("This tab is reserved for " + aTabs[k]), aTabs[k]);
}
// add panel to application
app.add(pTab);
// set focus to first tab
pTab.selectTab(0);
// return to application
return app;
}
See link for tabPanel reference.
Publishing your script as a web-app, allows you to insert the script on a google sites.