3

I wrote an Apps Script that takes a spreadsheet and converts it into a Google form. I want to display the form on my google site; however, I want the form to automatically refresh every time the site is opened, so that if the spreadsheet has changed, the form will also be updated when it displays. Essentially, I want the script to run automatically upon open of the Google site – any idea how to do this?

Also, as a side note (not as important), is there any way to incorporate a script into a google site without displaying it as a gadget? I don't want to display the script, I just want it to run in the background.

Kooshul
  • 33
  • 1
  • 3

1 Answers1

4

You can run an Apps Script function indirectly when the site loads by creating a stand alone Web App with HTML Service, publishing it, and putting window.onload into a script tag:

<script>
  window.onload = function() {
    console.log('Onload ran. ');//Open the browser console log to see debug messages

  };
</script>

Then use google.script.run to run a server function:

<script>
  window.onload = function() {
    console.log('hah! it ran. ');
    google.script.run
      .withSuccessHandler(run_This_On_Success)
      .gsServerFunction();
  };

 window.run_This_On_Success = function(the_Return) {
   console.log('was successful!: ' + the_Return);
 };

Code.gs

function gsServerFunction() {
  return true;
};

index.html

<!DOCTYPE html>
<html>
  <body>
    This is the body


    <script>
      window.onload = function() {
        console.log('hah! it ran. ');
        google.script.run
          .withSuccessHandler(wuzSugzezvul)
          .gsServerFunction();
       };

       window.wuzSugzezvul = function() {
         console.log('was successful!');
       };
    </script>
  </body>
</html>

Remove the border and the title of the apps script gadget, make it very small, and don't put in any text to display in the HTML.

Alan Wells
  • 30,746
  • 15
  • 104
  • 152
  • Thanks – I think that makes sense. However, I'm getting the error, "No HTML file named index was found. (line 7, file "Code"...)" – do I need to add the index.html as custom HTML box, or did I do something wrong? – Kooshul Dec 10 '15 at 15:53
  • The index.html file is an HTML file inside of the Apps Script code editor, as is the code.gs file also. The code.gs file is a default script file in a new project. With this strategy, you don't use an HTML box at all. You create a new Apps Script, then add it to an Apps Script gadget. – Alan Wells Dec 10 '15 at 16:13
  • That's what I did – I created a new Apps Script project containing my Code.gs and my index.html files, and then I published it as a web app, and added an Apps Script gadget to my site with the URL of the web app pasted in as the script, but it's still telling me that it can't find the index.html file – Kooshul Dec 10 '15 at 16:35
  • Don't put ".html" in the code. Just use the name. `createHtmlOutputFromFile('index')` The extension is assumed. That's the only other thing that I can think of that is causing the problem. – Alan Wells Dec 10 '15 at 16:50
  • Try running the `doGet()` code manually by clicking the Run button in the code editor. You may need to approve some permissions. Also, did you publish the script as a web app? Publish -> Deploy as Web App – Alan Wells Dec 10 '15 at 17:09