加载Google网站后自动运行Google应用脚本?

时间:2015-12-09 21:41:11

标签: google-apps-script google-sheets google-form google-sites

我写了一个Apps脚本,它带有一个电子表格并将其转换为Google表单。我想在我的google网站上显示该表单;但是,我希望每次打开网站时表单都会自动刷新,这样如果电子表格发生了变化,表单也会在显示时更新。从本质上讲,我希望脚本在Google网站开放时自动运行 - 任何想法如何做到这一点?

另外,作为附注(不重要),有没有办法将脚本合并到谷歌网站而不将其显示为小工具?我不想显示脚本,我只是希望它在后台运行。

2 个答案:

答案 0 :(得分:3)

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. ');

  };
</script>

Then you would 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 doGet() {
  return HtmlService.createHtmlOutputFromFile('index')
            .setSandboxMode(HtmlService.SandboxMode.IFRAME);
};

function gsServerFunction() {
  return true;
};

index.html

<!DOCTYPE html>
<html>
  <head>
    <base target="_top">
  </head>
  <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>

Then you can 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.

答案 1 :(得分:0)

我为 myfile.html

成功尝试了此代码
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js">
</script>

<script>
    // on document load..
    $(function() {
      console.log('was successful!');
    });
 </script>

如果其他人觉得有帮助,我会按顺序添加这个答案。