我正在尝试使用外部 CSS 样式表。然而,我得到的是在我的 html 侧边栏中呈现的“* include stylesheet *”。我在 code.gs 文件中创建了 include 函数。为什么这不起作用?
Index.html
<!DOCTYPE html>
<html>
<head>
<base target="_top">
<?!= include('stylesheet'); ?>
</head>
<body>
<script
src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js">
</script>
</body>
</html>
代码.gs
function onOpen() {
SpreadsheetApp.getUi()
.createMenu('Form')
.addItem('Open', 'openSidebar')
.addToUi();
}
function include(filename) {
return HtmlService.createHtmlOutputFromFile(filename)
.getContent();
}
function openSidebar(){
var htmlOutput = HtmlService.createHtmlOutputFromFile('Index');
htmlOutput.setTitle('Customer Inquiry Form');
SpreadsheetApp.getUi().showSidebar(htmlOutput);
}
stylesheet.html
<style>
body{
background-color:'gray';
}
</style>
最佳答案
在这个函数中:
function openSidebar(){
var htmlOutput = HtmlService.createHtmlOutputFromFile('Index');
htmlOutput.setTitle('Customer Inquiry Form');
SpreadsheetApp.getUi().showSidebar(htmlOutput);
}
您正在使用 createHtmlOutputFromFile它将提供您的 Index.html 的内容,但不会执行 scriptlets里面。
为了执行脚本,您需要创建一个 HtmlTemplate然后调用evaluate()方法,您的函数应如下所示:
function openSidebar(){
var template = HtmlService.createTemplateFromFile('Index')
.evaluate()
.setTitle('Customer Inquiry Form')
SpreadsheetApp.getUi().showSidebar(template);
}
同时在 stylesheet.html 中删除值 gray
的 '
:
<style>
html{
background-color: gray;
}
</style>
关于javascript - Google Apps 脚本 - 包含外部文件的模板语句不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39477092/