Google Apps脚本 - 包含外部文件无效的模板语句 [英] Google Apps Script - template statements to include external files not working
问题描述
我试图使用外部CSS样式表。但是我得到的是' * include stylesheet * '在我的html侧边栏中呈现。我在我的code.gs文件中创建了include函数。为什么这不起作用?
Index.html
< ;!DOCTYPE html>
< html>
< head>
< base target =_ top>
<?!= include('stylesheet'); ?>
< / head>
< body>
src =// ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js\">
< / script>
< / body>
< / html>
Code.gs
函数onOpen(){
SpreadsheetApp.getUi()
.createMenu('Form')
.addItem('Open',' openSidebar')
.addToUi();
}
函数包括(文件名){
返回HtmlService.createHtmlOutputFromFile(文件名)
.getContent();
函数openSidebar(){
var htmlOutput = HtmlService.createHtmlOutputFromFile('Index');
htmlOutput.setTitle('客户查询表');
SpreadsheetApp.getUi()。showSidebar(htmlOutput);
}
stylesheet.html
< style>
body {
background-color:'gray';
}
< / style>
此功能
函数openSidebar(){
var htmlOutput = HtmlService.createHtmlOutputFromFile('Index');
htmlOutput.setTitle('客户查询表');
SpreadsheetApp.getUi()。showSidebar(htmlOutput);
}
您正在使用 createHtmlOutputFromFile ,它将提供 Index.html 的内容,但它将不会执行里面的 scriptlet 。
为了执行脚本,您需要创建一个 HtmlTemplate ,然后调用
function openSidebar(){
var template = HtmlService.createTemplateFromFile('Index')
.evaluate()
.setTitle('Customer Inquiry Form')
SpreadsheetApp.getUi()。showSidebar(template);
}
同时移除'
值灰
在 stylesheet.html 中:
$ b
<风格>
html {
background-color:gray;
}
< / style>
I'm trying to use external CSS stylesheet. However what I get is the '* include stylesheet *' rendering in my html sidebar. I created the include function in my code.gs file. Why is this not working?
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>
Code.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>
At this function:
function openSidebar(){
var htmlOutput = HtmlService.createHtmlOutputFromFile('Index');
htmlOutput.setTitle('Customer Inquiry Form');
SpreadsheetApp.getUi().showSidebar(htmlOutput);
}
You're using the createHtmlOutputFromFile and it will serve the content of your Index.html, but it won't execute the scriptlets inside.
In order to execute the scriptlets you need to create an HtmlTemplate and then call evaluate() method, your function should look like this:
function openSidebar(){
var template = HtmlService.createTemplateFromFile('Index')
.evaluate()
.setTitle('Customer Inquiry Form')
SpreadsheetApp.getUi().showSidebar(template);
}
Also remove the '
of the value gray
in your stylesheet.html:
<style>
html{
background-color: gray;
}
</style>
这篇关于Google Apps脚本 - 包含外部文件无效的模板语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!