jkmayne
jkmayne

Reputation: 79

How to update a Ui App that is deployed in a Google Spreadsheet, using a server handler?

I have been attempting to create a UiApp that is deployed through a custom Spreadsheet menu. My problem is that when a server handler is called there does not seem to be a good way to return to the "main" or "doGet" function that contains the code of the original Ui.

Typically when returning from a server hanlder the following is used:

app.close();
return app;

However, when deploying a Ui in a Spreadsheet it is recommended using the following for displaying the Ui:

var ss = SpreadsheetApp.getActiveSpreadsheet();
ss.show(app);

I have tried every combination of the two methods that I can seem to find and the Ui either closes when the handler is invoked or crashes.

Please Help! I have included my code below:

function onOpen() { //call to invoke the menu when opening the spreadsheet
var ss = SpreadsheetApp.getActiveSpreadsheet();
var reportMenu = [{name: "Edit Reports", functionName: "deploy"}];
ss.addMenu("Reports", reportMenu);
}

function deploy() {
var app = UiApp.createApplication();
var form = app.createFormPanel();
var flow = app.createFlowPanel();



//creating handlers
var LB1handler = app.createServerClickHandler('checkSelect_');
LB1handler.addCallbackElement(flow);

//creating the flow...
flow.add(app.createLabel("Reports Interface:").setId("l1"));
flow.add(app.createLabel("What would you like to do?"));
flow.add(app.createListBox().addItem("Create a new report").addItem("Edit an Existing Report").setName("LB1"));
flow.add(app.createSubmitButton('Submit').addClickHandler(LB1handler));
form.add(flow);
app.add(form);

var ss = SpreadsheetApp.getActiveSpreadsheet();
ss.show(app);

}

function checkSelect_(e) {
var app = UiApp.getActiveApplication();
//do some stuff to the app
//but how to get back??????
//return app;
// var ss = SpreadsheetApp.getActiveSpreadsheet();
// ss.show(app);
}
//end code

Thanks for any and all help!!

Upvotes: 0

Views: 156

Answers (1)

Srik
Srik

Reputation: 7957

Well, I ran your exact code with return app uncommented and saw an unexpected error. This is because you have a form panel and you have added a click handler.

A SubmitButton in a form panel is supposed to behave like a HTML form. You don't need a click handler for it. When the submit button is clicked, a function called doPost is called. So whatever you want to handle, including creating a new UI, should be done in the doPost.

So, what you should do is either
a. Create a doPost and put your code in there. Something like

flow.add(app.createSubmitButton('Submit'));//.addClickHandler(LB1handler));

AND

function doPost(e){
  var app = UiApp.getActiveApplication();
  app.add(app.createLabel('Hello'));
  return app;
}

b. Use a regular button instead of a submit button. Use the createButton() function. I think you have to place it outside a form panel too.

Upvotes: 2

Related Questions