我正在加载一个模态对话框:
var html = HtmlService.createHtmlOutputFromFile('File')
.setSandboxMode(HtmlService.SandboxMode.IFRAME)
.setWidth(1000)
.setHeight(700);
SpreadsheetApp.getUi()
.showModalDialog(html, 'My Page');
现在,在 File.HTML 中,我想加载另一个带有 CSS 设置的 HTML 文件,我该怎么做?
我已经尝试使用 scriptlets 将它包含在
HtmlTemplate
中,但它不起作用:<?!= include('File'); ?>
编辑:
我在 code.gs 中定义了 include 函数:
function include (file) {
return HtmlService.createTemplateFromFile(file).evaluate().getContent();
}
最佳答案
问题是您正在使用:
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();
};
索引.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
或内置函数。您需要在名为 .gs
的 include
脚本文件中创建一个函数。 function include(filename) {
return HtmlService.createHtmlOutputFromFile(filename).getContent();
};
此外,您不能混合使用 HTML 服务和 UI 服务。我不知道这是否是你想要做的,但我想我会提到它。您想要完成的内容在此处的文档中进行了描述:
Documentation - Best Practices
关于html - 如何在 Google Apps Script 中的 HTMLOutput 中使用 scriptlet,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/27510858/