Вопрос

I am rewriting the question to make it easier to answer (I Hope). I want to have a Button inside a class (prototype) but I want the handler to be in the class also. Works fine if the handler is outside class but cannot find it if within.

Thanks in advance for your help.

function myWindow(message) {  
  this.SS = SpreadsheetApp.getActiveSpreadsheet(); 
  this.App = UiApp.createApplication();
  this.App.setTitle(message);
  this.Panel = this.App.createVerticalPanel();
  this.App.add(this.Panel);
  this.Button = this.App.createButton('OK', this.App.createServerHandler('handler'));
};

myWindow.prototype = {
  constructor: myWindow,
  handler: function (e) 
  {
    Browser.msgBox('Click');
  },
  show: function()
  {
    this.Panel.add(this.Button);
    this.SS.show(this.App);
  }
};

function Run() {
  var theWindow = new myWindow('Hello World');
  theWindow.show();
}

The error I get when I click the OK Button is "Script function not found handler"

Это было полезно?

Решение

Ok I have done a lot of reading and although it is not explicitly stated it seems createServerHandler is only designed for global functions and nothing else is provided in the UIAPP. That means what I have asked for cannot be done.

The solution would have to look like

function myWindow(message) {  
  this.SS = SpreadsheetApp.getActiveSpreadsheet(); 
  this.App = UiApp.createApplication();
  this.App.setTitle(message);
  this.Panel = this.App.createVerticalPanel();
  this.App.add(this.Panel);
  this.Button = this.App.createButton('OK', this.App.createServerHandler('handler'));
}

myWindow.prototype = {
  constructor: myWindow,
  show: function() {
    this.Panel.add(this.Button);
    this.SS.show(this.App);
  }
};

function handler(e) {
  Browser.msgBox('Click');    
  };

function Run() {
  var theWindow = new myWindow('Hello World');
  theWindow.show();
};

which is not flash but workable.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top