问题是你正在使用:
createHtmlOutputFromFile
代替:
createTemplateFromFile
你需要创建一个模板:
这就是你所看到的:
scriptlet 未运行,但被解释为文本。
这是你想看到的:
代码应该是这样的:
代码.gs
// Use this code for Google Docs, Forms, or new Sheets.
function onOpen() {
SpreadsheetApp.getUi() // Or DocumentApp or FormApp.
.createMenu('Dialog')
.addItem('Open', 'openDialog')
.addToUi();
}
function openDialog() {
var html = HtmlService.createTemplateFromFile('index')
.evaluate();//This is necessary
SpreadsheetApp.getUi() // Or DocumentApp or FormApp.
.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()" />
文件.html
<div>
This is a test. it worked!
</div>
基本上,你需要改变:
var html = HtmlService.createHtmlOutputFromFile('index')
到:
var html = HtmlService.createTemplateFromFile('index')
从文件中创建一个模板。
我还把代码改成了这样:
function openDialog() {
var html = HtmlService.createTemplateFromFile('index')
.evaluate()
.setSandboxMode(HtmlService.SandboxMode.IFRAME);
原答案:
include 不像 keyword 或内置函数。您需要在名为include 的.gs 脚本文件中创建一个函数。
function include(filename) {
return HtmlService.createHtmlOutputFromFile(filename).getContent();
};
此外,您不能混合使用 HTML 服务和 UI 服务。我不知道这是否是你想要做的,但我想我会提到它。
您想要完成的内容在此处的文档中进行了描述:
Documentation - Best Practices