0
votes

It appears that when one uses HtmlService.createTemplateFromFile(), the html code that is in the file is just a snippet. The method packages the snippet in an html wrapper which adds things like the DTD, and html, head, and body tags to form a complete html document. The documentation (https://developers.google.com/apps-script/guides/html/) suggests that HtmlService.createHtmlOutputFromFile() works differently. It suggests that in the later case the relevant file contains the ENTIRE html code and the method does not add a wrapper. Experimentation reveals that the later works like the former in that a wrapper is added. My question is this: Is there some way to return a snippet of html code (i.e., no wrapper) from an app-script? The reason that I ask is that I would like to be able to use AJAX to insert a snippet of code from an app-script into some point in the DOM structure. Thanks for any input.

 ... doug

P.S. I realize it's probably going to be a separate issue; but, it would be nice if the snippet could include a scriptlet.

1
Apps Script has a Content Service that will return content. You would probably need to have an If, then statement in your doGet() function that would either load your regular HTML Service, OR return content depending on a search string passed in the URL. - Alan Wells
I tried the Content Service. It does seem to go quite a ways towards solving my problem. It looks like I'm stuck to enter the code as a string rather than via the more convenient means of using a file as with the methods in the html service. That's probably not a huge deal as it is anticipated that the code will be fairly short "snipits". I'm more concerned about the fact that it doesn't appear that I will be able to include scriptlets in the snipit. That's a feature that I would really like. Thanks for the input. - user3311045
Why does the HTML need to come from the Apps Script? Could it come from a file in your Google Drive? - Alan Wells
In the case of static code (e.g., hello world), I would think that the HTML could come from Google Drive. However, I also think there may be other cases where one might want it to come from the script. Unfortunately, my question was just general in nature and not related to a specific issue. I need to learn more. You've been most helpful. Thanks for the input. - user3311045

1 Answers

0
votes

In general, I can think of 5 basic strategies:

  • Static HTML from within an Apps Script project
  • Static HTML from a file outside of Apps Script
  • Dynamic HTML created from server side code
  • Dynamic HTML created with JavaScript in the browser, with data retrieved from somewhere.
  • Retrieve HTML from some other website's page.

You want to use a scriptlet, which is dynamic HTML, triggered from the front end. If it's dynamic HTML, the HTML can be dynamically changed from Javascript in the browser, or you could formulate HTML from the server, and then return a string of HTML. I guess there are multiple strategies for how this could be done.

You can call a Google Apps Script from another website, and use Apps Script as server side code without directly returning any output to the screen. You would use Content Service to do that.