运行 Google 应用程序脚本在加载 Google 站点时自动执行?

Run a Google apps script automatically upon loading a Google Site?

我编写了一个 Apps 脚本,它接受电子表格并将其转换为 Google 形式。我想在我的 google 网站上显示该表格;但是,我希望每次打开站点时表单都自动刷新,这样如果电子表格发生变化,表单在显示时也会更新。本质上,我希望脚本在 Google 网站打开时自动 运行 – 知道如何做到这一点吗?

此外,作为旁注(不那么重要),有没有什么方法可以将脚本合并到 google 站点而不将其显示为小工具?我不想显示脚本,我只想让它在后台 运行。

您可以 运行 在站点加载时间接地执行 Apps 脚本功能,方法是创建一个带有 HTML 服务的独立 Web 应用程序,发布它,然后将 window.onload 放入脚本标记中:

<script>
  window.onload = function() {
    console.log('Onload ran. ');

  };
</script>

然后您将使用 google.script.run 到 运行 服务器函数:

<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>

那你可以去掉apps script gadget的边框和标题,把它弄的很小,不要放任何文字显示在HTML.