【问题标题】:Google Apps Script with multiple stylesheet & js links具有多个样式表和 js 链接的 Google Apps 脚本
【发布时间】:2014-11-22 15:20:51
【问题描述】:

我是 Google Apps 脚本的新手,正在尝试在我的 Google 网站中实现 spacegallery

我已经在脚本编辑器中将我的 css 样式表和 .js 文件分离为 .html 并编写了我的 html 页面 (minigallery.html),现在我正在尝试将它们全部链接到 Code.gs 部分。这是我到目前为止使用的示例: https://developers.google.com/apps-script/guides/html/best-practices#separate_html_css_and_javascript

这是我目前在我的 Code.gs 中的内容,但是当我运行已发布的版本时,css 和 js 文件似乎没有链接(当然我还没有放图片)。您可以在此处找到已发布的链接:

//script.google.com/macros/s/AKfycbybCtW9y-iCT81WvBoCmfGnXWhSwQ5dpa7xyHU_H1ot/dev

`
 // Script-as-app template.
function doGet() {
  var app = UiApp.createApplication().setTitle('Business Card Mini Gallery | MAvC Graphics.').setHeight(50).setWidth(100)
  return HtmlService.createTemplateFromFile('minigallery')
      .evaluate();
}function include(customcss) {
  return HtmlService.createHtmlOutputFromFile('customcss')
      .getContent();
}function include(layoutcss) {
  return HtmlService.createHtmlOutputFromFile('layoutcss')
      .getContent();
}function include(minigallerycss) {
  return HtmlService.createHtmlOutputFromFile('minigallerycss')
      .getContent();
}function include(eyesjs) {
  return HtmlService.createHtmlOutputFromFile('eyejs')
      .getContent();
}function include(jqueryjs) {
  return HtmlService.createHtmlOutputFromFile('jqueryjs')
      .getContent();
}function include(layoutjs) {
  return HtmlService.createHtmlOutputFromFile('layoutjs')
      .getContent();
}function include(utilsjs) {
  return HtmlService.createHtmlOutputFromFile('utilsjs')
      .getContent();
}function include(minigalleryjs) {
  return HtmlService.createHtmlOutputFromFile('minigalleryjs')
      .getContent();
}

` 这是我写得很糟糕的 minigallery.html 代码,用于链接 css 和 js(第一行和最后一行是最重要的):

    <?!= include('customcss'), ('layoutcss'), ('minigallerycss'); ?>

...//lengthy inner page code

<?!= include('eyejs'), ('jqueryjs'), ('layoutjs'), ('minigalleryjs'), ('utilsjs'); ?>

为了使事情复杂化,我将整个内容从 spacegallery 重命名为 minigallery,但我已经适当地更改了所有文件。

【问题讨论】:

  • 您具体遇到了什么问题?
  • 将 css 和 js 文件链接到 html,因为当我查看已发布的页面时,它显示没有样式或操作
  • 我没有收到任何调试错误

标签: javascript jquery html css google-apps-script


【解决方案1】:

您只需要一个名为include 的函数。现在你有很多函数都命名为“Include”。

要查找的文件名传递给include函数:

<?!= include('Stylesheet'); ?>

在上面的行中,Stylesheet 是要从中获取内容的文件的名称。您需要多个脚本,而不是多个函数。

迷你画廊.html

<?!= include('customcss'); ?>
<?!= include('layoutcss'); ?>
<?!= include('minigallerycss'); ?>
<?!= include('eyesjs'); ?>
<?!= include('jqueryjs'); ?>
etc.

在您的 .gs 文件中,只有一个包含语句:

代码.gs

function doGet() {

  return HtmlService.createTemplateFromFile('minigallery')
   .evaluate() // evaluate MUST come before setting the NATIVE mode
   .setTitle('Business Card Mini Gallery | MAvC Graphics.')
   .setSandboxMode(HtmlService.SandboxMode.NATIVE);
}

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

该代码直接取自文档。 filename 是一个变量,它可以接受任何传递给它的值。

您可以将 HTML 与 UI 服务一起使用。见以下链接:

Google Documentation - Use HTML with UI Service

但是,我认为您不能同时使用 HTML 和 UI 服务。我找不到任何说明这一点的东西,但我知道我过去曾尝试过。所以,我很确定你需要决定其中一个。但如果有人知道方法,我肯定想知道。

【讨论】:

  • 我真的想通了!原来我不需要包含。现在是我的 Code.gs:
  • function doGet() { var result=HtmlService.createTemplateFromFile('minigalery').evaluate();返回结果; } function getContent(filename) { var return1= HtmlService.createTemplateFromFile('minigalery').getRawContent();返回返回1; }
  • 和我的 minigallery.html pg:
  • 哦,我无法发布它,因为代码太长,而且我还是新手,无法赚取积分。你可以在这里查看:sites.google.com/site/typsetgraphics/home/…
  • 所以现在我只需要更新图片以使其看起来更好,并使用最近完成的名片就完成了!
猜你喜欢
  • 2023-03-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-01-28
相关资源
最近更新 更多