1

Google Apps Script recommends that you separate your CSS and JS from the HTML, using .html files. I have read and followed this documentation, but it's not working for me. Are the scriplets not working correctly? Here is what I have:

Html:

<!DOCTYPE html>
<html>
<head>
<?!= include('Stylesheet'); ?>
<?!= include('Javascript'); ?>
<base target="_top">
<link rel="stylesheet" href="https://ssl.gstatic.com/docs/script/css/add-ons1.css">
<link rel="stylesheet" href="https://fonts.googleapis.com/icon?family=Material+Icons">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">

</head>

Javascript.html

 <script>
/* stuff */
 </script>

Stylesheet.html

<style>
/*stuff*/
</style>

.gs

function doGet(request) {
  return HtmlService.createTemplateFromFile('Page').evaluate();
}

function include(filename) {
  return HtmlService.createHtmlOutputFromFile(filename).getContent();
}
10
  • That doesn't seem to work Commented Nov 6, 2017 at 21:26
  • When you retry your changes, are you using the "exec" or "dev" version of the code? The "exec" version is the published version that has "exec" on the end of the URL. If you are not using the development version, then you need to publish a new update every time that you want to test the published version. Commented Nov 6, 2017 at 21:29
  • 1
    You can see what HTML content has been served to your browser by using the browser development tools. Right click the page, and choose "inspect" The development tools will open, with the HTML shown. Check if the CSS and JavaScript is present. That's the best way to know for sure what is actually included. If there is malformed HTML, (Bad CSS or JavaScript code) there might be an error serving the page. Sometimes you can save the file, but there is an uncaught error in your code or CSS, that won't show up when you save the file. Hit the f12 key and look for error messages in the console. Commented Nov 6, 2017 at 21:35
  • It's not being included. The <?!= include('Stylesheet'); ?> and <?!= include('Javascript'); ?> lines are being displayed as text on the sidebar for some reason Commented Nov 6, 2017 at 22:02
  • Run the doGet() function from the code editor, and see if you get a prompt to authorize the code. There is nothing wrong with the code. Something else is wrong. Commented Nov 7, 2017 at 0:36

2 Answers 2

1

There is a problem with the example

The only problem with the example is that the filenames here in the documentation are all capitalized as follows:

CODE.GS PAGE.HTML STYLESHEET.HTML JAVASCRIPT.HTML

Which is unfortunate because the names as they are used in the code is different.

To Code.gs file:

function doGet(request) {
  return HtmlService.createTemplateFromFile('Page')
      .evaluate();//not all caps but it has to match the name of the file and it doesn't - Change to PAGE
}

function include(filename) {
  return HtmlService.createHtmlOutputFromFile(filename)
      .getContent();
}

The PAGE.HTML file:

<!DOCTYPE html>
<html>
  <head>
    <base target="_top">
    <?!= include('Stylesheet'); ?><!-- Change to STYLESHEET -->
  </head>
  <body>
    <h1>Welcome</h1>
    <p>Please enjoy this helpful script.</p>
    <?!= include('JavaScript'); ?><!-- Change to JAVASCRIPT -->
  </body>
</html>

Again Stylesheet and Javascript is not all capitalized and again both parameters of the include function should match the file names exactly and they don't.

STYLESHEET.HTML:

<style>
p {
  color: green;
}
</style>

JAVASCRIPT.HTML file:

<script>
window.addEventListener('load', function() {
  console.log('Page is loaded');
});
</script>

Once you get all of the page names the same as they are used in the code the outputs is:

Welcome

Please enjoy this helpful script.

And I think it would be a lot more helpful if the names of the files were not capitalized in the first place because it's pretty easy for a newbie to get this mixed up because they're trying desperately just to get everything copied exactly hoping that it will work.

Sign up to request clarification or add additional context in comments.

4 Comments

Good observation, I logged an issue concerning that. However my names for my files are correct.
Well I copied the tutorials and only changed the filenames and it worked for me.
I'm using Gapps scripts to make a sidebar add-on to google docs.
Here's a link to a simple Google Docs envelope printer that I did and it has a sidebar in it. Link
0

There is no stylesheet.html or javascript.html. They are all in one file, the script and style tags. If you want to put them in separate files you would put your javascript in a .js file and your css in a .css file.

3 Comments

There is no CSS file type inside an Apps Script project, but CSS is nothing but HTML. You don't need a special file type of CSS in Apps Script in order to separate out CSS, HTML and JavaScript. There is no .js file type inside of an Apps Script project. It's either a script or HTML. But a script tag is just another type of HTML.
According to google documentation, this link is how they want you to do it, but its not working
The separation of CSS and JavaScript into different files is a recommendation. It's for your benefit. It helps to organize your code and make sense of it. It's easier to maintain and work with.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.