我是 Google Apps 脚本的新手,正在尝试实现 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,但我已经适本地更改了所有文件。
最佳答案
您只需要一个名为include
的函数。现在你有很多函数都命名为“Include”。
要查找的文件的名称被传递给 include
函数:
<?!= include('Stylesheet'); ?>
在上面的行中,Stylesheet
是要从中获取内容的文件的名称。您需要多个脚本而不是多个函数。
迷你画廊.html
<?!= include('customcss'); ?>
<?!= include('layoutcss'); ?>
<?!= include('minigallerycss'); ?>
<?!= include('eyesjs'); ?>
<?!= include('jqueryjs'); ?>
etc.
在您的 .gs 文件中,只有一个 include 语句:
代码.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 服务。我找不到任何说明这一点的内容,但我知道我过去曾尝试过。所以,我很确定你需要做出其中一个决定。但是,如果有人知道一种方法,我当然很想知道。
关于javascript - 具有多个样式表和 js 链接的 Google Apps 脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27078975/