Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use project Javascript and CSS files in a Google Apps Script web app?

Tags:

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?

like image 672
Alf Eaton Avatar asked Jul 05 '12 12:07

Alf Eaton


People also ask

Can I use JavaScript in Google app script?

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.

How do I combine HTML CSS and JavaScript?

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.

How do you call an HTML File from an app script?

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.

Can I use jQuery in Google app script?

Google Apps Script allows us to use jQuery, which is the preferred technique in HTML programing to date.


1 Answers

Here is a workaround I have found useful:

  1. Add this function to your server-side Javascript:

    function getContent(filename) {     return HtmlService.createTemplateFromFile(filename).getRawContent(); } 
  2. 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> 
  3. 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.

like image 98
Nick Rioux Avatar answered Oct 16 '22 10:10

Nick Rioux