加载Google网站后会自动运行Google Apps脚本吗? [英] Run a Google apps script automatically upon loading a Google Site?

查看:54
本文介绍了加载Google网站后会自动运行Google Apps脚本吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我编写了一个Apps脚本,该脚本可以接收电子表格并将其转换为Google表单.我想在我的Google网站上显示表单;但是,我希望该表格在每次打开网站时自动刷新,以便如果电子表格已更改,则该表格在显示时也将进行更新.本质上,我希望脚本在打开Goog​​le网站后自动运行-任何想法如何做到这一点?

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?

此外,作为附带说明(不那么重要),是否有任何方法可以将脚本合并到Google网站中而不将其显示为小工具?我不想显示该脚本,只希望它在后台运行.

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.

推荐答案

您可以在网站加载时间接运行Apps脚本功能,方法是使用HTML Service创建独立的Web应用程序,将其发布并放置窗口.将加载到脚本标签中:

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>

然后,您将使用 google.script.run 运行服务器功能:

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);
 };

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>

然后,您可以删除应用程序脚本小工具的边框和标题,使其非常小,并且不要在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.

这篇关于加载Google网站后会自动运行Google Apps脚本吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆