The following is documented on stackoverlow and not directly related to my GSoC work but in case Google directs someone with the same problem here and as a "”warning” for any New Visioners I decided to document it here.
I was trying out the new library service for Apps, however I was working on the assumption that if the script works on my end it would work as a library the same way, and it is true for most things. But, I had very very bad luck and most of my script
depended on handlers (had a nice GUI) so I did not test it as a library but only wrote code in my editor and tested it from there, after 3-4 days of very hard work I had a surprise, a bad one, “Unknown macro” was the error and I got it from every handler not defined in the main panel. To make this clear let’s say the code was this:
1 function notWorkingGUI(){
2
3 var app=UiApp.createApplication();
4 var appPanel=app.createVerticalPanel().setId("appPanel");
5 var handler1=app.createServerHandler("handlerFunction1").addCallbackElement(appPanel);
6 var firstButton=app.createButton("Button 1", handler1);
7
8 appPanel.add(firstButton);
9 app.add(appPanel);
10
11 SpreadsheetApp.getActive().show(app);
12
13 }
14
15 function handlerFunction1(e){
16
17 var app=UiApp.getActiveApplication();
18
19 var appPanel2=app.createVerticalPanel().setId("appPanel2").setStyleAttribute("zIndex", 0).setStyleAttribute("position", "fixed");
20 var handler2=app.createServerHandler("handlerFunction2").addCallbackElement(appPanel2);
21 var secondButton=app.createButton("Button 2", handler2);
22 var label=app.createLabel("This should get visible after the click").setId("label").setVisible(false);
23
24 appPanel2.add(secondButton).add(label);
25 app.add(appPanel2);
26
27 return app;
28
29
30 }
31
32 function handlerFunction2(e){
33
34 var app=UiApp.getActiveApplication();
35
36 app.getElementById("label").setVisible(true);
37
38 return app;
39
40 }
Now If You would copy-paste this to Your Apps Script editor it would work like a charm, however if You would make it a library and call it like this:
1 function notWorking(){
2
3
4 Error.notWorkingGUI()
5
6
7 }
Only the first button click would be recognized, when clicking the second button the unknown macro error would be thrown.
Now, I could not find a solution anywhere so I “invented” my own,. I do not recommend it, but if this bug is not fixed an You really really need to have something like the code above (that is a handler created as a response to a handler) You can do it like this:
1 function workingGUI(){
2
3 //previous first part
4 var app=UiApp.createApplication();
5 var appPanel=app.createVerticalPanel().setId("appPanel");
6 var handler1=app.createServerHandler("handlerFunction1a").addCallbackElement(appPanel);
7 var firstButton=app.createButton("Button 1", handler1);
8
9
10 //previous second part
11 var appPanel2=app.createVerticalPanel().setId("appPanel2").setStyleAttribute("zIndex", 0).setStyleAttribute("position", "fixed");
12 var handler2=app.createServerHandler("handlerFunction2a").addCallbackElement(appPanel2);
13 var secondButton=app.createButton("Button 2", handler2).setId("button2");
14
15
16 appPanel.add(firstButton);
17 app.add(appPanel);
18
19 SpreadsheetApp.getActive().show(app);
20
21 }
22
23 function handlerFunction1a(e){
24
25 var app=UiApp.getActiveApplication();
26
27
28
29 var label=app.createLabel("This should get visible after the click").setId("label").setVisible(false);
30
31 app.getElementById("appPanel2").add(app.getElementById("button2")).add(label);
32 app.add(app.getElementById("appPanel2"));
33
34 return app;
35
36
37 }
38
39 function handlerFunction2a(e){
40
41 var app=UiApp.getActiveApplication();
42
43 app.getElementById("label").setVisible(true);
44
45 return app;
46
47 }
48
49
Again I do not recommend this but it works. Also before You go ahead and try solving it with a class (OOP) I tried Apps will not recognize a handler if it is part of the same class the GUI code is in. I also tried with making two classes, one which would have the code for the GUI and another with all the handlers, was a dead end.
Cheers,
No comments:
Post a Comment