Reputation: 281
I have this situation. I had done a script few month ago that is still running on a google sites. The doGet Function is :
function doGet(e) {
var myapp = UiApp.createApplication();
var mygrid = myapp.createGrid(5, 2);
var listboxAssociazione=myapp.createListBox().setId("listboxAssociazione").setName("listboxAssociazione");
var labelAssociazione=myapp.createLabel("Associazione").setId("Associazione");
var listboxMeseFatturato=myapp.createListBox().setId("listboxMeseFatturato").setName("listboxMeseFatturato");
var labelMeseFatturato=myapp.createLabel("Mese da fatturare").setId("Mese da fatturare");
var listboxAnnoFatturato=myapp.createListBox().setId("listboxAnnoFatturato").setName("listboxAnnoFatturato");
var labelAnnoFatturato=myapp.createLabel("Anno").setId("Anno");
var buttonCalcola=myapp.createButton().setText("Calcola").setId("CalcolaFattura");
var labelLavoroInCorso=myapp.createLabel("").setId("labelLavoroInCorso").setStyleAttribute('color', 'blue');
mygrid.setWidget(0, 0,labelAssociazione);
mygrid.setWidget(0, 1, listboxAssociazione);
mygrid.setWidget(1, 0, labelMeseFatturato);
mygrid.setWidget(1, 1, listboxMeseFatturato);
mygrid.setWidget(2, 0, labelAnnoFatturato);
mygrid.setWidget(2, 1, listboxAnnoFatturato);
mygrid.setWidget(3, 1, buttonCalcola);
mygrid.setWidget(4, 1, labelLavoroInCorso);
var p=DocsList.getFolders();
var totcartelle = p.length;
var trovato= false;
var cartellaAssociazioni;
for(var j=0;j<totcartelle && trovato==false;j++){
var prova=p[j].getId();
if(p[j].getId()=="0B-H4Ioaio5w5YTg3MGFkOWQtMzYzNy00ZTFhLWEzY2YtZTVlNzIwYWJhMmJm"){
trovato=true;
cartellaAssociazioni=p[j];
}
}
var cartelle =cartellaAssociazioni.getFolders();
var lunghezza = cartelle.length;
for(var i = 0; i < lunghezza; i++) //these arrays are zero based it looks like
{ var prova=cartelle[i].getName();
listboxAssociazione.addItem(prova);
}
//Aggiunta dei mesi alla listbox
listboxMeseFatturato.addItem("Gennaio");
listboxMeseFatturato.addItem("Febbraio");
listboxMeseFatturato.addItem("Marzo");
listboxMeseFatturato.addItem("Aprile");
listboxMeseFatturato.addItem("Maggio");
listboxMeseFatturato.addItem("Giugno");
listboxMeseFatturato.addItem("Luglio");
listboxMeseFatturato.addItem("Agosto");
listboxMeseFatturato.addItem("Settembre");
listboxMeseFatturato.addItem("Ottobre");
listboxMeseFatturato.addItem("Novembre");
listboxMeseFatturato.addItem("Dicembre");
//Aggiunta dell'anno di fatturazione
var d= new Date();
var annoAttuale=parseInt(d.getFullYear());
listboxAnnoFatturato.addItem(""+annoAttuale);
listboxAnnoFatturato.addItem(""+(annoAttuale-1));
var formpanel=myapp.createFormPanel().setId("form");
var serverClickHandler = myapp.createServerClickHandler('GeneraFattura');
serverClickHandler.addCallbackElement(formpanel);
buttonCalcola.addClickHandler(serverClickHandler);
var serverClickHandlerStatus = myapp.createServerClickHandler('ChangeStatus');
serverClickHandlerStatus.addCallbackElement(formpanel);
buttonCalcola.addClickHandler(serverClickHandlerStatus);
formpanel.add(mygrid);
myapp.add(formpanel);
return myapp;
}
//Function to disable the button to avoid double click
function ChangeStatus(e){
var app = UiApp.getActiveApplication();
app.getElementById("labelLavoroInCorso").setText("Attendere! Calcolo della richiesta di pagamento in corso...");
app.getElementById("CalcolaFattura").setEnabled(false);
return app;
}
Now my problem is that when i click the button "Calcola" in the site, it seems that the function "GeneraFattura" runs a number of random times instead of 1.
If i run the same function from the script editor in Google site with the same 3 parameter given by me
var valoreAssociazione = "Volley";
var valoreMese="May";
var valoreAnno = "2012";
instead from the form above, it works correctly.
When i say it works correctly i mean that in the function "GeneraFattura" there is only one line where i copy a spreadsheet. If i run the script from the script editor the output is one copy of the spreadsheet, if i run the script from the sites i get 5 6 copy of the spreadsheet.
Where do you think is the problem??Why a different behaviour between google sites script editor execution and the script execution embedded in the google site??
Thank you in advance
Upvotes: 1
Views: 259
Reputation: 7858
This is a known bug that we are actively working on. Handlers that run more than 30 seconds may end up being called up to 3 times. See this issue:
http://code.google.com/p/google-apps-script-issues/issues/detail?id=1504
Upvotes: 1
Reputation: 46794
Not sure it is the actual reason of your issue but the method createServerClickHandler()
is in the list of deprecated members so I'd suggest to replace it as recommended.
Should be like this : (same modification for both handlers of course ;-)
var serverClickHandler = myapp.createServerHandler('GeneraFattura');
buttonCalcola.addClickHandler(serverClickHandler);
serverClickHandler.addCallbackElement(formpanel);
Note : I would also suggest another approach to avoid multiple click on your button using a client handler. It could go like this :
//Client Handler to disable the button to avoid double click (insert in the UI definition function)
var msg = app.getElementById("labelLavoroInCorso").setText("Attendere! Calcolo della richiesta di pagamento in corso...").setVisible(false);
var Clienthandler = app.createClientHandler()
.forEventSource().setEnabled(false);
.forTargets(msg).setVisible(true);
buttonCalcola.addClickHandler(Clienthandler)
// then at the end of the serverhandler function you will have to re-enable the button and setVisible(false) the message
EDIT : the interrest of the client handler is that it has immediate effect, when you use 2 server handlers you cannot be sure which one will execute first. It also gives a better user experience because of the immediate responsiveness (well, that's my opinion ;-)
Upvotes: 0