If I create a simple HTML web app in Google Apps Script, like this:
function doGet() { return HtmlService.createHtmlOutputFromFile("index.html"); }
and index.html looks like this:
<script src="http://code.jquery.com/jquery-1.7.2.min.js"></script> <div>Test</div>
is it possible to add JS and CSS files as part of the project and include them using script and link tags, or do they have to be inline/hosted elsewhere?
Google Apps Script is a rapid application development platform that makes it fast and easy to create business applications that integrate with Google Workspace. You write code in modern JavaScript and have access to built-in libraries for favorite Google Workspace applications like Gmail, Calendar, Drive, and more.
To link a CSS file with your HTML file, you have to write the next script on your HTML file inside the head tag. To link a Js file with your HTML, you only have to add the source of the script inside the body tag or outside; it doesn't matter.
To add an HTML file to your Apps Script project, open the Script Editor and choose File > New > HTML File. Within the HTML file, you can write most standard HTML, CSS, and client-side JavaScript. The page will be served as HTML5, although some advanced features of HTML5 are not available, as explained in Restrictions.
Google Apps Script allows us to use jQuery, which is the preferred technique in HTML programing to date.
Here is a workaround I have found useful:
Add this function to your server-side Javascript:
function getContent(filename) { return HtmlService.createTemplateFromFile(filename).getRawContent(); }
Add a second 'html' file to your project that contains only the JS or CSS surrounded by <script>
or <style>
tags as appropriate.
<!-- myscript.js.html --> <script> alert("Script included!"); </script>
Now you can include the script in your main HTML template like this:
<?!= getContent("myscript.js") ?>
This way, you can have your JS and CSS split into as many files as you like and keep them all accessible from within the Apps Script project.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With