如何在Google Apps脚本中将css连接到html

时间:2016-09-03 17:21:45

标签: html css google-apps-script

我正在尝试制作我的第一个Google App。我的HTML文档包含HTML Service: Best Practices中列出的include语句。

<html>
    <head>
        <base target="_top">
        <?!= include('stylesheet') ?>
    </head>
    <body>
       ...
    </body>

我的code.gs文件如下:

function doGet() {
   return HtmlService.createTemplateFromFile('BooksTS2.html')
  .evaluate();
}

我的样式表名为stylesheet.html,出于测试目的非常简单:

<style>
   p {color: green;}
</style>

但是当我运行它时,我收到此错误消息:ReferenceError:“include”未定义。

1 个答案:

答案 0 :(得分:5)

在示例中,他们创建了可重复使用的函数 include(),您需要将其添加到 code.gs 文件中:

function include(filename) {
  return HtmlService.createHtmlOutputFromFile(filename)
      .getContent();
}

您可以随时在html文件中直接使用打印scriptlet,如下所示:

<html>
    <head>
        <base target="_top">
    <?!= HtmlService.createHtmlOutputFromFile('stylesheet').getContent(); ?>
    </head>
    <body>
       ...
    </body>
</html>