Reputation: 334
I have very little knowledge in coding with the Ui on Google Apps Script. I am trying to implement a simple form in a Spreadsheet. The spreadsheet do two things:
My step 1 is failing miserably because it keeps giving me a popup error as follows:
Error encountered:
An unexpected error occurred
My code is more complicated than the one below, but even this produces the same error:
function a(e) {
var doc = SpreadsheetApp.getActiveSpreadsheet();
var app = UiApp.createApplication().setTitle('New Name');
var form = app.createVerticalPanel().setId('form');
var grid = app.createGrid(2, 3).setId('grid');
grid.setWidget(0, 0, app.createLabel('Name: '));
grid.setWidget(0, 1, app.createTextBox().setName('name'));
grid.setWidget(0, 2, app.createHTML().setId('nameRequired'));
grid.setWidget(1, 0, app.createButton('Submit')
.addClickHandler(app.createServerClickHandler('b')
.addCallbackElement(grid)));
form.add(grid);
app.add(form);
doc.show(app);
}
function b(e) {
var app = UiApp.getActiveApplication();
if (e.parameter.name == '') app.getElementById('nameRequired')
.setHTML('<font color=\'red\'>* required</font>');
SpreadsheetApp.getActiveSpreadsheet().show(app);
}
Any help would be greatly appreciated. Thank you!
Upvotes: 0
Views: 793
Reputation: 46822
Change the last line in the handler function to return app;
When you reuse SpreadsheetApp.getActiveSpreadsheet().show(app);
in the handler function you are actually trying to add the same instance of the UiApp (that you called with getActiveApplication()
) to the spreadsheet and this is what is generating an error.
code below :
function b(e) {
var app = UiApp.getActiveApplication();
Logger.log(e.parameter.name);
if (e.parameter.name == '') app.getElementById('nameRequired')
.setHTML('* required').setStyleAttributes({'color':'red','fontWeight':'bold'});// use as many pairs you need in the attributes object
return app;//update Ui
}
Upvotes: 1