Search code examples
google-apps-scriptgoogle-sites

Improve my script run time


Is there anyway to improve my script run time? I have a script that creates 2 listboxes: Listbox1 items is all my google site pages, listbox2 items is the sub-pages of listbox1 page. The script runs fine but sometimes it takes between 2 and 5 seconds to get all of the listbox2 items.

You can try my script here.

And here is my script:

function doGet() 
{
  var app = UiApp.createApplication();
  //GUI with 2 listbox
  //Listbox1: onclick > lbox1onclick(e), onchange > lbox1onchange(e)
  app.add(app.loadComponent("MyUrlParser")); 
  var lbox1 = app.getElementById('ListBox1');
  lbox1.addItem(' ');
  var lbox1_Item = SitesApp.getSite('phichdaica').getChildByName('manga').getChildren();
  for(var i = lbox1_Item.length-1; i >= 0; i--)
  {
      lbox1.addItem(lbox1_Item[i].getTitle());
  }
  return app;  

}

function lbox1onclick(e)
{
  var app = UiApp.getActiveApplication();
  var lbox2 = app.getElementById('ListBox2');
  lbox2.clear();
  return app;
}

function lbox1onchange(e)
{
  var app = UiApp.getActiveApplication();
  // var value = e.parameter.lbox1;
  var lbox1value = e.parameter.ListBox1;
  var lbox2 = app.getElementById('ListBox2');


  var lbox2_Item = SitesApp.getSite('phichdaica').getChildByName('manga').getChildByName(lbox1value).getChildren();

  for(var i=lbox2_Item.length-1; i >= 0; i--)
  {
    lbox2.addItem(lbox2_Item[i].getTitle());
  }
  return app;
}  

Solution

  • I don't think it will speed up the process but you could use just one handler function to do that : on change listBox1, clear listBox 2 and re-populate it immediately. What is taking some time is the call to site's content so the difference might not be significative but the 'logic' of your script would be improved ;-)

    Looking at your page, I see that listBox 2 is never cleared... is that a temporary issue ? Did you change something recently ?

    Also, what is supposed to happen when something is selected in listBox2 ?

    EDIT : following your comment, if you want to improve user experience concerning the 'responsiveness' of your UI the best way is to use client handlers to trigger the visibility of a 'wait message' for example(something like "Updating the list"). I usually use an animated gif that I made visible with a client handler and that is made invisible again when the server handler returns (ie I set it invisible in the server handler function).

    here is a working example, just try to change the date in the upper right corner.