[英]How to include CSS and javascript file in Google Script App
I am new about Google App Script .我是Google App Script的新手。 I have done coding in google script app.
我在谷歌脚本应用程序中完成了编码。 I have read the best practice for adding CSS and JavaScript. I have followed this documentation and created a JavaScript and CSS file.
我已阅读添加 CSS 和 JavaScript 的最佳实践。我已按照此文档创建了一个 JavaScript 和 CSS 文件。 But when I have run this code, it's showing me wrong output.
但是当我运行这段代码时,它显示错误的 output。
Documentaion Link: https://developers.google.com/apps-script/guides/html/best-practices#separate_html_css_and_javascript文档链接: https://developers.google.com/apps-script/guides/html/best-practices#separate_html_css_and_javascript
My code is below:我的代码如下:
code.gs代码.gs
function doGet(request) {
return HtmlService.createTemplateFromFile('page')
.evaluate();
}
function include(filename) {
return HtmlService.createHtmlOutputFromFile(filename)
.getContent();
}
function onOpen() {
SpreadsheetApp.getUi() // Or DocumentApp or SlidesApp or FormApp.
.createMenu('Dialog')
.addItem('Open', 'openSidebar')
.addToUi();
}
function openSidebar() {
var htmlOutput = HtmlService.createHtmlOutputFromFile('page').setTitle('Dashboard');
SpreadsheetApp.getUi().showSidebar(htmlOutput); SpreadsheetApp.getUi().showSidebar(htmlOutput); }
}
page.html页.html
<!DOCTYPE html>
<html>
<head>
<base target="_top">
<?!= include('stylesheet'); ?>
</head>
<body>
<h1>Welcome</h1>
<p>Please enjoy this helpful script.</p>
<?!= include('javascript'); ?>
</body>
</html>
stylesheet.html样式表.html
<style>
p {
color: green;
}
</style>
javascript.html javascript.html
<script>
window.addEventListener('load', function() {
console.log('Page is loaded');
});
</script>
When I click on run with the "opOpen" option it's run success full, but in the out there is nothing any effect.当我使用“opOpen”选项单击运行时,它运行成功,但在外面没有任何效果。 Its show include line.
它的节目包括线。
I'll write the answer here, since it will be better readable.我会在这里写下答案,因为它的可读性更好。
Your openDialog
function should look like this你的
openDialog
function 应该是这样的
function openDialog() {
var html = HtmlService.createTemplateFromFile('page').evaluate();
SpreadsheetApp.getUi().showModalDialog(html, 'Dialog title');
}
you also don't need doGet
for modal dialog or sidebar!您也不需要
doGet
模式对话框或侧边栏!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.