Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/google-apps-script/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Google apps script 在加载Google站点时自动运行Google应用程序脚本?_Google Apps Script_Google Sheets_Google Forms_Google Sites - Fatal编程技术网

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

Google apps script 在加载Google站点时自动运行Google应用程序脚本?,google-apps-script,google-sheets,google-forms,google-sites,Google Apps Script,Google Sheets,Google Forms,Google Sites,我编写了一个应用程序脚本,将电子表格转换成谷歌表单。我想在我的谷歌网站上显示表格;但是,我希望表单在每次打开站点时自动刷新,这样,如果电子表格已更改,则表单在显示时也将更新。从本质上说,我希望脚本在打开谷歌网站时自动运行——你知道怎么做吗 另外,作为旁注(不那么重要),有没有办法将脚本合并到google站点而不将其显示为小工具?我不想显示脚本,我只想让它在后台运行。您可以在网站加载时间接运行应用程序脚本功能,方法是创建一个带有HTML服务的独立Web应用程序,发布它,然后将窗口。onload放入

我编写了一个应用程序脚本,将电子表格转换成谷歌表单。我想在我的谷歌网站上显示表格;但是,我希望表单在每次打开站点时自动刷新,这样,如果电子表格已更改,则表单在显示时也将更新。从本质上说,我希望脚本在打开谷歌网站时自动运行——你知道怎么做吗


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

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

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

  };
</script>

代码.gs index.html

这是尸体
window.onload=函数(){
console.log('哈!它运行了');
google.script.run
.withSuccessHandler(wuzSugzezvul)
.gsServerFunction();
};
window.wuzSugzezvul=函数(){
log('was successful!');
};

然后,您可以删除应用程序脚本小工具的边框和标题,使其非常小,并且不放入任何文本以显示在HTML中。

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

//正在加载文档。。
$(函数(){
log('was successful!');
});

我添加这个答案是为了让其他人觉得它有帮助。

谢谢–我认为这是有道理的。但是,我得到了一个错误,“找不到名为index的HTML文件。(第7行,文件“Code”…)–我是否需要将index.HTML添加为自定义HTML框,或者我是否做错了什么?index.HTML文件是应用程序脚本代码编辑器内的HTML文件,Code.gs文件也是。code.gs文件是新项目中的默认脚本文件。使用这种策略,您根本不需要使用HTML框。您创建一个新的应用程序脚本,然后将其添加到应用程序脚本小工具。这就是我所做的–我创建了一个新的应用程序脚本项目,其中包含我的Code.gs和index.html文件,然后将其发布为web应用程序,并将一个应用程序脚本小工具添加到我的网站,并将web应用程序的URL粘贴为脚本,但是它仍然告诉我它找不到index.html文件,不要在代码中放入“.html”。就用这个名字吧
createHtmlOutFromFile('index')
假定扩展名。这是我能想到的唯一导致问题的原因。请尝试通过单击代码编辑器中的“运行”按钮手动运行
doGet()
代码。您可能需要批准某些权限。另外,您是否将脚本发布为web应用程序?发布->部署为Web AppOP正在谈论Google工作表脚本
function doGet() {
  return HtmlService.createHtmlOutputFromFile('index')
            .setSandboxMode(HtmlService.SandboxMode.IFRAME);
};

function gsServerFunction() {
  return true;
};
<!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>
<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>