0

I need global variable in google script, to hold page ID, like a string. Here, they suggested to use object Hidden for this purpose. I can create this object and set its value.

Code to achieve the same :

function doGet(e) {
  var app = UiApp.createApplication();
  //Get current indentificator
  var mid = 'page-id';
  app.add( app.createHidden('mid').setValue(mid).setId('mid'));
  return app;
}

But how can I get this value from another function?

For example :

function maketbl(){
  var app = UiApp.getActiveApplication();
  app.(?!)
}

Thanks!

Community
  • 1
  • 1
rth
  • 2,946
  • 1
  • 22
  • 27

2 Answers2

1

I see that your requirement is to have functionality similar to that of global variables. I suggest that you use script properties, user properties or the cache service to accomplish this feat. An example is below

ScriptProperties.setProperty('special', 'sauce'); // Use this to set the property
var specialValue = ScriptProperties.getProperty('special'); // use this to access the property
Abhishek Ram
  • 324
  • 4
  • 17
  • Thank you @AbhishekRam! It works! Can I use this for save objects? For example: ScriptProperties.setProperty('special', {'a':1,'b':2, 'c':3}); and then var specialValue = ScriptProperties.getProperty('special'); specialValue['a']; – rth Jan 08 '14 at 16:58
  • 1
    The setProperty accepts only strings so the trick here is save the object as string ScriptProperties.setProperty('special', '{'a':1,'b':2, 'c':3}') and then access it like var specialValue = JSON.parse(ScriptProperties.getProperty('special')) and when resaving using thr stringify function. – Abhishek Ram Jan 08 '14 at 17:11
0

Try the bellow code:

function doGet(e) {
  var app = UiApp.createApplication();  
  var mid = 'page-id';
  var hidden = app.createHidden('mid').setValue(mid).setId('mid');
  app.add(hidden);

  //Create your handler
  var handler = app.createServerHandler('maketbl');
  handler.addCallbackElement(hidden)

  //Create a button to trigger your function
  app.add(app.createButton().setText("go forest").addClickHandler(handler));

  return app;
}

function maketbl(e){
  var app = UiApp.getActiveApplication();
  //Retrieve the hidden field
  var hidden = app.getElementById("mid");

  //Show the value stored at e.parameter.mid  where mid is the name of the field
  var dialogBox  = app.createDialogBox().setText(e.parameter.mid);  

  dialogBox.setPopupPosition(100, 100);   
  dialogBox.show();
  return app;
}

Live version here.

br araujo
  • 1,856
  • 2
  • 13
  • 14
  • Thank you br araujo! But how I can get value if my function isn't callback? Is possible to reach this value without making handler? – rth Jan 08 '14 at 13:18
  • Do you mean via javascript? – br araujo Jan 08 '14 at 13:18
  • in Javascript I can use global variables, but in google apps they don't work, unfortunately. So I have to use Hidden object to save some parameters inside uiapp. I hadn't found any way to get this parameter using UiInstance. – rth Jan 08 '14 at 14:13
  • Global variables work like a charm in GAS. I'd rather think that you misunderstand something. If you stay in the same thread you can use global variables. If you change threads you have a callback. BTW you should add a setName("mid") to above code :) – Taras Jan 08 '14 at 16:27
  • 1
    BTW: I don't think so... here the Class UiInstance documentation: createHidden(name) Creates a new Hidden with a specified name. – rth Jan 08 '14 at 17:06