Google Apps脚本网络应用:无法分离CSS和JS代码 [英] Google Apps Script Web App: Can't separate out css and js code

查看:44
本文介绍了Google Apps脚本网络应用:无法分离CSS和JS代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将我的javascript和样式表与以Web App形式发布的Google Apps Spreadsheet脚本中的主HTML文件分开.我已经看到此答案解决问题,但我无法采用这种方法为我工作.

I'm trying to separate my javascript and stylesheet from the main HTML file in my Google Apps Spreadsheet script that is published as a Web App. I have seen this answer to the problem, but I cannot get this approach to work for me.

当我在主HTML文件中包含样式表和javascript时,它可以正常工作.当我尝试完全按照答案的建议将它们分开时,不会处理样式表和javascript代码,而是在浏览器中显示调用"getContent()"的行.似乎根本没有执行getContent().

When I have my stylesheet and javascript in the main HTML file, it works fine. When I try to separate them exactly as the answer recommends, the stylesheet and javascript code is not processed and instead the line that calls 'getContent()' is displayed in my browser. It looks like getContent() is not being executed at all.

我尝试将代码从电子表格移至独立Web应用程序,但仍然存在相同的问题.有什么想法为什么对我不起作用吗?谢谢!

I've tried moving my code away from the Spreadsheet to a Standalone Web App but still have the same problem. Any ideas why it's not working for me? Thank you!

我的Code.gs中的一些内容:

A bit from my Code.gs:

   function doGet() {
      var output = HtmlService.createHtmlOutputFromFile('index');
      output.setSandboxMode(HtmlService.SandboxMode.IFRAME);
      output.setTitle('Dashboard Tool');
      return output;
    }
function getContent(filename) {
  Logger.log('getContent()');  
  return HtmlService.createTemplateFromFile(filename).getRawContent();
}

index.html:

index.html:

<?!= getContent("stylesheet") ?>
    <div class='header'>Dashboard</div>
    <div id=content>
Content Here
</div>
<?!= getContent("javascript") ?>

'stylesheet.html'代码被style标签包围,而'javascript.html'代码被script标签包围.

'stylesheet.html' code is surrounded by the style tag and 'javascript.html' code is surrounded by the script tag.

推荐答案

您忘记了

You forgot evaluate() in the createHtmlOutputFromFile(), also you should use the createTemplateFromFile, as such.

var output = HtmlService.createTemplateFromFile('index').evaluate();

正如@ brian-p所指出的那样,您需要使用模板"而不是输出",因为评估发生在Scriplets <?!= ?>上.

As @brian-p pointed out, you need the 'Template' instead of 'Output', for the evaluate occur on the scriplets <?!= ?>.

这篇关于Google Apps脚本网络应用:无法分离CSS和JS代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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