HTML BUTTON TO LAUNCH A SCRIPT

2,116 views
Skip to first unread message

CHARLIE BOUVIER

unread,
Aug 16, 2022, 4:48:39 AM8/16/22
to Google Apps Script Community
Hey all, hope you will be able to help me..

I have a problem with my html, I would like to do a menu which does a function if I click on the buttons. For the moment i did it:

My html code:
</html>
<body>
    <input type="button" value="a" onclick="a()">    <p></p>
    <span class="annuler" onclick="google.script.host.close()">Annuler</span>
    <script>
function a() {
  var ss = SpreadsheetApp.openById('xxxxxxxxxx');;
     var sheet = ss.getActiveSheet();
sheet.getRange('G6:BD405').clearContent();
        }
    </script>
  </body>
</html>

And my script is:
  const html = HtmlService.createHtmlOutputFromFile('form')
    .setWidth(300)
    .setHeight(485);
  SpreadsheetApp.getUi().showModalDialog(html, 'Boîte à outils');
}    

What I would like to is when I click on the a button, the fonction a() works, but at the moment, when I press the a button, it does nothing, only the "annuler" button is working and I do not know why. Can someone help me? :) 

Thanks a lot! :)

Edward Ulle

unread,
Aug 16, 2022, 9:23:12 AM8/16/22
to Google Apps Script Community
You can not run SpreadsheetApp. from the html client.  You need to move it to the Code.gs file that has the script that shows the dialog.  

Then in the <script> function a() { google.script.run.a(); }

CHARLIE BOUVIER

unread,
Aug 16, 2022, 10:06:32 AM8/16/22
to Google Apps Script Community
Dear, 

Thanks a lot, i understand how it works now, is there a way to show the status of this script? or to go away from the html page when i press the button? You ever helped me a lot..

Edward Ulle

unread,
Aug 16, 2022, 10:26:47 AM8/16/22
to Google Apps Script Community
I'm not sure what you mean by status of this script.  But if you want to click the button run function a and then close the page.  See this article https://developers.google.com/apps-script/guides/html/reference/run.

<script>
function a() {
    google.script.run.withSuccessHandler( 
        function () {
            google.script.host.close();
        }
    ).a();
}
</script>

CHARLIE BOUVIER

unread,
Aug 16, 2022, 10:54:37 AM8/16/22
to Google Apps Script Community
Hi,

Yeah the same but at the begining, lik I press and it closes instantly the page. Something like that but not at the end of the script :/ Sorry for my english and thanks a lot for you help.

CHARLIE BOUVIER

unread,
Aug 16, 2022, 10:58:03 AM8/16/22
to Google Apps Script Community
It is more like i would like to click, close and run the script :/

Edward Ulle

unread,
Aug 16, 2022, 11:05:14 AM8/16/22
to google-apps-sc...@googlegroups.com
Google.script.run are asynchronous, which means without the withSuccessHandler the script doesn;t wait for the function to finish before executing the next line of code.

<script>
function a() {
    google.script.run.a();
    google.script.host.close();
}
</script>

--
You received this message because you are subscribed to a topic in the Google Groups "Google Apps Script Community" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/google-apps-script-community/lusfet2bJhc/unsubscribe.
To unsubscribe from this group and all its topics, send an email to google-apps-script-c...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/google-apps-script-community/5fa30b85-12ea-4e30-8de4-fc3533e0986fn%40googlegroups.com.

CHARLIE BOUVIER

unread,
Aug 17, 2022, 3:10:15 AM8/17/22
to google-apps-sc...@googlegroups.com
Hi,

So there is no way to prevent people doing this script that the script is working, like in the html page something like "the script is working, please wait" and when the script is done, write in the html page " script finished" ?
At the moment it works and I thank you a lot for your help, the only thing I do not like is that when I press, the script works but we do not see that it is working :(

 width=Sans virus.www.avast.com

Ron Meske

unread,
Aug 17, 2022, 1:50:44 PM8/17/22
to Google Apps Script Community
In your script you can display messages using: SpreadsheetApp.getActiveSpreadsheet().toast(msg, title, timeoutseconds);

If you do this at the beginning of your script you can alert users that a script is running.  https://developers.google.com/apps-script/reference/spreadsheet/spreadsheet#toastmsg,-title,-timeoutseconds
Reply all
Reply to author
Forward
0 new messages