问题在于您正在使用:
createHtmlOutputFromFile
改为:
createTemplateFromFile
你需要创建一个模板:
这是你所看到的:
脚本块未运行,而是被解释为文本。
这是你想看到的:
以下是代码应该如何编写:
Code.gs
function onOpen() {
SpreadsheetApp.getUi()
.createMenu('Dialog')
.addItem('Open', 'openDialog')
.addToUi();
}
function openDialog() {
var html = HtmlService.createTemplateFromFile('index')
.evaluate();
SpreadsheetApp.getUi()
.showModalDialog(html, 'Dialog title');
}
function include(File) {
return HtmlService.createHtmlOutputFromFile(File).getContent();
};
index.html
<?!= include('File')
Hello, world!
<input type="button" value="Close"
onclick="google.script.host.close()" />
File.html
<div>
This is a test. it worked!
</div>
基本上,您需要更改:
var html = HtmlService.createHtmlOutputFromFile('index')
to:
var html = HtmlService.createTemplateFromFile('index')
从文件创建模板。
我也将代码更改为:
function openDialog() {
var html = HtmlService.createTemplateFromFile('index')
.evaluate()
.setSandboxMode(HtmlService.SandboxMode.IFRAME);
翻译后的答案:
include
不是像关键字或内置函数一样的东西。您需要在一个名为 .gs
的脚本文件中创建一个名为 include
的函数。
function include(filename) {
return HtmlService.createHtmlOutputFromFile(filename).getContent();
};
此外,您不能混合使用HTML服务和UI服务。我不知道您是否想这样做,但我想提一下。
您想要完成的内容在此文档中进行了描述:
文档 - 最佳实践