cvnntg
cvnntg

Reputation: 334

GAS: Error encountered: An unexpected error occurred

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:

  1. If any field is left blank, it should create a warning message.
  2. Once all fields are filled, then information should input into the Spreadsheet (not displayed in the code below).

My step 1 is failing miserably because it keeps giving me a popup error as follows:

https://docs.google.com

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

Answers (1)

Serge insas
Serge insas

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.

update

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

Related Questions